Class: Menu::Base
- Inherits:
-
Object
- Object
- Menu::Base
- 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.
Direct Known Subclasses
AdapterMenu, DatabaseOption, EntityAddition, EntityQueries, MainMenu, PersonOption, TimeMenu::TimeMenu, WorktimeQueries
Instance Attribute Summary collapse
-
#menu_description ⇒ String
readonly
private
A string with the head description of the menu.
-
#menu_items ⇒ Hash
readonly
private
A hash which maps (number => string) for the menu items.
Instance Method Summary collapse
-
#add_menu_item(description, index = nil) ⇒ Object
private
method to add a given key/value pair to the menu hash.
- #define_menu_items ⇒ Object private abstract
- #determine_action(input) ⇒ Object private abstract
-
#get_entry(message) ⇒ String
private
method to print a given message and read the provided input.
-
#handle_wrong_option ⇒ Object
private
default behavior when a user provides not valid input.
-
#initialize(description = nil) ⇒ Base
constructor
initialization.
-
#print_menu ⇒ Object
method to print the menu to the terminal and wait for input.
Constructor Details
#initialize(description = nil) ⇒ Base
initialization
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 @menu_items = Hash[@menu_items.sort] end |
Instance Attribute Details
#menu_description ⇒ String (readonly, private)
Returns a string with the head description of the menu.
38 39 40 |
# File 'lib/menu/base_menu.rb', line 38 def @menu_description end |
#menu_items ⇒ Hash (readonly, private)
Returns a hash which maps (number => string) for the menu items.
36 37 38 |
# File 'lib/menu/base_menu.rb', line 36 def @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
66 67 68 69 |
# File 'lib/menu/base_menu.rb', line 66 def (description, index=nil) index = @menu_items.length + 1 if (index == nil) @menu_items[index] = description end |
#define_menu_items ⇒ Object (private)
subclasses need to implement this method
42 43 44 45 46 |
# File 'lib/menu/base_menu.rb', line 42 def 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)
subclasses need to implement 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
74 75 76 77 |
# File 'lib/menu/base_menu.rb', line 74 def get_entry() print .blue.bright gets.chomp end |
#handle_wrong_option ⇒ Object (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 |
#print_menu ⇒ Object
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 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 |