Class: Redwood::Node
Direct Known Subclasses
Constant Summary
Constants included from Redwood
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#<<(child) ⇒ Object
Graft a child.
-
#[](key) ⇒ Object
Lookup a child node by its name.
-
#add_child(name) {|child| ... } ⇒ Object
Creates a child, adds it to children, and returns the child.
-
#initialize(name = nil, parent = nil) ⇒ Node
constructor
A new instance of Node.
Methods included from Redwood
#ancestors, #children, #depth, #descendants, #graft, #has_children?, #height, #leaf?, #only_child?, #parent, #prune, #root, #root?, #siblings, #unlink, #view, #walk
Constructor Details
#initialize(name = nil, parent = nil) ⇒ Node
Returns a new instance of Node.
10 11 12 13 |
# File 'lib/redwood/node.rb', line 10 def initialize(name=nil, parent=nil) @name = name @parent = parent end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/redwood/node.rb', line 7 def name @name end |
#value ⇒ Object
Returns the value of attribute value.
8 9 10 |
# File 'lib/redwood/node.rb', line 8 def value @value end |
Instance Method Details
#<<(child) ⇒ Object
Graft a child
24 25 26 |
# File 'lib/redwood/node.rb', line 24 def <<(child) graft child end |
#[](key) ⇒ Object
Lookup a child node by its name
29 30 31 32 |
# File 'lib/redwood/node.rb', line 29 def [](key) selected_child = children.select {|child| child.name == key } selected_child.size.eql?(1) ? selected_child.first : selected_child end |
#add_child(name) {|child| ... } ⇒ Object
Creates a child, adds it to children, and returns the child
16 17 18 19 20 21 |
# File 'lib/redwood/node.rb', line 16 def add_child(name) child = self.class.new(name, self) yield child if block_given? children << child child end |