Class: Async::Node
- Inherits:
-
Object
- Object
- Async::Node
- Defined in:
- lib/async/node.rb
Overview
Represents a node in a tree, used for nested Task instances.
Instance Attribute Summary collapse
-
#annotation ⇒ Object
readonly
A useful identifier for the current node.
- #children ⇒ Object readonly
- #parent ⇒ Object
Instance Method Summary collapse
- #annotate(annotation) ⇒ Object
-
#consume ⇒ Object
If the node has a parent, and is #finished?, then remove this node from the parent.
- #description ⇒ Object
-
#finished? ⇒ Boolean
Whether the node can be consumed safely.
-
#initialize(parent = nil) ⇒ Node
constructor
Create a new node in the tree.
- #print_hierarchy(out = $stdout) ⇒ Object
-
#reap(child) ⇒ Object
Remove a given child node.
- #to_s ⇒ Object
-
#traverse(level = 0) {|node, level| ... } ⇒ Object
Traverse the tree.
Constructor Details
#initialize(parent = nil) ⇒ Node
Create a new node in the tree.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/async/node.rb', line 28 def initialize(parent = nil) @children = nil @parent = nil @annotation = nil @object_name = nil if parent self.parent = parent end end |
Instance Attribute Details
#annotation ⇒ Object (readonly)
A useful identifier for the current node.
47 48 49 |
# File 'lib/async/node.rb', line 47 def annotation @annotation end |
#children ⇒ Object (readonly)
44 45 46 |
# File 'lib/async/node.rb', line 44 def children @children end |
#parent ⇒ Object
41 42 43 |
# File 'lib/async/node.rb', line 41 def parent @parent end |
Instance Method Details
#annotate(annotation) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/async/node.rb', line 49 def annotate(annotation) if block_given? previous_annotation = @annotation @annotation = annotation yield @annotation = previous_annotation else @annotation = annotation end end |
#consume ⇒ Object
If the node has a parent, and is #finished?, then remove this node from the parent.
107 108 109 110 111 112 113 |
# File 'lib/async/node.rb', line 107 def consume if @parent and finished? @parent.reap(self) @parent.consume @parent = nil end end |
#description ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/async/node.rb', line 60 def description @object_name ||= "#{self.class}:0x#{object_id.to_s(16)}" if @annotation "#{@object_name} #{@annotation}" else @object_name end end |
#finished? ⇒ Boolean
Whether the node can be consumed safely. By default, checks if the children set is empty.
101 102 103 |
# File 'lib/async/node.rb', line 101 def finished? @children.nil? or @children.empty? end |
#print_hierarchy(out = $stdout) ⇒ Object
131 132 133 134 135 |
# File 'lib/async/node.rb', line 131 def print_hierarchy(out = $stdout) self.traverse do |node, level| out.puts "#{"\t" * level}#{node}" end end |
#reap(child) ⇒ Object
Remove a given child node.
117 118 119 |
# File 'lib/async/node.rb', line 117 def reap(child) @children.delete(child) end |
#to_s ⇒ Object
70 71 72 |
# File 'lib/async/node.rb', line 70 def to_s "\#<#{description}>" end |
#traverse(level = 0) {|node, level| ... } ⇒ Object
Traverse the tree.
123 124 125 126 127 128 129 |
# File 'lib/async/node.rb', line 123 def traverse(level = 0, &block) yield self, level @children&.each do |child| child.traverse(level + 1, &block) end end |