Class: Rattler::Util::GraphViz::DigraphBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rattler/util/graphviz/digraph_builder.rb

Overview

DigraphBuilder is used to build GraphViz objects representing trees of nodes.

Author:

  • Jason Arhart

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, name = 'G') ⇒ DigraphBuilder

Create a new digraph builder for root.



33
34
35
36
37
38
39
# File 'lib/rattler/util/graphviz/digraph_builder.rb', line 33

def initialize(root, name='G')
  @root = root
  @g = ::GraphViz.digraph(name)
  @nodes = {}
  @node_serial = 0
  @node_builder = NodeBuilder.new
end

Class Method Details

.digraph(root, name = 'G') ⇒ Object

Return a new GraphViz digraph object representing root.

Returns:

  • a new GraphViz digraph object representing root



28
29
30
# File 'lib/rattler/util/graphviz/digraph_builder.rb', line 28

def self.digraph(root, name='G')
  self.new(root, name).digraph
end

Instance Method Details

#digraphObject

Returns a new GraphViz digraph object representing the root object.

Returns:

  • a new GraphViz digraph object representing the root object



42
43
44
45
46
47
# File 'lib/rattler/util/graphviz/digraph_builder.rb', line 42

def digraph
  @digraph ||= begin
    node(@root)
    @g
  end
end

#node(o) ⇒ Object

Return a GraphViz::Node object for o. Multiple requests with the same object return the same node object.



53
54
55
56
57
58
59
60
# File 'lib/rattler/util/graphviz/digraph_builder.rb', line 53

def node(o)
  @nodes.fetch(o.object_id) do
    new_node = @g.add_node new_node_name, @node_builder.node_options(o)
    @nodes[o.object_id] = new_node
    @node_builder.each_child_of(o) {|_| new_node << node(_) }
    new_node
  end
end