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) ⇒ Menu

Returns a new instance of Menu.



2
3
4
# File 'lib/menuizer/menu.rb', line 2

def initialize(namespace)
  @namespace = namespace ? "#{namespace}_" : nil
end

Instance Method Details

#activate(key) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/menuizer/menu.rb', line 6

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



17
18
19
# File 'lib/menuizer/menu.rb', line 17

def active_item
  @active_item
end

#active_itemsObject



20
21
22
23
24
25
26
27
28
# File 'lib/menuizer/menu.rb', line 20

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

#header(title) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/menuizer/menu.rb', line 31

def header(title)
  current << Item.new({
    type: :header,
  },{
    namespace: @namespace,
    title: title,
  })
end

#item(title, path: nil, **opts) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/menuizer/menu.rb', line 39

def item(title, path: nil, **opts)
  unless block_given?
    item = Item.new({
      type: :item,
      parent: @parent,
      **opts,
    },{
      namespace: @namespace,
      title: title,
      path: path,
    })
    map[title] = item
    current << item
  else
    owner = @parent
    parents = @current
    item = @parent = Item.new({
      type: :tree,
      children: [],
      parent: owner,
      **opts,
    },{
      namespace: @namespace,
      title: title,
    })
    @current = item.children
    yield
    children, @current, @parent = @current, parents, owner
    current << item
  end
end

#itemsObject



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

def items
  @items ||= []
end