Class: Menuizer::Menu

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

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

def header(title)
  current << OpenStruct.new(
    type: :header,
    title: title,
  )
end

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



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

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

#itemsObject



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

def items
  @items ||= []
end