Skip to content

Instantly share code, notes, and snippets.

@manno
Last active August 29, 2015 14:22
Show Gist options
  • Save manno/db252c98181fa1244a1e to your computer and use it in GitHub Desktop.
Save manno/db252c98181fa1244a1e 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.config.commands = Pry::CommandSet.new do
helpers do
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
end
command "aasm_graph" do |klass|
output.puts dot_notation(klass)
end
command "save_aasm_graph" do |klass|
`echo "#{dot_notation(klass)}" | dot -Tjpg -o #{klass.to_s.downcase}.jpg`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment