Method: StateMachines::Event#transition

Defined in:
lib/state_machines/event.rb

#transition(options) ⇒ Object

Creates a new transition that determines what to change the current state to when this event fires.

Since this transition is being defined within an event context, you do not need to specify the :on option for the transition. For example:

state_machine do
  event :ignite do
    transition :parked => :idling, :idling => same, :if => :seatbelt_on? # Transitions to :idling if seatbelt is on
    transition all => :parked, :unless => :seatbelt_on?                  # Transitions to :parked if seatbelt is off
  end
end

See StateMachines::Machine#transition for a description of the possible configurations for defining transitions.

Raises:

  • (ArgumentError)


102
103
104
105
106
107
108
109
110
111
112
# File 'lib/state_machines/event.rb', line 102

def transition(options)
  raise ArgumentError, 'Must specify as least one transition requirement' if options.empty?

  # Only a certain subset of explicit options are allowed for transition
  # requirements
  StateMachines::OptionsValidator.assert_valid_keys!(options, :from, :to, :except_from, :except_to, :if, :unless, :if_state, :unless_state, :if_all_states, :unless_all_states, :if_any_state, :unless_any_state) if (options.keys - i[from to on except_from except_to except_on if unless if_state unless_state if_all_states unless_all_states if_any_state unless_any_state]).empty?

  branches << branch = Branch.new(options.merge(on: name))
  @known_states |= branch.known_states
  branch
end