Class: Grifork::Graph::Node

Inherits:
Object
  • Object
show all
Includes:
Configured
Defined in:
lib/grifork/graph/node.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configured

#config

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

.countObject (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

#childrenObject (readonly)

Returns the value of attribute children.



3
4
5
# File 'lib/grifork/graph/node.rb', line 3

def children
  @children
end

#hostObject (readonly)

Returns the value of attribute host.



3
4
5
# File 'lib/grifork/graph/node.rb', line 3

def host
  @host
end

#indexObject (readonly)

Returns the value of attribute index.



3
4
5
# File 'lib/grifork/graph/node.rb', line 3

def index
  @index
end

#levelObject (readonly)

Returns the value of attribute level.



3
4
5
# File 'lib/grifork/graph/node.rb', line 3

def level
  @level
end

#numberObject (readonly)

Returns the value of attribute number.



3
4
5
# File 'lib/grifork/graph/node.rb', line 3

def number
  @number
end

#parentObject (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

.addObject



7
8
9
10
# File 'lib/grifork/graph/node.rb', line 7

def add
  @count ||= 0
  @count  += 1
end

Instance Method Details

#acceptable?Boolean

Returns:

  • (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_nodesObject



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

#idObject



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

Returns:

  • (Boolean)


49
50
51
# File 'lib/grifork/graph/node.rb', line 49

def local?
  parent ? false : true
end

#to_sObject



28
29
30
# File 'lib/grifork/graph/node.rb', line 28

def to_s
  "<#{index}:#{id}>"
end