Class: ActiverecordCallbackLens::Renderer::GraphvizRenderer
- Inherits:
-
Object
- Object
- ActiverecordCallbackLens::Renderer::GraphvizRenderer
- 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
-
.render(graph, expand: false) ⇒ String
DOT language string.
Instance Method Summary collapse
-
#initialize(graph, expand: false) ⇒ GraphvizRenderer
constructor
A new instance of GraphvizRenderer.
-
#render ⇒ String
DOT language string.
-
#to_svg ⇒ String?
Shells out to the
dotbinary and returns the rendered SVG string.
Constructor Details
#initialize(graph, expand: false) ⇒ GraphvizRenderer
Returns a new instance of GraphvizRenderer.
32 33 34 35 |
# File 'lib/activerecord_callback_lens/renderer/graphviz_renderer.rb', line 32 def initialize(graph, expand: false) @graph = graph = end |
Class Method Details
.render(graph, expand: false) ⇒ String
Returns 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: ).render end |
Instance Method Details
#render ⇒ String
Returns 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_svg ⇒ String?
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.
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 |