Class: Menuizer::Menu

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

Defined Under Namespace

Classes: Item

Instance Attribute Summary collapse

Instance Method Summary collapse

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
38
39
40
41
42
# 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"
    if @config.cache
      yml = @config.yml ||= YAML.load_file(path)
    else
      yml = YAML.load_file(path)
    end
    load_data yml
  end
end

Instance Attribute Details

#dataObject (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



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

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



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

def active_item
  @active_item ||= nil
end

#active_itemsObject



58
59
60
61
62
63
64
65
66
# File 'lib/menuizer/menu.rb', line 58

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

#item(key) ⇒ Object



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

def item(key)
  map[key]
end

#itemsObject



72
73
74
# File 'lib/menuizer/menu.rb', line 72

def items
  @items ||= []
end