Class: ActionNav::Base
- Inherits:
-
Object
- Object
- ActionNav::Base
- Defined in:
- lib/action_nav/base.rb
Instance Attribute Summary collapse
-
#active_paths ⇒ Object
readonly
Returns the value of attribute active_paths.
-
#context ⇒ Hash
readonly
The context for this navigation.
-
#controller ⇒ ActionController::Base
readonly
The controller that initialized this navigation.
Class Method Summary collapse
-
.item(id, &block) ⇒ ActionNav::Item
Add a new item to this navigation.
-
.items ⇒ Hash
Return all items for this navigation.
Instance Method Summary collapse
-
#activate(*parts) ⇒ Object
(also: #active)
Add an active navigation by passing the full path to active item.
-
#active_path?(*parts) ⇒ Boolean
Is the given active path?.
-
#add_context(key, value) ⇒ Hash
Add context to this navigation.
-
#initialize(controller) ⇒ ActionNav::Base
constructor
Initialize a new navigation.
-
#items ⇒ Array<ActionNav::ItemInstance>
Return a full list of items for this instance as instances.
Constructor Details
#initialize(controller) ⇒ ActionNav::Base
Initialize a new navigation
12 13 14 15 16 |
# File 'lib/action_nav/base.rb', line 12 def initialize(controller) @controller = controller @active_paths = [] @context = {} end |
Instance Attribute Details
#active_paths ⇒ Object (readonly)
Returns the value of attribute active_paths.
22 23 24 |
# File 'lib/action_nav/base.rb', line 22 def active_paths @active_paths end |
#context ⇒ Hash (readonly)
The context for this navigation.
27 28 29 |
# File 'lib/action_nav/base.rb', line 27 def context @context end |
#controller ⇒ ActionController::Base (readonly)
The controller that initialized this navigation.
21 22 23 |
# File 'lib/action_nav/base.rb', line 21 def controller @controller end |
Class Method Details
.item(id, &block) ⇒ ActionNav::Item
Add a new item to this navigation
72 73 74 75 76 |
# File 'lib/action_nav/base.rb', line 72 def self.item(id, &block) item = Item.new(nil, id) item.dsl(&block) if block_given? items[id] = item end |
.items ⇒ Hash
Return all items for this navigation
81 82 83 |
# File 'lib/action_nav/base.rb', line 81 def self.items @items ||= {} end |
Instance Method Details
#activate(*parts) ⇒ Object Also known as: active
Add an active navigation by passing the full path to active item.
44 45 46 |
# File 'lib/action_nav/base.rb', line 44 def activate(*parts) @active_paths.push(parts) end |
#active_path?(*parts) ⇒ Boolean
Is the given active path?
52 53 54 55 56 57 |
# File 'lib/action_nav/base.rb', line 52 def active_path?(*parts) @active_paths.any? do |path| a = path.size.times.map { |i| path[0, path.size - i] } a.include?(parts) end end |
#add_context(key, value) ⇒ Hash
Add context to this navigation.
64 65 66 |
# File 'lib/action_nav/base.rb', line 64 def add_context(key, value) @context[key] = value end |
#items ⇒ Array<ActionNav::ItemInstance>
Return a full list of items for this instance as instances.
33 34 35 36 37 38 39 40 |
# File 'lib/action_nav/base.rb', line 33 def items @items ||= self.class.items.each_with_object([]) do |(key, item), array| instance = ItemInstance.new(self, item) unless instance.hidden? array << instance end end end |