Class: Kin::Nav::Menu

Inherits:
Object
  • Object
show all
Defined in:
lib/kin/nav.rb

Overview

Represents a navigation menu - contains one or more items which are links to other parts of the application.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, formatter = nil) ⇒ Menu

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new menu. Don’t use Menu.new, use Nav.setup.

Parameters:



98
99
100
101
102
103
104
105
# File 'lib/kin/nav.rb', line 98

def initialize(name, formatter = nil)
  @name  = name.to_sym
  @formatter = formatter
  @items = []

  # Used to find the active item on a given page.
  @matchers = Struct.new(:best, :controller, :generic).new([], [], [])
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



86
87
88
# File 'lib/kin/nav.rb', line 86

def items
  @items
end

#nameObject (readonly)

Returns the value of attribute name.



86
87
88
# File 'lib/kin/nav.rb', line 86

def name
  @name
end

Instance Method Details

#active_item(controller) ⇒ Symbol

Returns the active item based on the given controller.

Parameters:

  • controller (#controller_name, #action_name)

    An object which behaves like a controller.

Returns:

  • (Symbol)


155
156
157
158
159
160
161
162
163
164
# File 'lib/kin/nav.rb', line 155

def active_item(controller)
  match = lambda { |matcher| matcher.matches?(controller) }

  found =
    @matchers.best.detect(&match)       ||
    @matchers.controller.detect(&match) ||
    @matchers.generic.detect(&match)

  found.item.id if found
end

#add_active_match(name, item) ⇒ Object

Adds a controller name / action name pair for matching active items.

Parameters:

Returns:

  • Kin::Nav::ItemMatcher



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/kin/nav.rb', line 130

def add_active_match(name, item)
  name = name.split('/')
  matcher = ItemMatcher.new(item, name[0..-2].join('/'), name.last)

  if matcher.action?
    @matchers.best << matcher
  elsif matcher.controller?
    @matchers.controller << matcher
  else
    @matchers.generic << matcher
  end

  matcher
end

#formatterKin::Nav::Formatters::Basic

Returns the default formatter to be used when rendering this menu.



114
115
116
# File 'lib/kin/nav.rb', line 114

def formatter
  @formatter || Merb::Plugins.config[:kin][:nav_formatter]
end