Class: StateMachines::Branch

Inherits:
Object
  • Object
show all
Defined in:
lib/state_machines/graphviz/monkeypatch.rb

Instance Method Summary collapse

Instance Method Details

#draw(graph, event, valid_states) ⇒ Object

Draws a representation of this branch on the given graph. This will draw an edge between every state this branch matches from to either the configured to state or, if none specified, then a loopback to the from state.

For example, if the following from states are configured:

  • idling

  • first_gear

  • backing_up

…and the to state is parked, then the following edges will be created:

  • idling -> parked

  • first_gear -> parked

  • backing_up -> parked

Each edge will be labeled with the name of the event that would cause the transition.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/state_machines/graphviz/monkeypatch.rb', line 136

def draw(graph, event, valid_states)
  state_requirements.each do |state_requirement|
    # From states determined based on the known valid states
    from_states = state_requirement[:from].filter(valid_states)

    # If a to state is not specified, then it's a loopback and each from
    # state maps back to itself
    if state_requirement[:to].values.empty?
      loopback = true
    else
      to_state = state_requirement[:to].values.first
      to_state = to_state ? to_state.to_s : 'nil'
      loopback = false
    end

    # Generate an edge between each from and to state
    from_states.each do |from_state|
      from_state = from_state ? from_state.to_s : 'nil'
      graph.add_edges(from_state, loopback ? from_state : to_state, :label => event.to_s)
    end
  end

  true
end