Class: Hutils::Node
- Inherits:
-
Object
- Object
- Hutils::Node
- Defined in:
- lib/hutils.rb
Instance Attribute Summary collapse
-
#common ⇒ Object
The set of common attributes that are shared by all slots in this branch of the tree.
-
#parent ⇒ Object
Parent node.
-
#slots ⇒ Object
Ordered child nodes of this node.
-
#tags ⇒ Object
Arbitrary tags that applications can associate on a node.
Instance Method Summary collapse
-
#common_complete ⇒ Object
The set of common attributes that are shared by all slots in this branch of the tree, but accounting for all parent nodes as well.
- #depth ⇒ Object
-
#initialize(parent, common) ⇒ Node
constructor
A new instance of Node.
- #print ⇒ Object
- #replace_slot(old, new) ⇒ Object
- #root ⇒ Object
- #root? ⇒ Boolean
Constructor Details
#initialize(parent, common) ⇒ Node
Returns a new instance of Node.
8 9 10 11 12 13 |
# File 'lib/hutils.rb', line 8 def initialize(parent, common) @common = common @parent = parent @slots = [] @tags = {} end |
Instance Attribute Details
#common ⇒ Object
The set of common attributes that are shared by all slots in this branch of the tree.
17 18 19 |
# File 'lib/hutils.rb', line 17 def common @common end |
#parent ⇒ Object
Parent node.
20 21 22 |
# File 'lib/hutils.rb', line 20 def parent @parent end |
#slots ⇒ Object
Ordered child nodes of this node.
23 24 25 |
# File 'lib/hutils.rb', line 23 def slots @slots end |
#tags ⇒ Object
Arbitrary tags that applications can associate on a node.
26 27 28 |
# File 'lib/hutils.rb', line 26 def @tags end |
Instance Method Details
#common_complete ⇒ Object
The set of common attributes that are shared by all slots in this branch of the tree, but accounting for all parent nodes as well.
30 31 32 |
# File 'lib/hutils.rb', line 30 def common_complete @common.merge(@parent ? @parent.common_complete : {}) end |
#depth ⇒ Object
34 35 36 |
# File 'lib/hutils.rb', line 34 def depth @parent ? @parent.depth + 1 : 0 end |
#print ⇒ Object
38 39 40 41 42 |
# File 'lib/hutils.rb', line 38 def print indent = " " * depth $stdout.puts "#{indent}[ #{depth} ]#{@common}" slots.each { |node| node.print } end |
#replace_slot(old, new) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/hutils.rb', line 44 def replace_slot(old, new) @slots.each_with_index do |node, i| if old == node @slots[i] = new return end end abort("bad replace") end |
#root? ⇒ Boolean
58 59 60 |
# File 'lib/hutils.rb', line 58 def root? @parent == nil end |