Class: Menu::Base

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

Overview

abstract parent class for the menu hierarchy

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



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
  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



32
33
34
# File 'lib/menu/base_menu.rb', line 32

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



30
31
32
# File 'lib/menu/base_menu.rb', line 30

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



60
61
62
63
# File 'lib/menu/base_menu.rb', line 60

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



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

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



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

Parameters:

  • message (String)

    output message

Returns:

  • (String)

    the input from the terminal



68
69
70
71
# File 'lib/menu/base_menu.rb', line 68

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

#handle_wrong_optionObject (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

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 print_menu
  puts @menu_description
  @menu_items.each_pair { |key, value|
    puts "(#{key}) #{value}"
  }

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