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.
- #inspect ⇒ Object
- #print_hierarchy(out = $stdout) ⇒ Object
-
#reap(child) ⇒ Object
Remove a given child node.
- #stop ⇒ Object
- #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.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/async/node.rb', line 30 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.
49 50 51 |
# File 'lib/async/node.rb', line 49 def annotation @annotation end |
#children ⇒ Object (readonly)
46 47 48 |
# File 'lib/async/node.rb', line 46 def children @children end |
#parent ⇒ Object
43 44 45 |
# File 'lib/async/node.rb', line 43 def parent @parent end |
Instance Method Details
#annotate(annotation) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/async/node.rb', line 51 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.
113 114 115 116 117 118 119 |
# File 'lib/async/node.rb', line 113 def consume if @parent and finished? @parent.reap(self) @parent.consume @parent = nil end end |
#description ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/async/node.rb', line 62 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.
107 108 109 |
# File 'lib/async/node.rb', line 107 def finished? @children.nil? or @children.empty? end |
#inspect ⇒ Object
76 77 78 |
# File 'lib/async/node.rb', line 76 def inspect to_s end |
#print_hierarchy(out = $stdout) ⇒ Object
141 142 143 144 145 |
# File 'lib/async/node.rb', line 141 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.
123 124 125 |
# File 'lib/async/node.rb', line 123 def reap(child) @children.delete(child) end |
#stop ⇒ Object
137 138 139 |
# File 'lib/async/node.rb', line 137 def stop @children&.each(&:stop) end |
#to_s ⇒ Object
72 73 74 |
# File 'lib/async/node.rb', line 72 def to_s "\#<#{description}>" end |
#traverse(level = 0) {|node, level| ... } ⇒ Object
Traverse the tree.
129 130 131 132 133 134 135 |
# File 'lib/async/node.rb', line 129 def traverse(level = 0, &block) yield self, level @children&.each do |child| child.traverse(level + 1, &block) end end |