Class: Menu

Inherits:
Object
  • Object
show all
Includes:
BasicLogging
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

@@global_elements =
[]
@@hidden_elements =
[]

Constants included from BasicLogging

BasicLogging::DEBUG, BasicLogging::ERROR, BasicLogging::FATAL, BasicLogging::INFO, BasicLogging::Levels, BasicLogging::UNKNOWN, BasicLogging::WARN

Instance Attribute Summary collapse

Attributes included from BasicLogging

#log_level, #target

Instance Method Summary collapse

Methods included from BasicLogging

is_muted?, #log, mute, #set_level, set_level, set_target, #set_target

Constructor Details

#initialize(options = {}) ⇒ Menu

Returns a new instance of Menu.



36
37
38
39
40
41
42
43
# File 'lib/menu.rb', line 36

def initialize(options = {})
  @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.



87
88
89
# File 'lib/menu.rb', line 87

def key
  @key
end

#nameObject

Returns the value of attribute name.



87
88
89
# File 'lib/menu.rb', line 87

def name
  @name
end

Instance Method Details

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/menu.rb', line 49

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



45
46
47
# File 'lib/menu.rb', line 45

def call(*args)
  show()
end