Class: Navtastic::Menu

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Menu

Create a new empty menu

Parameters:

  • parent (Menu) (defaults to: nil)

    the parent menu of this is submenu



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

#configMenu::Configuration (readonly)

Returns the configuration for this menu.

Returns:



23
24
25
# File 'lib/navtastic/menu.rb', line 23

def config
  @config
end

#itemsArray<Item> (readonly)

Returns the items in this menu.

Returns:

  • (Array<Item>)

    the items in this menu



17
18
19
# File 'lib/navtastic/menu.rb', line 17

def items
  @items
end

#parentMenu? (readonly)

Returns this parent of this menu.

Returns:

  • (Menu, nil)

    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

Parameters:

  • url (String)

    the url of the item

Returns:

  • (Item)

    if an item with that url exists

  • (nil)

    if the item doens't exist



99
100
101
# File 'lib/navtastic/menu.rb', line 99

def [](url)
  items_by_url[url]
end

#base_urlString

The base url of this menu, including all parent base urls

Returns:

  • (String)


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_itemItem?

Returns the current active item.

Returns:

  • (Item, nil)

    the current active item

See Also:



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

#depthInteger

The depth of this menu

The root menu always has depth 0.

Returns:

  • (Integer)

    the depth of this menu



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

#eachObject



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

Parameters:

  • name (String)

    the name to display in the menu

  • url (String) (defaults to: nil)

    the url to link to, if the item is a link

  • options (Hash) (defaults to: {})

    extra confiration options

Yields:

  • (submenu)

    block to generate a sub menu

Yield Parameters:

  • submenu (Menu)

    the menu to be initialized



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, options = {})
  # If only options were given and no url, move options to the right place
  if url.is_a?(Hash) && options.empty?
    options = url
    url = nil
  end

  item = Item.new(self, name, url, options)

  if block_given?
    submenu = Menu.new(self)

    submenu.config.base_url = url if options[:base_url] && url

    yield submenu
    item.submenu = submenu
  end

  @items << item
  register_item(item)

  item
end

#root?true, false

Returns:

  • (true)

    if this menu is the root menu

  • (false)

    if this menu is a submenu



38
39
40
# File 'lib/navtastic/menu.rb', line 38

def root?
  @parent.nil?
end