class SimpleNavigation::ItemAdapter
This class acts as an adapter to items that are not defined using the DSL in the config/navigation.rb, but directly provided inside the application. When defining the items that way, every item you provide needs to define the following methods:
-
key
-
name
-
url
and optionally
-
options
-
items
- if one of your items has a subnavigation it must respondto <tt>items</tt> providing the subnavigation.
You can also specify your items as a list of hashes. The hashes will be converted to objects automatically. The hashes representing the items obviously must have the keys :key, :name and :url and optionally the keys :options and :items.
See SimpleNavigation::ItemContainer#item for the purpose of these methods.
Attributes
Public Class Methods
# File lib/simple_navigation/core/item_adapter.rb, line 32 def initialize(item) @item = item.is_a?(Hash) ? to_object(item) : item end
Public Instance Methods
Returns the items (subnavigation) for this item if it responds to :items and the items-collection is not empty. Returns nil otherwise.
# File lib/simple_navigation/core/item_adapter.rb, line 44 def items item.items if item.respond_to?(:items) && item.items && item.items.any? end
Returns the options for this item. If the wrapped item does not implement an options method, an empty hash is returned.
# File lib/simple_navigation/core/item_adapter.rb, line 38 def options item.respond_to?(:options) ? item.options : {} end
Protected Instance Methods
Converts the specified hash into an object. Each key will be added as method.
# File lib/simple_navigation/core/item_adapter.rb, line 57 def to_object(hash) mod = Module.new do hash.each_pair do |key, value| define_method key do value end end end Object.new.extend(mod) end