Class: Navtastic::Menu
- Inherits:
-
Object
- Object
- Navtastic::Menu
- Includes:
- Enumerable
- Defined in:
- lib/navtastic/menu.rb
Overview
Stores items generated by a definition block
Defined Under Namespace
Classes: Configuration
Instance Attribute Summary collapse
-
#config ⇒ Menu::Configuration
readonly
The configuration for this menu.
-
#items ⇒ Array<Item>
readonly
The items in this menu.
-
#parent ⇒ Menu?
readonly
This parent of this menu.
Instance Method Summary collapse
-
#[](url) ⇒ Item?
Find an item in this menu matching the url.
-
#base_url ⇒ String
The base url of this menu, including all parent base urls.
-
#current_item ⇒ Item?
The current active item.
-
#depth ⇒ Integer
The depth of this menu.
- #each ⇒ Object
-
#initialize(parent = nil) ⇒ Menu
constructor
Create a new empty menu.
-
#item(name, url = nil, options = {}) {|submenu| ... } ⇒ Object
Add a new item at the end of the menu.
- #root? ⇒ true, false
Constructor Details
#initialize(parent = nil) ⇒ Menu
Create a new empty menu
28 29 30 31 32 33 34 |
# File 'lib/navtastic/menu.rb', line 28 def initialize(parent = nil) @parent = parent @config = Menu::Configuration.new @current_item = nil @items = [] end |
Instance Attribute Details
#config ⇒ Menu::Configuration (readonly)
Returns the configuration for this menu.
23 24 25 |
# File 'lib/navtastic/menu.rb', line 23 def config @config end |
#items ⇒ Array<Item> (readonly)
Returns the items in this menu.
17 18 19 |
# File 'lib/navtastic/menu.rb', line 17 def items @items end |
#parent ⇒ Menu? (readonly)
Returns this parent of this menu.
20 21 22 |
# File 'lib/navtastic/menu.rb', line 20 def parent @parent end |
Instance Method Details
#[](url) ⇒ Item?
Find an item in this menu matching the url
99 100 101 |
# File 'lib/navtastic/menu.rb', line 99 def [](url) items_by_url[url] end |
#base_url ⇒ String
The base url of this menu, including all parent base urls
135 136 137 138 139 140 141 |
# File 'lib/navtastic/menu.rb', line 135 def base_url base_url = config.base_url.to_s base_url.prepend Navtastic.configuration.base_url.to_s if root? base_url.prepend @parent.base_url.to_s unless root? base_url = nil if base_url.empty? base_url end |
#current_item ⇒ Item?
Returns the current active item.
124 125 126 127 128 129 130 |
# File 'lib/navtastic/menu.rb', line 124 def current_item if root? @current_item else @parent.current_item end end |
#depth ⇒ Integer
The depth of this menu
The root menu always has depth 0.
47 48 49 50 51 52 53 |
# File 'lib/navtastic/menu.rb', line 47 def depth if @parent @parent.depth + 1 else 0 end end |
#each ⇒ Object
87 88 89 90 91 |
# File 'lib/navtastic/menu.rb', line 87 def each @items.each do |item| yield item end end |
#item(name, url = nil, options = {}) {|submenu| ... } ⇒ Object
Add a new item at the end of the menu
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/navtastic/menu.rb', line 63 def item(name, url = nil, = {}) # If only options were given and no url, move options to the right place if url.is_a?(Hash) && .empty? = url url = nil end item = Item.new(self, name, url, ) if block_given? = Menu.new(self) .config.base_url = url if [:base_url] && url yield item. = end @items << item register_item(item) item end |
#root? ⇒ true, false
38 39 40 |
# File 'lib/navtastic/menu.rb', line 38 def root? @parent.nil? end |