class ActiveMenu::Node

Attributes

children[RW]
id[RW]
options[RW]
parent[RW]

Public Class Methods

new(id, options={}) { |self| ... } click to toggle source
# File lib/active_menu/node.rb, line 5
def initialize(id, options={}, &block)
  @id = id.to_sym
  @parent = nil
  @children = []
  @options = options
  yield(self) if block_given?
end

Public Instance Methods

child(id, options={}) { |new_child| ... } click to toggle source
# File lib/active_menu/node.rb, line 22
def child(id, options={}, &block)
  new_child = self.class.new(id, options)
  new_child.parent = self
  @children << new_child
  yield(new_child) if block_given?
  new_child
end
exists?(id) click to toggle source
# File lib/active_menu/node.rb, line 42
def exists?(id)
  id = id.to_sym
  if self.get(id)
    true
  else
    false
  end
end
get(id) { |first_element| ... } click to toggle source
# File lib/active_menu/node.rb, line 30
def get(id, &block)
  id = id.to_sym
  selected = @children.select {|m| m.id == id}
  if selected.length >= 1
    first_element = selected.first
    yield(first_element) if block_given?
    first_element
  else
    false
  end
end
leave_children!() click to toggle source
# File lib/active_menu/node.rb, line 51
def leave_children!
  @children = []
end
method_missing(method_name, *args) click to toggle source
# File lib/active_menu/node.rb, line 64
def method_missing(method_name, *args)
  unless respond_to?(method_name)
    option_name = method_name.to_s.gsub("=", "").to_sym
    match_eq = method_name.to_s.match(/^(\w)=/)
    self.class.class_eval do
      define_method method_name do |value=nil|
        option(option_name, value)
      end
    end
    send(method_name, *args)
  else
    send(method_name, *args)
  end
end
option(key, value=nil) click to toggle source
# File lib/active_menu/node.rb, line 14
def option(key, value=nil)
  if value.nil?
    @options[key]
  else
    @options[key] = value
  end
end