Class: ActiverecordCallbackLens::Renderer::GraphvizRenderer

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

Overview

Renders a Graph::Graph as a Graphviz DOT language string.

The output is a ‘digraph` block with a left-to-right rank direction, one declaration per node and one arrow per edge:

digraph callback_lens {
  rankdir=LR;
  n0 [label="before_save"];
  n1 [label="active?"];
  n1 -> n0;
}

Node labels are derived from the node type (see #node_label) using the same four-case logic as MermaidRenderer. Double quotes in a label are escaped as ‘"` so they cannot break the surrounding DOT label syntax.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph) ⇒ GraphvizRenderer

Returns a new instance of GraphvizRenderer.

Parameters:



30
31
32
# File 'lib/activerecord_callback_lens/renderer/graphviz_renderer.rb', line 30

def initialize(graph)
  @graph = graph
end

Class Method Details

.render(graph) ⇒ String

Returns DOT language string.

Parameters:

Returns:

  • (String)

    DOT language string



25
26
27
# File 'lib/activerecord_callback_lens/renderer/graphviz_renderer.rb', line 25

def self.render(graph)
  new(graph).render
end

Instance Method Details

#renderString

Returns DOT language string.

Returns:

  • (String)

    DOT language string



35
36
37
38
39
40
41
# File 'lib/activerecord_callback_lens/renderer/graphviz_renderer.rb', line 35

def render
  lines = ["digraph callback_lens {", "  rankdir=LR;"]
  lines.concat(node_declarations)
  lines.concat(edge_declarations)
  lines << "}"
  lines.join("\n")
end

#to_svgString?

Shells out to the ‘dot` binary and returns the rendered SVG string. Returns nil when Graphviz is not installed (the `dot` binary is absent from PATH) rather than raising.

Returns:

  • (String, nil)


48
49
50
51
52
53
54
55
56
# File 'lib/activerecord_callback_lens/renderer/graphviz_renderer.rb', line 48

def to_svg
  IO.popen(["dot", "-Tsvg"], "r+") do |io|
    io.write(render)
    io.close_write
    io.read
  end
rescue Errno::ENOENT
  nil
end