Class: Alacarte::Menu

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, the group 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 = options[:name] || args[0]
  @path = options[: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 = options[:label] || I18n.t("alacarte.menus.#{@translation_key}", :default => @translation_key.to_s)
  @as = options[:as] || @name
  @blocks = []
  @blocks << block if block_given?
  @html_options = options[:html]
  @group_options = options[:group]
  @wrapper_options = 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

#asObject (readonly)

Returns the value of attribute as.



9
10
11
# File 'lib/alacarte/menu.rb', line 9

def as
  @as
end

#blocksObject (readonly)

Returns the value of attribute blocks.



9
10
11
# File 'lib/alacarte/menu.rb', line 9

def blocks
  @blocks
end

#deep_nameObject (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_optionsObject (readonly)

Returns the value of attribute group_options.



9
10
11
# File 'lib/alacarte/menu.rb', line 9

def group_options
  @group_options
end

#html_optionsObject (readonly)

Returns the value of attribute html_options.



9
10
11
# File 'lib/alacarte/menu.rb', line 9

def html_options
  @html_options
end

#itemsObject (readonly)

Returns the value of attribute items.



9
10
11
# File 'lib/alacarte/menu.rb', line 9

def items
  @items
end

#labelObject (readonly)

Returns the value of attribute label.



9
10
11
# File 'lib/alacarte/menu.rb', line 9

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/alacarte/menu.rb', line 9

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/alacarte/menu.rb', line 9

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



9
10
11
# File 'lib/alacarte/menu.rb', line 9

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/alacarte/menu.rb', line 9

def path
  @path
end

#translation_keyObject (readonly)

Returns the value of attribute translation_key.



9
10
11
# File 'lib/alacarte/menu.rb', line 9

def translation_key
  @translation_key
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/alacarte/menu.rb', line 9

def type
  @type
end

#wrapper_optionsObject (readonly)

Returns the value of attribute wrapper_options.



9
10
11
# File 'lib/alacarte/menu.rb', line 9

def wrapper_options
  @wrapper_options
end

Class Method Details

.envObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/alacarte/menu.rb', line 83

def valid?
  if options[:if] && options[:if].respond_to?(:call)
    return options[:if].call
  end

  if options[:unless] && options[:unless].respond_to?(:call)
    return !options[:unless].call
  end
  
  return true
end