Class: ActiveMenu::Node
- Inherits:
-
Object
- Object
- ActiveMenu::Node
- Defined in:
- lib/active_menu/node.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#id ⇒ Object
Returns the value of attribute id.
-
#options ⇒ Object
Returns the value of attribute options.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #child(id, options = {}) {|new_child| ... } ⇒ Object
- #exists?(id) ⇒ Boolean
- #get(id, &block) ⇒ Object
-
#initialize(id, options = {}) {|_self| ... } ⇒ Node
constructor
A new instance of Node.
- #leave_children! ⇒ Object
- #method_missing(method_name, *args) ⇒ Object
- #option(key, value = nil) ⇒ Object
Constructor Details
#initialize(id, options = {}) {|_self| ... } ⇒ Node
Returns a new instance of Node.
5 6 7 8 9 10 11 |
# File 'lib/active_menu/node.rb', line 5 def initialize(id, ={}, &block) @id = id.to_sym @parent = nil @children = [] @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
#children ⇒ Object
Returns the value of attribute children.
3 4 5 |
# File 'lib/active_menu/node.rb', line 3 def children @children end |
#id ⇒ Object
Returns the value of attribute id.
3 4 5 |
# File 'lib/active_menu/node.rb', line 3 def id @id end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/active_menu/node.rb', line 3 def @options end |
#parent ⇒ Object
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
22 23 24 25 26 27 28 |
# File 'lib/active_menu/node.rb', line 22 def child(id, ={}, &block) new_child = self.class.new(id, ) new_child.parent = self @children << new_child yield(new_child) if block_given? new_child end |
#exists?(id) ⇒ 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 |