Module: StatesmanViz

Defined in:
lib/statesman_viz.rb,
lib/statesman_viz/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.generate(state_machine_class) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/statesman_viz.rb', line 6

def generate(state_machine_class)
  unless state_machine_class.respond_to?(:states)
    raise ArgumentError, "State machine class must respond to `.states`"
  end
  unless state_machine_class.respond_to?(:successors)
    raise ArgumentError, "State machine class must respond to `.successors`"
  end

  state_graph = GraphViz.new( :G, :type => :digraph )

  node_map = {}
  state_machine_class.states.each do |state|
    state = state.to_s
    node_map[state] = state_graph.add_nodes(state)
  end

  state_machine_class.successors.each do |from_state, to_states|
    from_state = from_state.to_s
    to_states.each do |to_state|
      to_state = to_state.to_s
      state_graph.add_edges(node_map[from_state], node_map[to_state])
    end
  end

  output_file_path = "/tmp/StatesmanViz"
  unless Dir.exists?(output_file_path)
    FileUtils.mkdir_p(output_file_path)
  end

  output_file_name = File.join(output_file_path, "#{state_machine_class.to_s}.png")

  state_graph.output(:png => output_file_name)
end