Class: Navtastic::Menu

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/navtastic/menu.rb

Overview

Stores items generated by a definition block

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMenu

Create a new empty menu


13
14
15
16
17
# File 'lib/navtastic/menu.rb', line 13

def initialize
  @items = []
  @items_by_url = {}
  @current_item = nil
end

Instance Attribute Details

#current_itemItem? (readonly)


10
11
12
# File 'lib/navtastic/menu.rb', line 10

def current_item
  @current_item
end

#itemsArray<Item> (readonly)


7
8
9
# File 'lib/navtastic/menu.rb', line 7

def items
  @items
end

Instance Method Details

#[](url) ⇒ Item?

Find an item in this menu matching the url


51
52
53
# File 'lib/navtastic/menu.rb', line 51

def [](url)
  @items_by_url[url]
end

#each {|item| ... } ⇒ Object

Loop over every item in this menu

Yields:

Yield Parameters:


39
40
41
42
43
# File 'lib/navtastic/menu.rb', line 39

def each
  @items.each do |item|
    yield item
  end
end

#item(name, url = nil) ⇒ Object

Add a new item at the end of the menu


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/navtastic/menu.rb', line 23

def item(name, url = nil)
  item = Item.new(self, name, url)

  @items << item
  @items_by_url[item.url] = item if item.url

  # The first item with a url is the default current item
  @current_item = item if @current_item.nil? && item.url

  item
end