class Menu

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

Attributes

key[RW]
name[RW]

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

add(element, pos = nil) click to toggle source
# 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
Also aliased as: add_action, add_sub_menu, add_menu
add_action(element, pos = nil)
Alias for: add
add_menu(element, pos = nil)
Alias for: add
add_sub_menu(element, pos = nil)
Alias for: add
call(*args) click to toggle source
# File lib/menu.rb, line 56
def call(*args)
  show()
end

Private Instance Methods

show() click to toggle source
# File lib/menu.rb, line 82
  def show()
    @elements.each do |ele|
      name = ele.name.downcase
      key = ele.key.downcase                    
      print name.dup << " (#{key}) * " if !@@hidden_elements.include?(ele)
    end
    puts
    ele = nil
    until ele
      key = "%c" %wait_for_user 
=begin
      if "\e" == key
        break
      end
=end
      ele = @elements.detect{ |e| key == e.key  }
      ele ||= @@global_elements.detect{ |e| key == e.key  }
    end
    ele.call if ele
  end