Class: Goldberg::Menu
- Defined in:
- lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu.rb
Defined Under Namespace
Classes: Node
Instance Attribute Summary collapse
-
#root ⇒ Object
Returns the value of attribute root.
-
#selected ⇒ Object
Returns the name of the currently-selected item or nil if no item is selected.
Instance Method Summary collapse
- #crumbs ⇒ Object
- #get_item(item_id) ⇒ Object
-
#get_menu(level) ⇒ Object
Returns the array of items at the given level.
-
#initialize(role = nil) ⇒ Menu
constructor
A new instance of Menu.
-
#select(name) ⇒ Object
Selects the menu item for the given name, if it exists in this menu.
-
#selected?(menu_id) ⇒ Boolean
Returns true if the specified item is selected; false if otherwise.
Constructor Details
#initialize(role = nil) ⇒ Menu
Returns a new instance of Menu.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu.rb', line 82 def initialize(role = nil) @root = Node.new @by_id = Hash.new @by_name = Hash.new @selected = Hash.new @vector = Array.new @crumbs = Array.new items = nil if role if role.cache[:credentials]..size > 0 items = MenuItem.(role.cache[:credentials].) else # role has no permissions # (items will remain as nil: the only node will be the root) end else # No role given: build menu of everything items = MenuItem. end if items if items.size > 0 # Build hashes of items by name and id for item in items do node = Node.new node.setup(item) @by_id[item.id] = node @by_name[item.name] = node end # Then build tree of items for item in items do node = @by_id[item.id] p_id = node.parent_id if p_id if @by_id.has_key?(p_id) @by_id[p_id].add_child(node) end else @root.add_child(node) end end end # if items.size > 0 self.select(nil) end # if items end |
Instance Attribute Details
#root ⇒ Object
Returns the value of attribute root.
80 81 82 |
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu.rb', line 80 def root @root end |
#selected ⇒ Object
Returns the name of the currently-selected item or nil if no item is selected.
173 174 175 |
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu.rb', line 173 def selected @selected end |
Instance Method Details
#crumbs ⇒ Object
187 188 189 190 191 192 193 194 195 |
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu.rb', line 187 def crumbs crumbs = Array.new for crumb in @crumbs do item = get_item(crumb) crumbs << item end return crumbs end |
#get_item(item_id) ⇒ Object
157 158 159 |
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu.rb', line 157 def get_item(item_id) return @by_id[item_id] end |
#get_menu(level) ⇒ Object
Returns the array of items at the given level.
162 163 164 165 166 167 168 |
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu.rb', line 162 def (level) if @vector.length > level return @vector[level].children else return []#nil end end |
#select(name) ⇒ Object
Selects the menu item for the given name, if it exists in this menu. If not returns nil.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu.rb', line 135 def select(name) if name and @by_name.has_key?(name) node = @by_name[name] @selected = Hash.new @vector = Array.new @crumbs = Array.new while node and node.id @selected[node.id] = node @vector.unshift node @crumbs.unshift node.id node = @by_id[node.parent_id] end @vector.unshift @root return @by_name[name] else if @root.children and @root.children.length > 0 select(@by_id[@root.children[0]].name) end end end |
#selected?(menu_id) ⇒ Boolean
Returns true if the specified item is selected; false if otherwise.
183 184 185 |
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu.rb', line 183 def selected?() @selected.has_key?() ? true : false end |