Class: Node
Overview
A Node is a Hash with an array of children and a parent associated to it.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
-
#&(index) ⇒ Object
See Node#child.
-
#<<(hash) ⇒ Array
Adds a child node to self.
-
#==(node) ⇒ Boolean
True if the nodes are equal.
-
#>>(node) ⇒ Object
Removes a child node from self.
-
#ascend(element = nil) {|element| ... } ⇒ Object
Iterates through parents recursively (including self).
-
#child(index) ⇒ Node
Returns a child by its index.
-
#clear ⇒ Object
Clears all keys, parent and children.
-
#descend(element = nil, level = 0) {|element, level| ... } ⇒ Object
Iterates through children recursively (including self).
-
#each_child {|c| ... } ⇒ Object
Iterates through children.
-
#find_child(&block) ⇒ Node?
Descend children until the block returns something.
-
#find_parent(&block) ⇒ Node?
Ascend parents until the block returns something.
-
#from(node) ⇒ self
Clone another node (replaces all keys, parent and children).
-
#initialize(*args) ⇒ Node
constructor
Creates a new Node.
-
#inspect ⇒ String
A textual representation of self.
-
#root ⇒ Node
Returns the root node.
-
#to_hash ⇒ Hash
Converts self to a hash.
-
#to_node ⇒ Node
Returns self.
Methods inherited from Hash
Constructor Details
#initialize(*args) ⇒ Node
Creates a new Node
22 23 24 25 |
# File 'lib/glyph/node.rb', line 22 def initialize(*args) super(*args) @children = [] end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
18 19 20 |
# File 'lib/glyph/node.rb', line 18 def children @children end |
#parent ⇒ Object
Returns the value of attribute parent.
19 20 21 |
# File 'lib/glyph/node.rb', line 19 def parent @parent end |
Instance Method Details
#&(index) ⇒ Object
See Node#child.
78 79 80 |
# File 'lib/glyph/node.rb', line 78 def &(index) @children[index] end |
#<<(hash) ⇒ Array
Adds a child node to self
54 55 56 57 58 59 |
# File 'lib/glyph/node.rb', line 54 def <<(hash) raise ArgumentError, "#{hash} is not a Hash" unless hash.is_a? Hash ht = hash.to_node ht.parent = self @children << ht end |
#==(node) ⇒ Boolean
Returns true if the nodes are equal.
159 160 161 162 |
# File 'lib/glyph/node.rb', line 159 def ==(node) return false unless node.is_a? Node self.to_hash == node.to_hash && self.children == node.children end |
#>>(node) ⇒ Object
Removes a child node from self
64 65 66 67 68 |
# File 'lib/glyph/node.rb', line 64 def >>(node) raise ArgumentError, "Unknown child node" unless @children.include? node node.parent = nil @children.delete node end |
#ascend(element = nil) {|element| ... } ⇒ Object
Iterates through parents recursively (including self)
129 130 131 132 133 |
# File 'lib/glyph/node.rb', line 129 def ascend(element=nil, &block) element ||= self yield element ascend(element.parent, &block) if element.parent end |
#child(index) ⇒ Node
Returns a child by its index
73 74 75 |
# File 'lib/glyph/node.rb', line 73 def child(index) @children[index] end |
#clear ⇒ Object
Clears all keys, parent and children
44 45 46 47 48 |
# File 'lib/glyph/node.rb', line 44 def clear super @children.clear @parent = nil end |
#descend(element = nil, level = 0) {|element, level| ... } ⇒ Object
Iterates through children recursively (including self)
93 94 95 96 97 98 99 |
# File 'lib/glyph/node.rb', line 93 def descend(element=nil, level=0, &block) element ||= self yield element, level element.each_child do |c| descend c, level+1, &block end end |
#each_child {|c| ... } ⇒ Object
Iterates through children
84 85 86 |
# File 'lib/glyph/node.rb', line 84 def each_child @children.each {|c| yield c } end |
#find_child(&block) ⇒ Node?
Descend children until the block returns something. Each child is passed to the block.
105 106 107 108 109 110 111 112 |
# File 'lib/glyph/node.rb', line 105 def find_child(&block) children.each do |c| c.descend do |node, level| return node if block.call(node) end end nil end |
#find_parent(&block) ⇒ Node?
Ascend parents until the block returns something. Each parent is passed to the block.
118 119 120 121 122 123 124 |
# File 'lib/glyph/node.rb', line 118 def find_parent(&block) return nil unless parent parent.ascend do |node| return node if block.call(node) end nil end |
#from(node) ⇒ self
Clone another node (replaces all keys, parent and children)
35 36 37 38 39 40 41 |
# File 'lib/glyph/node.rb', line 35 def from(node) n = node.to_node replace node @parent = n.parent @children = n.children self end |
#inspect ⇒ String
Returns a textual representation of self.
149 150 151 152 153 154 155 |
# File 'lib/glyph/node.rb', line 149 def inspect string = "" descend do |e, level| string << " "*level+e.to_hash.inspect+"\n" end string.chomp end |
#root ⇒ Node
Returns the root node
136 137 138 |
# File 'lib/glyph/node.rb', line 136 def root ascend(parent) {|e| return e unless e.parent } end |
#to_hash ⇒ Hash
Converts self to a hash
143 144 145 |
# File 'lib/glyph/node.rb', line 143 def to_hash {}.merge(self) end |