Class: Alacarte::Menu
- Inherits:
-
Object
- Object
- Alacarte::Menu
- Defined in:
- lib/alacarte/menu.rb
Overview
Alacarte::Menu is the base class for defining menu items.
Constant Summary collapse
- VALID_ELEMENTS =
[:link, :span, :separator, :title, :subtitle, :line, :text]
- @@env =
nil
Instance Attribute Summary collapse
-
#as ⇒ Object
readonly
Returns the value of attribute as.
-
#blocks ⇒ Object
readonly
Returns the value of attribute blocks.
-
#deep_name ⇒ Object
readonly
Returns the value of attribute deep_name.
-
#group_options ⇒ Object
readonly
Returns the value of attribute group_options.
-
#html_options ⇒ Object
readonly
Returns the value of attribute html_options.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#translation_key ⇒ Object
readonly
Returns the value of attribute translation_key.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#wrapper_options ⇒ Object
readonly
Returns the value of attribute wrapper_options.
Class Method Summary collapse
-
.env ⇒ Object
Returns the environment that was set to the Alacarte::Menu.
-
.env? ⇒ Boolean
Tests if an environment was set to the Alacarte::Menu.
-
.reset_env! ⇒ Object
Resets the env, used in rspec testing.
Instance Method Summary collapse
-
#block? ⇒ Boolean
Tests if a block was passed to the Alacarte::Menu object.
-
#build(env = nil) ⇒ Object
Builds the menu, based on the environment that is passed.
- #extend(&block) ⇒ Object
-
#initialize(parent, type, *args, &block) ⇒ Menu
constructor
Creates a menu item.
-
#method_missing(id, *args, &block) ⇒ Object
Try to send the menu to the passed
env
, then try to create a menu item of the missing method. -
#valid? ⇒ Boolean
Tests to see if the current menu item is valid in the current setting.
Constructor Details
#initialize(parent, type, *args, &block) ⇒ Menu
Creates a menu item. Menu items can relate to a parent
menu item and a type should be passed. The type can be used for rendering (current types are :menu
, :link
and :span
).
The first two attributes that can be passed (not required) are the name
and path
of the menu item. These can also be set in the options hash. Other settings are derived from name
, but can also be passed in the options hash.
The options you can pass are:
-
:name
sets the name of the menu item -
:path
sets the path that the menu item should link to -
:label
sets the label that should be displayed as the menu item -
:as
defines how the element can be matched with the current element -
:if
set a conditions when the menu item should be valid -
:unless
set an inverse conditions when the menu item should be valid -
:html
pass any html options that you want to be added to the menu item -
:group
when the menu item has valid children, thegroup
options are passed as the html options of the grouping element -
:wrapper
pass any html options that you want to add to the wrapping li item
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/alacarte/menu.rb', line 42 def initialize(parent, type, *args, &block) @options = args.last.is_a?(::Hash) ? args.pop : {} @parent = parent @type = type @name = [:name] || args[0] @path = [:path] || args[1] @deep_name = @parent ? "#{@parent.deep_name}.#{@name.to_s}" : @name.to_s @translation_key = (block_given? && @type != :menu) ? "#{deep_name}.root" : "#{deep_name}" @label = [:label] || I18n.t("alacarte.menus.#{@translation_key}", :default => @translation_key.to_s) @as = [:as] || @name @blocks = [] @blocks << block if block_given? @html_options = [:html] @group_options = [:group] @wrapper_options = [:wrapper] build end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *args, &block) ⇒ Object
Try to send the menu to the passed env
, then try to create a menu item of the missing method.
96 97 98 99 100 101 102 103 104 |
# File 'lib/alacarte/menu.rb', line 96 def method_missing(id, *args, &block) if Menu.env? && Menu.env.respond_to?(id) @@env.send(id, *args, &block) elsif VALID_ELEMENTS.member?(id) @items << Menu.new(self, id, *args, &block) else super end end |
Instance Attribute Details
#as ⇒ Object (readonly)
Returns the value of attribute as.
9 10 11 |
# File 'lib/alacarte/menu.rb', line 9 def as @as end |
#blocks ⇒ Object (readonly)
Returns the value of attribute blocks.
9 10 11 |
# File 'lib/alacarte/menu.rb', line 9 def blocks @blocks end |
#deep_name ⇒ Object (readonly)
Returns the value of attribute deep_name.
9 10 11 |
# File 'lib/alacarte/menu.rb', line 9 def deep_name @deep_name end |
#group_options ⇒ Object (readonly)
Returns the value of attribute group_options.
9 10 11 |
# File 'lib/alacarte/menu.rb', line 9 def @group_options end |
#html_options ⇒ Object (readonly)
Returns the value of attribute html_options.
9 10 11 |
# File 'lib/alacarte/menu.rb', line 9 def @html_options end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
9 10 11 |
# File 'lib/alacarte/menu.rb', line 9 def items @items end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
9 10 11 |
# File 'lib/alacarte/menu.rb', line 9 def label @label end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/alacarte/menu.rb', line 9 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/alacarte/menu.rb', line 9 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
9 10 11 |
# File 'lib/alacarte/menu.rb', line 9 def parent @parent end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/alacarte/menu.rb', line 9 def path @path end |
#translation_key ⇒ Object (readonly)
Returns the value of attribute translation_key.
9 10 11 |
# File 'lib/alacarte/menu.rb', line 9 def translation_key @translation_key end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
9 10 11 |
# File 'lib/alacarte/menu.rb', line 9 def type @type end |
#wrapper_options ⇒ Object (readonly)
Returns the value of attribute wrapper_options.
9 10 11 |
# File 'lib/alacarte/menu.rb', line 9 def @wrapper_options end |
Class Method Details
.env ⇒ Object
Returns the environment that was set to the Alacarte::Menu
17 18 19 |
# File 'lib/alacarte/menu.rb', line 17 def self.env @@env end |
.env? ⇒ Boolean
Tests if an environment was set to the Alacarte::Menu
12 13 14 |
# File 'lib/alacarte/menu.rb', line 12 def self.env? !!@@env end |
.reset_env! ⇒ Object
Resets the env, used in rspec testing
22 23 24 |
# File 'lib/alacarte/menu.rb', line 22 def self.reset_env! @@env = nil end |
Instance Method Details
#block? ⇒ Boolean
Tests if a block was passed to the Alacarte::Menu object
66 67 68 |
# File 'lib/alacarte/menu.rb', line 66 def block? (@blocks.size > 0) end |
#build(env = nil) ⇒ Object
Builds the menu, based on the environment that is passed.
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/alacarte/menu.rb', line 71 def build(env = nil) @@env = env if env @items = [] if Menu.env? && self.block? @blocks.each do |block| self.instance_eval(&block) end end end |
#extend(&block) ⇒ Object
61 62 63 |
# File 'lib/alacarte/menu.rb', line 61 def extend(&block) @blocks << block if block_given? end |
#valid? ⇒ Boolean
Tests to see if the current menu item is valid in the current setting
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/alacarte/menu.rb', line 83 def valid? if [:if] && [:if].respond_to?(:call) return [:if].call end if [:unless] && [:unless].respond_to?(:call) return ![:unless].call end return true end |