Class: GetThemAll::GraphBuilder::TreeNode

Inherits:
Object
  • Object
show all
Defined in:
lib/get_them_all/extensions/graph_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix, text) ⇒ TreeNode

Returns a new instance of TreeNode.



18
19
20
21
22
# File 'lib/get_them_all/extensions/graph_builder.rb', line 18

def initialize(prefix, text)
  @prefix = prefix
  @text = text
  @children = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



16
17
18
# File 'lib/get_them_all/extensions/graph_builder.rb', line 16

def children
  @children
end

#textObject (readonly)

Returns the value of attribute text.



16
17
18
# File 'lib/get_them_all/extensions/graph_builder.rb', line 16

def text
  @text
end

Instance Method Details

#add_child(prefix, text) ⇒ Object



24
25
26
27
28
# File 'lib/get_them_all/extensions/graph_builder.rb', line 24

def add_child(prefix, text)
  ret = TreeNode.new(prefix, text)
  @children << ret
  ret
end

#dump_to_dot_file(path) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/get_them_all/extensions/graph_builder.rb', line 62

def dump_to_dot_file(path)
  File.open(path, 'w') do |f|
    f.write(<<-EOF)
    digraph toto {
node [shape=box];
#{dump_node_dot()}
    }      
    EOF
  end
end

#dump_to_file(path) ⇒ Object



44
45
46
47
48
# File 'lib/get_them_all/extensions/graph_builder.rb', line 44

def dump_to_file(path)
  File.open(path, 'w') do |f|
    f.write( dump_node(0) )
  end
end

#find_node(text) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/get_them_all/extensions/graph_builder.rb', line 30

def find_node(text)
  if @text == text
    self
  elsif !@children.empty?
    @children.map{|node| node.find_node(text) }.compact.first
  else
    nil
  end
end

#inspectObject



40
41
42
# File 'lib/get_them_all/extensions/graph_builder.rb', line 40

def inspect
  "{#{@text}}"
end