lib/state_machine/machine.rb in state_machine-0.6.2 vs lib/state_machine/machine.rb in state_machine-0.6.3
- old
+ new
@@ -1037,25 +1037,34 @@
# current directory (".").
# * <tt>:format</tt> - The image format to generate the graph in.
# Default is "png'.
# * <tt>:font</tt> - The name of the font to draw state names in.
# Default is "Arial".
+ # * <tt>:orientation</tt> - The direction of the graph ("portrait" or
+ # "landscape"). Default is "portrait".
+ # * <tt>:output</tt> - Whether to generate the output of the graph
def draw(options = {})
options = {
:name => "#{owner_class.name}_#{attribute}",
:path => '.',
:format => 'png',
- :font => 'Arial'
+ :font => 'Arial',
+ :orientation => 'portrait',
+ :output => true
}.merge(options)
- assert_valid_keys(options, :name, :font, :path, :format)
+ assert_valid_keys(options, :name, :path, :format, :font, :orientation, :output)
begin
# Load the graphviz library
require 'rubygems'
require 'graphviz'
- graph = GraphViz.new('G', :output => options[:format], :file => File.join(options[:path], "#{options[:name]}.#{options[:format]}"))
+ graph = GraphViz.new('G',
+ :output => options[:format],
+ :file => File.join(options[:path], "#{options[:name]}.#{options[:format]}"),
+ :rankdir => options[:orientation] == 'landscape' ? 'LR' : 'TB'
+ )
# Add nodes
states.by_priority.each do |state|
node = state.draw(graph)
node.fontname = options[:font]
@@ -1066,10 +1075,10 @@
edges = event.draw(graph)
edges.each {|edge| edge.fontname = options[:font]}
end
# Generate the graph
- graph.output
+ graph.output if options[:output]
graph
rescue LoadError
$stderr.puts 'Cannot draw the machine. `gem install ruby-graphviz` and try again.'
false
end