Class | StateMachine::Event |
In: |
lib/state_machine/event.rb
|
Parent: | Object |
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.
branches | [R] | The list of branches that determine what state this event transitions objects to when fired |
human_name | [W] | The human-readable name for the event |
known_states | [R] | A list of all of the states known to this event using the configured branches/transitions as the source |
machine | [RW] | The state machine for which this event is defined |
name | [R] | The name of the event |
qualified_name | [R] | The fully-qualified name of the event, scoped by the machine‘s namespace |
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, line 130 130: def can_fire?(object, requirements = {}) 131: !transition_for(object, requirements).nil? 132: 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, line 202 202: def draw(graph) 203: valid_states = machine.states.by_priority.map {|state| state.name} 204: branches.collect {|branch| branch.draw(graph, name, valid_states)}.flatten 205: 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, line 168 168: def fire(object, *args) 169: machine.reset(object) 170: 171: if transition = transition_for(object) 172: transition.perform(*args) 173: else 174: on_failure(object) 175: false 176: end 177: 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, line 84 84: def human_name(klass = @machine.owner_class) 85: @human_name.is_a?(Proc) ? @human_name.call(self, klass) : @human_name 86: 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, line 214 214: def inspect 215: transitions = branches.map do |branch| 216: branch.state_requirements.map do |state_requirement| 217: "#{state_requirement[:from].description} => #{state_requirement[:to].description}" 218: end * ', ' 219: end 220: 221: "#<#{self.class} name=#{name.inspect} transitions=[#{transitions * ', '}]>" 222: end
Converts the name of this event to a string
# File lib/state_machine/event.rb, line 70 70: def name_to_s 71: name.to_s 72: 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, line 181 181: def on_failure(object) 182: state = machine.states.match!(object) 183: machine.invalidate(object, :state, :invalid_transition, [[:event, human_name(object.class)], [:state, state.human_name(object.class)]]) 184: 185: Transition.new(object, machine, name, state.name, state.name).run_callbacks(:before => false) 186: 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, line 192 192: def reset 193: @branches = [] 194: @known_states = [] 195: 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, line 110 110: def transition(options) 111: raise ArgumentError, 'Must specify as least one transition requirement' if options.empty? 112: 113: # Only a certain subset of explicit options are allowed for transition 114: # requirements 115: 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? 116: 117: branches << branch = Branch.new(options.merge(:on => name)) 118: @known_states |= branch.known_states 119: branch 120: 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:
# File lib/state_machine/event.rb, line 144 144: def transition_for(object, requirements = {}) 145: assert_valid_keys(requirements, :from, :to, :guard) 146: requirements[:from] = machine.states.match!(object).name unless custom_from_state = requirements.include?(:from) 147: 148: branches.each do |branch| 149: if match = branch.match(object, requirements) 150: # Branch allows for the transition to occur 151: from = requirements[:from] 152: to = match[:to].values.empty? ? from : match[:to].values.first 153: 154: return Transition.new(object, machine, name, from, to, !custom_from_state) 155: end 156: end 157: 158: # No transition matched 159: nil 160: end
Add the various instance methods that can transition the object using the current event
# File lib/state_machine/event.rb, line 227 227: def add_actions 228: # Checks whether the event can be fired on the current object 229: machine.define_helper(:instance, "can_#{qualified_name}?") do |machine, object, *args| 230: machine.event(name).can_fire?(object, *args) 231: end 232: 233: # Gets the next transition that would be performed if the event were 234: # fired now 235: machine.define_helper(:instance, "#{qualified_name}_transition") do |machine, object, *args| 236: machine.event(name).transition_for(object, *args) 237: end 238: 239: # Fires the event 240: machine.define_helper(:instance, qualified_name) do |machine, object, *args| 241: machine.event(name).fire(object, *args) 242: end 243: 244: # Fires the event, raising an exception if it fails 245: machine.define_helper(:instance, "#{qualified_name}!") do |machine, object, *args| 246: object.send(qualified_name, *args) || raise(StateMachine::InvalidTransition.new(object, machine, name)) 247: end 248: end