Class: Separatum::GraphViz::Drawer

Inherits:
Object
  • Object
show all
Defined in:
lib/separatum/graph_viz/drawer.rb

Instance Method Summary collapse

Constructor Details

#initialize(svg_file_name: nil, dot_file_name: nil) ⇒ Drawer

Returns a new instance of Drawer.



4
5
6
7
8
# File 'lib/separatum/graph_viz/drawer.rb', line 4

def initialize(svg_file_name: nil, dot_file_name: nil)
  @svg_file_name = svg_file_name
  @dot_file_name = dot_file_name
  @gvp = ::Separatum::GraphViz::Proxy.new(::GraphViz.new(:G, type: :digraph))
end

Instance Method Details

#draw(object_transitions) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/separatum/graph_viz/drawer.rb', line 10

def draw(object_transitions)
  object_transitions.each do |prev_object, next_object|
    prev_node = @gvp.add_node(object_title(prev_object))
    next_node = @gvp.add_node(object_title(next_object))
    @gvp.add_edge(prev_node, next_node)
  end

  if @svg_file_name
    @gvp.output(svg: @svg_file_name)
  end

  if @dot_file_name
    @gvp.output(dot: @dot_file_name)
  end

  self
end

#object_title(object) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/separatum/graph_viz/drawer.rb', line 28

def object_title(object)
  if object.id.is_a?(String) && object.id.is_uuid?
    "#{object.class}[#{object.id.slice(0, 6)}]"
  else
    "#{object.class}[#{object.id}]"
  end
end