Class: Menuizer::Menu

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

Defined Under Namespace

Classes: Item

Instance Method Summary collapse

Constructor Details

#initialize(namespace, config) ⇒ Menu

Returns a new instance of Menu.



2
3
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
# File 'lib/menuizer/menu.rb', line 2

def initialize(namespace,config)
  @config = config
  @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 Method Details

#activate(key) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/menuizer/menu.rb', line 40

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_itemObject



51
52
53
# File 'lib/menuizer/menu.rb', line 51

def active_item
  @active_item ||= nil
end

#active_itemsObject



54
55
56
57
58
59
60
61
62
# File 'lib/menuizer/menu.rb', line 54

def active_items
  result = []
  item = active_item
  while item
    result << item
    item = item.parent
  end
  result.reverse
end

#dataObject



36
37
38
# File 'lib/menuizer/menu.rb', line 36

def data
  @data ||= {}
end

#item(key) ⇒ Object



64
65
66
# File 'lib/menuizer/menu.rb', line 64

def item(key)
  map[key]
end

#itemsObject



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

def items
  @items ||= []
end