Class: Menu::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/menu/base_menu.rb

Overview

This class provides the common methods of the different query menus The children need to define the method define_menu_items and determine_action. If the child class does not implement this method Base raises a NotImplementedError.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description = nil) ⇒ Base

initialization

Parameters:

  • description (String) (defaults to: nil)

    the headline of the menu



11
12
13
14
15
16
17
18
# File 'lib/menu/base_menu.rb', line 11

def initialize(description=nil)
  @menu_items = Hash.new()
  if (description == nil)
    @menu_description = "Default menu. Please add description: "
  end
  define_menu_items
  @menu_items = Hash[@menu_items.sort]
end

Instance Attribute Details

Returns a string with the head description of the menu.

Returns:

  • (String)

    a string with the head description of the menu



38
39
40
# File 'lib/menu/base_menu.rb', line 38

def menu_description
  @menu_description
end

Returns a hash which maps (number => string) for the menu items.

Returns:

  • (Hash)

    a hash which maps (number => string) for the menu items



36
37
38
# File 'lib/menu/base_menu.rb', line 36

def menu_items
  @menu_items
end

Instance Method Details

#add_menu_item(description, index = nil) ⇒ Object (private)

method to add a given key/value pair to the menu hash

Parameters:

  • description (String)

    the description of the menu item

  • index (Integer) (defaults to: nil)

    the index that should be used as key



66
67
68
69
# File 'lib/menu/base_menu.rb', line 66

def add_menu_item(description, index=nil)
  index = @menu_items.length + 1 if (index == nil)
  @menu_items[index] = description
end

#define_menu_itemsObject (private)

This method is abstract.

subclasses need to implement this method

Raises:

  • (NotImplementedError)

    if the subclass does not have this method



42
43
44
45
46
# File 'lib/menu/base_menu.rb', line 42

def define_menu_items
  fail NotImplementedError, " Error: the subclass " \
    "#{self.name.split("::").last} needs to implement the method: " \
    "define_menu_items from its base class".red
end

#determine_action(input) ⇒ Object (private)

This method is abstract.

subclasses need to implement this method

Parameters:

  • input (String)

    the provided user input

Raises:

  • (NotImplementedError)

    if the subclass does not have this method



51
52
53
54
55
# File 'lib/menu/base_menu.rb', line 51

def determine_action(input)
  fail NotImplementedError, " Error: the subclass " \
    "#{self.name.split("::").last} needs to implement the method: " \
    "determine_action from its base class".red
end

#get_entry(message) ⇒ String (private)

method to print a given message and read the provided input

Parameters:

  • message (String)

    output message

Returns:

  • (String)

    the input from the terminal



74
75
76
77
# File 'lib/menu/base_menu.rb', line 74

def get_entry(message)
  print message.blue.bright
  gets.chomp
end

#handle_wrong_optionObject (private)

default behavior when a user provides not valid input



58
59
60
61
# File 'lib/menu/base_menu.rb', line 58

def handle_wrong_option
  print "Option not available. ".red
  determine_action(get_entry("Select option: "))
end

method to print the menu to the terminal and wait for input



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/menu/base_menu.rb', line 21

def print_menu
  isFinished = true
  while (isFinished)
    puts @menu_description
    @menu_items.each_pair { |key, value|
      puts "(#{key}) #{value}"
    }

    isFinished = determine_action(get_entry("Select option: "))
  end
end