class StateMachine::Event
An event defines an action that transitions an attribute from one state to another. The state that an attribute is transitioned to depends on the branches configured for the event.
Attributes
The list of branches that determine what state this event transitions objects to when fired
The human-readable name for the event
A list of all of the states known to this event using the configured branches/transitions as the source
The state machine for which this event is defined
The name of the event
The fully-qualified name of the event, scoped by the machine's namespace
Public Instance Methods
Determines whether any transitions can be performed for this event based on the current state of the given object.
If the event can't be fired, then this will return false, otherwise true.
Note that this will not take the object context into account. Although a transition may be possible based on the state machine definition, object-specific behaviors (like validations) may prevent it from firing.
# File lib/state_machine/event.rb 125 def can_fire?(object, requirements = {}) 126 !transition_for(object, requirements).nil? 127 end
Evaluates the given block within the context of this event. This simply provides a DSL-like syntax for defining transitions.
# File lib/state_machine/event.rb 85 def context(&block) 86 instance_eval(&block) 87 end
Draws a representation of this event on the given graph. This will create 1 or more edges on the graph for each branch (i.e. transition) configured.
A collection of the generated edges will be returned.
# File lib/state_machine/event.rb 197 def draw(graph) 198 valid_states = machine.states.by_priority.map {|state| state.name} 199 branches.collect {|branch| branch.draw(graph, name, valid_states)}.flatten 200 end
Attempts to perform the next available transition on the given object. If no transitions can be made, then this will return false, otherwise true.
Any additional arguments are passed to the StateMachine::Transition#perform
instance method.
# File lib/state_machine/event.rb 163 def fire(object, *args) 164 machine.reset(object) 165 166 if transition = transition_for(object) 167 transition.perform(*args) 168 else 169 on_failure(object) 170 false 171 end 172 end
Transforms the event name into a more human-readable format, such as “turn on” instead of “turn_on”
# File lib/state_machine/event.rb 79 def human_name(klass = @machine.owner_class) 80 @human_name.is_a?(Proc) ? @human_name.call(self, klass) : @human_name 81 end
Generates a nicely formatted description of this event's contents.
For example,
event = StateMachine::Event.new(machine, :park) event.transition all - :idling => :parked, :idling => same event # => #<StateMachine::Event name=:park transitions=[all - :idling => :parked, :idling => same]>
# File lib/state_machine/event.rb 209 def inspect 210 transitions = branches.map do |branch| 211 branch.state_requirements.map do |state_requirement| 212 "#{state_requirement[:from].description} => #{state_requirement[:to].description}" 213 end * ', ' 214 end 215 216 "#<#{self.class} name=#{name.inspect} transitions=[#{transitions * ', '}]>" 217 end
Marks the object as invalid and runs any failure callbacks associated with this event. This should get called anytime this event fails to transition.
# File lib/state_machine/event.rb 176 def on_failure(object) 177 state = machine.states.match!(object) 178 machine.invalidate(object, :state, :invalid_transition, [[:event, human_name(object.class)], [:state, state.human_name(object.class)]]) 179 180 Transition.new(object, machine, name, state.name, state.name).run_callbacks(:before => false) 181 end
Resets back to the initial state of the event, with no branches / known states associated. This allows you to redefine an event in situations where you either are re-using an existing state machine implementation or are subclassing machines.
# File lib/state_machine/event.rb 187 def reset 188 @branches = [] 189 @known_states = [] 190 end
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 StateMachine::Machine#transition
for a description of the possible configurations for defining transitions.
# File lib/state_machine/event.rb 105 def transition(options) 106 raise ArgumentError, 'Must specify as least one transition requirement' if options.empty? 107 108 # Only a certain subset of explicit options are allowed for transition 109 # requirements 110 assert_valid_keys(options, :from, :to, :except_from, :if, :unless) if (options.keys - [:from, :to, :on, :except_from, :except_to, :except_on, :if, :unless]).empty? 111 112 branches << branch = Branch.new(options.merge(:on => name)) 113 @known_states |= branch.known_states 114 branch 115 end
Finds and builds the next transition that can be performed on the given object. If no transitions can be made, then this will return nil.
Valid requirement options:
-
:from
- One or more states being transitioned from. If none are specified, then this will be the object's current state. -
:to
- One or more states being transitioned to. If none are specified, then this will match any to state. -
:guard
- Whether to guard transitions with the if/unless conditionals defined for each one. Default is true.
# File lib/state_machine/event.rb 139 def transition_for(object, requirements = {}) 140 assert_valid_keys(requirements, :from, :to, :guard) 141 requirements[:from] = machine.states.match!(object).name unless custom_from_state = requirements.include?(:from) 142 143 branches.each do |branch| 144 if match = branch.match(object, requirements) 145 # Branch allows for the transition to occur 146 from = requirements[:from] 147 to = match[:to].values.empty? ? from : match[:to].values.first 148 149 return Transition.new(object, machine, name, from, to, !custom_from_state) 150 end 151 end 152 153 # No transition matched 154 nil 155 end
Protected Instance Methods
Add the various instance methods that can transition the object using the current event
# File lib/state_machine/event.rb 222 def add_actions 223 # Checks whether the event can be fired on the current object 224 machine.define_helper(:instance, "can_#{qualified_name}?") do |machine, object, *args| 225 machine.event(name).can_fire?(object, *args) 226 end 227 228 # Gets the next transition that would be performed if the event were 229 # fired now 230 machine.define_helper(:instance, "#{qualified_name}_transition") do |machine, object, *args| 231 machine.event(name).transition_for(object, *args) 232 end 233 234 # Fires the event 235 machine.define_helper(:instance, qualified_name) do |machine, object, *args| 236 machine.event(name).fire(object, *args) 237 end 238 239 # Fires the event, raising an exception if it fails 240 machine.define_helper(:instance, "#{qualified_name}!") do |machine, object, *args| 241 object.send(qualified_name, *args) || raise(StateMachine::InvalidTransition.new(object, machine, name)) 242 end 243 end