Class: Menu::Base
- Inherits:
-
Object
- Object
- Menu::Base
- Defined in:
- lib/menu/base_menu.rb
Overview
abstract parent class for the menu hierarchy
Direct Known Subclasses
DiagramMenu, IndexOutputMenu, IndexOverviewMenu, IndexSelectionMenu, MainMenu, SubselectMenu
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
8 9 10 11 12 13 14 15 |
# File 'lib/menu/base_menu.rb', line 8 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.
32 33 34 |
# File 'lib/menu/base_menu.rb', line 32 def @menu_description end |
#menu_items ⇒ Hash (readonly, private)
Returns a hash which maps (number => string) for the menu items.
30 31 32 |
# File 'lib/menu/base_menu.rb', line 30 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
60 61 62 63 |
# File 'lib/menu/base_menu.rb', line 60 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
36 37 38 39 40 |
# File 'lib/menu/base_menu.rb', line 36 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
45 46 47 48 49 |
# File 'lib/menu/base_menu.rb', line 45 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
68 69 70 71 |
# File 'lib/menu/base_menu.rb', line 68 def get_entry() print .blue.bright gets.chomp end |
#handle_wrong_option ⇒ Object (private)
default behavior when a user provides not valid input
52 53 54 55 |
# File 'lib/menu/base_menu.rb', line 52 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
18 19 20 21 22 23 24 25 |
# File 'lib/menu/base_menu.rb', line 18 def puts @menu_description @menu_items.each_pair { |key, value| puts "(#{key}) #{value}" } determine_action(get_entry("Select option: ")) end |