Class: ActiveMenu::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/active_menu/node.rb

Direct Known Subclasses

Menu

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, options = {}) {|_self| ... } ⇒ Node

Returns a new instance of Node.

Yields:

  • (_self)

Yield Parameters:



5
6
7
8
9
10
11
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



3
4
5
# File 'lib/active_menu/node.rb', line 3

def children
  @children
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/active_menu/node.rb', line 3

def id
  @id
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/active_menu/node.rb', line 3

def options
  @options
end

#parentObject

Returns the value of attribute parent.



3
4
5
# File 'lib/active_menu/node.rb', line 3

def parent
  @parent
end

Instance Method Details

#child(id, options = {}) {|new_child| ... } ⇒ Object

Yields:

  • (new_child)


22
23
24
25
26
27
28
# 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) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
# 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, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# 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!Object



51
52
53
# File 'lib/active_menu/node.rb', line 51

def leave_children!
  @children = []
end

#option(key, value = nil) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/active_menu/node.rb', line 14

def option(key, value=nil)
  if value.nil?
    @options[key]
  else
    @options[key] = value
  end
end