Class: SimpleNavigation::ItemAdapter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/simple_navigation/core/item_adapter.rb

Overview

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 respond to items 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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item) ⇒ ItemAdapter



28
29
30
# File 'lib/simple_navigation/core/item_adapter.rb', line 28

def initialize(item)
  @item = item.instance_of?(Hash) ? to_object(item) : item
end

Instance Attribute Details

#itemObject (readonly)

Returns the value of attribute item



26
27
28
# File 'lib/simple_navigation/core/item_adapter.rb', line 26

def item
  @item
end

Instance Method Details

#itemsObject

Returns the items (subnavigation) for this item if it responds to :items and the items-collection is not empty. Returns nil otherwise.



38
39
40
# File 'lib/simple_navigation/core/item_adapter.rb', line 38

def items
  (@item.respond_to?(:items) && !(@item.items.nil? || @item.items.empty?)) ? @item.items : nil
end

#optionsObject

Returns the options for this item. If the wrapped item does not implement an options method, an empty hash is returned.



33
34
35
# File 'lib/simple_navigation/core/item_adapter.rb', line 33

def options
  @item.respond_to?(:options) ? @item.options : {}
end

#to_simple_navigation_item(item_container) ⇒ Object

Converts this Item into a SimpleNavigation::Item



43
44
45
# File 'lib/simple_navigation/core/item_adapter.rb', line 43

def to_simple_navigation_item(item_container)
  SimpleNavigation::Item.new(item_container, key, name, url, options, items)
end