Created
June 10, 2015 13:02
-
-
Save manno/77306755468a3bc220ad to your computer and use it in GitHub Desktop.
pry command for graphing AASM models with graphviz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's an error in this script. If you want to make it work you need to apply this change: