Class: Menuizer::Menu
- Inherits:
-
Object
- Object
- Menuizer::Menu
- Defined in:
- lib/menuizer/menu.rb
Defined Under Namespace
Classes: Item
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
- #activate(key) ⇒ Object
- #active_item ⇒ Object
- #active_items ⇒ Object
-
#initialize(namespace, config, data) ⇒ Menu
constructor
A new instance of Menu.
- #item(key) ⇒ Object
- #items ⇒ Object
Constructor Details
#initialize(namespace, config, data) ⇒ Menu
Returns a new instance of Menu.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/menuizer/menu.rb', line 4 def initialize(namespace,config,data) @config = config @data = data @parent = nil @namespace = namespace if @namespace @namespace = "#{namespace}_" item_class = :"Item_#{namespace}" else item_class = :ItemDefault end if self.class.const_defined?(item_class) @item_class = self.class.const_get(item_class) else @item_class = self.class.const_set(item_class, Class.new(Item)) if converter = @config.converter @item_class.instance_eval do converter.each do |key,block| define_method key do block.call @opts[key], @opts end end end end end if path = @config.file_path require "yaml" load_data YAML.load_file(path) end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
2 3 4 |
# File 'lib/menuizer/menu.rb', line 2 def data @data end |
Instance Method Details
#activate(key) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/menuizer/menu.rb', line 39 def activate(key) active_items.each do |item| item.is_active = false end @active_item = item = map[key] while item item.is_active = true item = item.parent end end |
#active_item ⇒ Object
50 51 52 |
# File 'lib/menuizer/menu.rb', line 50 def active_item @active_item ||= nil end |
#active_items ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/menuizer/menu.rb', line 53 def active_items result = [] item = active_item while item result << item item = item.parent end result.reverse end |
#item(key) ⇒ Object
63 64 65 |
# File 'lib/menuizer/menu.rb', line 63 def item(key) map[key] end |
#items ⇒ Object
67 68 69 |
# File 'lib/menuizer/menu.rb', line 67 def items @items ||= [] end |