Class: Grifork::Graph::Node
- Inherits:
-
Object
- Object
- Grifork::Graph::Node
- Includes:
- Configured
- Defined in:
- lib/grifork/graph/node.rb
Class Attribute Summary collapse
-
.count ⇒ Object
readonly
Returns the value of attribute count.
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Class Method Summary collapse
Instance Method Summary collapse
- #acceptable? ⇒ Boolean
- #add_child(child) ⇒ Object
- #all_descendant_nodes ⇒ Object
- #id ⇒ Object
-
#initialize(host, parent: nil) ⇒ Node
constructor
A new instance of Node.
- #local? ⇒ Boolean
- #to_s ⇒ Object
Methods included from Configured
Constructor Details
#initialize(host, parent: nil) ⇒ Node
Returns a new instance of Node.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/grifork/graph/node.rb', line 13 def initialize(host, parent: nil) @host = host @children = [] if parent @parent = parent @level = parent.level + 1 @number = parent.children.size else @level = 0 @number = 0 end @index = self.class.count || 0 self.class.add end |
Class Attribute Details
.count ⇒ Object (readonly)
Returns the value of attribute count.
6 7 8 |
# File 'lib/grifork/graph/node.rb', line 6 def count @count end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
3 4 5 |
# File 'lib/grifork/graph/node.rb', line 3 def children @children end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
3 4 5 |
# File 'lib/grifork/graph/node.rb', line 3 def host @host end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
3 4 5 |
# File 'lib/grifork/graph/node.rb', line 3 def index @index end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
3 4 5 |
# File 'lib/grifork/graph/node.rb', line 3 def level @level end |
#number ⇒ Object (readonly)
Returns the value of attribute number.
3 4 5 |
# File 'lib/grifork/graph/node.rb', line 3 def number @number end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
3 4 5 |
# File 'lib/grifork/graph/node.rb', line 3 def parent @parent end |
Class Method Details
.add ⇒ Object
7 8 9 10 |
# File 'lib/grifork/graph/node.rb', line 7 def add @count ||= 0 @count += 1 end |
Instance Method Details
#acceptable? ⇒ Boolean
53 54 55 |
# File 'lib/grifork/graph/node.rb', line 53 def acceptable? children.size < config.branches end |
#add_child(child) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/grifork/graph/node.rb', line 42 def add_child(child) unless acceptable? raise "Unacceptable!" end @children << child end |
#all_descendant_nodes ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/grifork/graph/node.rb', line 57 def all_descendant_nodes @descendants = @children @descendables = @children.select { |c| c.children.size > 0 } while @descendables.size > 0 child = @descendables.shift @descendants.concat(child.children) new_descendables = child.children { |n| n.children.size > 0 } @descendables.concat(new_descendables) end @descendants end |
#id ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/grifork/graph/node.rb', line 32 def id @id ||= -> { if parent "#{parent.id}-#{level}.#{number}" else "#{level}.#{number}" end }.call end |
#local? ⇒ Boolean
49 50 51 |
# File 'lib/grifork/graph/node.rb', line 49 def local? parent ? false : true end |
#to_s ⇒ Object
28 29 30 |
# File 'lib/grifork/graph/node.rb', line 28 def to_s "<#{index}:#{id}>" end |