Last active
August 29, 2015 14:22
-
-
Save manno/db252c98181fa1244a1e 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.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