Class: Olelo::Menu
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Util
#check, #decode64, #deep_copy, #encode64, #escape, #escape_html, #escape_javascript, included, #md5, #no_cache?, #sha256, #titlecase, #truncate, #unescape, #unescape_backslash, #unescape_html, #valid_xml_chars?
Constructor Details
#initialize(name) ⇒ Menu
Returns a new instance of Menu.
7
8
9
10
|
# File 'lib/olelo/menu.rb', line 7
def initialize(name)
@name = name.to_sym
@items = {}
end
|
Instance Attribute Details
Returns the value of attribute name.
5
6
7
|
# File 'lib/olelo/menu.rb', line 5
def name
@name
end
|
Instance Method Details
35
36
37
38
39
40
|
# File 'lib/olelo/menu.rb', line 35
def <<(item)
raise TypeError, "Only items allowed" unless MenuItem === item
raise "Item #{item.name} exists already in #{path.join('/')}" if @items.include?(item.name)
item.parent = self
@items[item.name] = item
end
|
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/olelo/menu.rb', line 16
def [](name)
path = path.to_s
i = path.index('/')
if i
item = @items[path[0...i]]
item[path[i+1..-1]] if item
else
@items[name.to_sym]
end
end
|
#append(items) ⇒ Object
31
32
33
|
# File 'lib/olelo/menu.rb', line 31
def append(items)
items.each {|item| self << item }
end
|
61
62
63
|
# File 'lib/olelo/menu.rb', line 61
def
empty? ? '' : %{<ul id="menu-#{html_id}">#{map(&:build_menu).join}</ul>}
end
|
46
47
48
|
# File 'lib/olelo/menu.rb', line 46
def clear
@items.clear
end
|
#each(&block) ⇒ Object
12
13
14
|
# File 'lib/olelo/menu.rb', line 12
def each(&block)
@items.each_value(&block)
end
|
#empty? ⇒ Boolean
42
43
44
|
# File 'lib/olelo/menu.rb', line 42
def empty?
@items.empty?
end
|
69
70
71
|
# File 'lib/olelo/menu.rb', line 69
def html_id
escape_html path.join('-')
end
|
#item(name, options = {}) ⇒ Object
27
28
29
|
# File 'lib/olelo/menu.rb', line 27
def item(name, options = {})
self << MenuItem.new(name, options)
end
|
73
74
75
|
# File 'lib/olelo/menu.rb', line 73
def path
[name]
end
|
#remove(name) ⇒ Object
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/olelo/menu.rb', line 50
def remove(name)
path = name.to_s
i = path.index('/')
if i
item = @items[path[0...i]]
item.remove(path[i+1..-1]) if item
else
@items.delete(name.to_sym)
end
end
|
65
66
67
|
# File 'lib/olelo/menu.rb', line 65
def to_html
.html_safe
end
|