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, expand: false) ⇒ GraphvizRenderer

Returns a new instance of GraphvizRenderer.

Parameters:

  • graph (Graph::Graph)
  • expand (Boolean) (defaults to: false)


32
33
34
35
# File 'lib/activerecord_callback_lens/renderer/graphviz_renderer.rb', line 32

def initialize(graph, expand: false)
  @graph = graph
  @expand = expand
end

Class Method Details

.render(graph, expand: false) ⇒ String

Returns DOT language string.

Parameters:

  • graph (Graph::Graph)
  • expand (Boolean) (defaults to: false)

    expand proc filter labels to their source snippet

Returns:

  • (String)

    DOT language string



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

def self.render(graph, expand: false)
  new(graph, expand: expand).render
end

Instance Method Details

#renderString

Returns DOT language string.

Returns:

  • (String)

    DOT language string



38
39
40
41
42
43
44
# File 'lib/activerecord_callback_lens/renderer/graphviz_renderer.rb', line 38

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)


51
52
53
54
55
56
57
58
59
# File 'lib/activerecord_callback_lens/renderer/graphviz_renderer.rb', line 51

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