Class: Menu

Inherits:
Object
  • Object
show all
Extended by:
Logging
Defined in:
lib/menu.rb

Overview

Shows options that the user can choose from. Options may be associated with sub-menus.

Defined Under Namespace

Classes: MenuError

Constant Summary collapse

@@log =
self.init_logger(STDOUT)
@@global_elements =
[]
@@hidden_elements =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

init_logger, log_level=, log_target=

Methods included from File_Checking

#file_check, file_check

Constructor Details

#initialize(options = {}) ⇒ Menu

Returns a new instance of Menu.



46
47
48
49
50
51
52
53
54
# File 'lib/menu.rb', line 46

def initialize(options = {})
  @log = @@log
  @elements = [] 
  if(options && !options.respond_to?(:to_hash) )
    raise MenuError.new('Arguments to menu.new must be hash-pairs')
  end
  @name = options[:name] if options[:name]
  @key = options[:key] if options[:key]
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#add(element, pos = nil) ⇒ Object Also known as: add_action, add_sub_menu, add_menu



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/menu.rb', line 60

def add(element, pos = nil)
  if(element.respond_to?(:call))
    if(pos)
      @elements.insert(pos, element)
    else
      @elements << element 
    end
    # make available the same element in any sub-menus.
    if(element.respond_to?(:global) && element.global)
      @@global_elements << element
    end
    # hidden elements
    if(element.respond_to?(:hidden) && element.hidden)
      @@hidden_elements << element
    end
  else
    raise MenuError("{#element.to_s} cannot be used as action or sub-menu: missing method 'call()'}")
  end
end

#call(*args) ⇒ Object



56
57
58
# File 'lib/menu.rb', line 56

def call(*args)
  show()
end