Class: Graphy::Diagram

Inherits:
Object
  • Object
show all
Defined in:
lib/graphy/diagram.rb

Overview

GraphViz wrapper

Constant Summary collapse

GRAPH_ATTRIBUTES =
{
  ranksep:     0.5,
  nodesep:     0.4,
  pad:         '0.4,0.4',
  margin:      '0,0',
  concentrate: true,
  labelloc:    :t,
  fontsize:    14,
  fontname:    'Arial BoldMT',
  splines:     'spline'
}
NODE_ATTRIBUTES =
{
  shape:    "Mrecord",
  fontsize: 12,
  fontname: "ArialMT",
  margin:   "0.07,0.05",
  penwidth: 1.0
}
EDGE_ATTRIBUTES =
{
  fontname:      "ArialMT",
  fontsize:      10,
  arrowsize:     0.9,
  penwidth:      1.0,
  labelangle:    32,
  labeldistance: 1.8,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Diagram

Returns a new instance of Diagram.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/graphy/diagram.rb', line 35

def initialize(name, options = {})
  graph_opts = {
    parent: options[:parent],
    type: options[:parent]&.type
  }.compact
  @graph = GraphViz.new(name, **graph_opts)

  GRAPH_ATTRIBUTES.each { |attribute, value| @graph[attribute] = value }
  NODE_ATTRIBUTES.each  { |attribute, value| @graph.node[attribute] = value }
  EDGE_ATTRIBUTES.each  { |attribute, value| @graph.edge[attribute] = value }

  @graph[:rankdir] = options[:orientation] == :horizonal ? :LR : :TB
  @graph[:label] = "#{name}\\n\\n"
  @edges = []
end

Instance Attribute Details

#edgesObject

Returns the value of attribute edges.



4
5
6
# File 'lib/graphy/diagram.rb', line 4

def edges
  @edges
end

#graphObject

Returns the value of attribute graph.



4
5
6
# File 'lib/graphy/diagram.rb', line 4

def graph
  @graph
end

Instance Method Details

#draw_edge(from, to, options) ⇒ Object



63
64
65
# File 'lib/graphy/diagram.rb', line 63

def draw_edge(from, to, options)
  graph.add_edges(from, to, options)
end

#draw_graph(diagram) ⇒ Object



67
68
69
# File 'lib/graphy/diagram.rb', line 67

def draw_graph(diagram)
  graph.add_graph(diagram.graph)
end

#draw_node(name, options = {}) ⇒ Object



59
60
61
# File 'lib/graphy/diagram.rb', line 59

def draw_node(name, options = {})
  graph.add_nodes(name, options)
end

#get_node(name) ⇒ Object



51
52
53
# File 'lib/graphy/diagram.rb', line 51

def get_node(name)
  graph.search_node(name)
end

#node_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/graphy/diagram.rb', line 55

def node_exists?(name)
  !!get_node(name)
end

#write(options = {}) ⇒ Object



71
72
73
# File 'lib/graphy/diagram.rb', line 71

def write(options = {})
  graph.output(options)
end