Sha256: 33a5d1ee59d3312da707b904868dd9b1bf078b1490d86c4bb20cea3d770763cd

Contents?: true

Size: 1.41 KB

Versions: 18

Compression:

Stored size: 1.41 KB

Contents

module SimpleNavigation
  
  # 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:
  #
  # * <tt>key</tt>
  # * <tt>name</tt>
  # * <tt>url</tt>
  #
  # and optionally
  # 
  # * <tt>options</tt>
  # * <tt>items</tt> - if one of your items has a subnavigation it must respond to <tt>items</tt> providing the subnavigation.
  #
  # See SimpleNavigation::ItemContainer#item for the purpose of these methods.
  class ItemAdapter
    delegate :key, :name, :url, :to => :item
    
    attr_reader :item

    def initialize(item)
      @item = item
    end
        
    # Returns the options for this item. If the wrapped item does not implement an options method, an empty hash is returned.
    def options
      @item.respond_to?(:options) ? @item.options : {}
    end
    
    # Returns the items (subnavigation) for this item if it responds to :items and the items-collection is not empty. Returns nil otherwise.
    def items
      (@item.respond_to?(:items) && !(@item.items.nil? || @item.items.empty?)) ? @item.items : nil
    end
    
    # Converts this Item into a SimpleNavigation::Item
    def to_simple_navigation_item(item_container)
      SimpleNavigation::Item.new(item_container, key, name, url, options, items)
    end
    
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
simple-navigation-2.7.3 lib/simple_navigation/item_adapter.rb
simple-navigation-2.7.2 lib/simple_navigation/item_adapter.rb
simple-navigation-2.7.1 lib/simple_navigation/item_adapter.rb
simple-navigation-2.7.0 lib/simple_navigation/item_adapter.rb
simple-navigation-2.6.0 lib/simple_navigation/item_adapter.rb
simple-navigation-2.5.4 lib/simple_navigation/item_adapter.rb
simple-navigation-2.5.3 lib/simple_navigation/item_adapter.rb
simple-navigation-2.5.2 lib/simple_navigation/item_adapter.rb
simple-navigation-2.5.1 lib/simple_navigation/item_adapter.rb
simple-navigation-2.5.0 lib/simple_navigation/item_adapter.rb
simple-navigation-2.4.2 lib/simple_navigation/item_adapter.rb
simple-navigation-2.4.1 lib/simple_navigation/item_adapter.rb
simple-navigation-2.4.0 lib/simple_navigation/item_adapter.rb
simple-navigation-2.2.3 lib/simple_navigation/item_adapter.rb
simple-navigation-2.2.2 lib/simple_navigation/item_adapter.rb
simple-navigation-2.2.1 lib/simple_navigation/item_adapter.rb
simple-navigation-2.2.0 lib/simple_navigation/item_adapter.rb
simple-navigation-2.1.0 lib/simple_navigation/item_adapter.rb