Skip to content

Instantly share code, notes, and snippets.

@manno
Created June 10, 2015 13:02
Show Gist options
  • Save manno/77306755468a3bc220ad to your computer and use it in GitHub Desktop.
Save manno/77306755468a3bc220ad to your computer and use it in GitHub Desktop.
pry command for graphing AASM models with graphviz
# see https://github.com/ivantsepp/aasm_graph/blob/master/bin/aasm_graph
Pry::Commands.helpers {
def dot_template(edges)
<<-DOT
digraph cronjob {
rankdir=LR; /* This should be configurable */
node [shape = circle];
#{edges}
}
DOT
end
def dot_notation(klass)
klass = Object.const_get(klass) if klass.is_a? String
edges = []
if initial = klass.aasm.initial_state
edges << "initial [shape=point];"
edges << "initial -> #{initial};"
end
klass.aasm.events.each do |_, event|
event.transitions.each do |transition|
edges << "#{transition.from} -> #{transition.to} [ label = \"#{event.name}\" ];"
end
end
dot_template(edges.join("\n"))
end
}
Pry::Commands.create_command "aasm_graph" do
description "print dot notation for aasm graph"
def process
output.puts dot_notation(args.first)
end
end
Pry::Commands.create_command "save_aasm_graph" do
description "save graph of an aasm to jpg file."
def process
klass = args.first
`echo "#{dot_notation(klass)}" | dot -Tjpg -o #{klass.to_s.downcase}.jpg`
end
end
@pjg
Copy link

pjg commented Aug 12, 2015

There's an error in this script. If you want to make it work you need to apply this change:

-  klass.aasm.events.each do |_, event|
+  klass.aasm.events.each do |event|

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment