Class: Renderer::GraphViz

Inherits:
Object
  • Object
show all
Defined in:
lib/renderer/graphviz.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_name, config) ⇒ GraphViz

Returns a new instance of GraphViz.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/renderer/graphviz.rb', line 5

def initialize(file_name, config)
  @g = Graphviz::Graph.new('G', **{
    overlap: false,
    splines: true,
    sep: 1,
    concentrate: true,
    rankdir: "LR"
  })
  @file_name = file_name
  @config = config
end

Instance Method Details

#add_edge(from, to, opts) ⇒ Object



21
22
23
24
25
26
# File 'lib/renderer/graphviz.rb', line 21

def add_edge(from, to, opts)
  from_node = create_if_missing(from)
  to_node = create_if_missing(to)
  options =  ({style: 'bold'}).merge(opts)
  from_node.connect(to_node, options)
end

#add_node(name, opts) ⇒ Object



17
18
19
# File 'lib/renderer/graphviz.rb', line 17

def add_node(name, opts)
  @g.add_node(name, label: name)
end

#create_if_missing(name) ⇒ Object



28
29
30
31
# File 'lib/renderer/graphviz.rb', line 28

def create_if_missing(name)
  n = @g.get_node(name).first
  n.nil? ? add_node(name, {}) : n
end

#outputObject



33
34
35
# File 'lib/renderer/graphviz.rb', line 33

def output
  Graphviz::output(@g, path: @file_name, format: nil) #format: nil to force detection based on extension.
end