Class: StateMachines::Graph

Inherits:
GraphViz
  • Object
show all
Defined in:
lib/state_machines/graphviz/graph.rb

Overview

Provides a set of higher-order features on top of the raw GraphViz graphs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Creates a new graph with the given name.

Configuration options:

  • :path - The path to write the graph file to. Default is the current directory (“.”).

  • :format - The image format to generate the graph in. Default is “png’.

  • :font - The name of the font to draw state names in. Default is “Arial”.

  • :orientation - The direction of the graph (“portrait” or “landscape”). Default is “portrait”.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/state_machines/graphviz/graph.rb', line 24

def initialize(name, options = {})
  options = { path: 'doc/state_machines', format: 'png', font: 'Arial', orientation: 'portrait' }.merge(options)
  options.assert_valid_keys(:path, :format, :font, :orientation)

  # TODO fail if path cannot be created or readonly
  unless Dir.exist? options[:path]
    FileUtils.mkpath(options[:path])
  end
  @font = options[:font]
  @file_path = File.join(options[:path], "#{name}.#{options[:format]}")
  @file_format = options[:format]

  super('G', rankdir: options[:orientation] == 'landscape' ? 'LR' : 'TB')
end

Instance Attribute Details

#file_formatObject (readonly)

The image format to generate the graph in



11
12
13
# File 'lib/state_machines/graphviz/graph.rb', line 11

def file_format
  @file_format
end

#file_pathObject (readonly)

The graph’s full filename



8
9
10
# File 'lib/state_machines/graphviz/graph.rb', line 8

def file_path
  @file_path
end

#fontObject (readonly)

The name of the font to draw state names in



5
6
7
# File 'lib/state_machines/graphviz/graph.rb', line 5

def font
  @font
end

Instance Method Details

#add_edges(*args) ⇒ Object

Adds a new edge to the graph. The font for the edge will be automatically set based on the graph configuration. The generated edge will be returned.

For example,

graph = StateMachines::Graph.new('test')
graph.add_edges('parked', 'idling', :label => 'ignite')


66
67
68
69
70
# File 'lib/state_machines/graphviz/graph.rb', line 66

def add_edges(*args)
  edge = super
  edge.fontname = @font
  edge
end

#add_nodes(*args) ⇒ Object

Adds a new node to the graph. The font for the node will be automatically set based on the graph configuration. The generated node will be returned.

For example,

graph = StateMachines::Graph.new('test')
graph.add_nodes('parked', :label => 'Parked', :width => '1', :height => '1', :shape => 'ellipse')


53
54
55
56
57
# File 'lib/state_machines/graphviz/graph.rb', line 53

def add_nodes(*args)
  node = super
  node.fontname = @font
  node
end

#outputObject

Generates the actual image file based on the nodes / edges added to the graph. The path to the file is based on the configuration options for this graph.



42
43
44
# File 'lib/state_machines/graphviz/graph.rb', line 42

def output
  super(@file_format => @file_path)
end