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.
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.
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.
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]>
Marks the object as invalid and runs any failure callbacks associated with this event. This should get called anytime this event fails to transition.
Creates a new transition that determines what to change the current state to when this event fires.
The options for a new transition uses the Hash syntax to map beginning states to ending states. For example,
transition :parked => :idling, :idling => :first_gear
In this case, when the event is fired, this transition will cause the state to be idling if it‘s current state is parked or first_gear if it‘s current state is idling.
To help define these implicit transitions, a set of helpers are available for slightly more complex matching:
See StateMachine::MatcherHelpers for more information.
Examples:
transition all => nil # Transitions to nil regardless of the current state transition all => :idling # Transitions to :idling regardless of the current state transition all - [:idling, :first_gear] => :idling # Transitions every state but :idling and :first_gear to :idling transition nil => :idling # Transitions to :idling from the nil state transition :parked => :idling # Transitions to :idling if :parked transition [:parked, :stalled] => :idling # Transitions to :idling if :parked or :stalled transition :parked => same # Loops :parked back to :parked transition [:parked, :stalled] => same # Loops either :parked or :stalled back to the same state transition all - :parked => same # Loops every state but :parked back to the same state # Transitions to :idling if :parked, :first_gear if :idling, or :second_gear if :first_gear transition :parked => :idling, :idling => :first_gear, :first_gear => :second_gear
Transitions can also be defined use an explicit set of deprecated configuration options:
Examples:
transition :to => nil transition :to => :idling transition :except_from => [:idling, :first_gear], :to => :idling transition :from => nil, :to => :idling transition :from => [:parked, :stalled], :to => :idling transition :from => :parked transition :from => [:parked, :stalled] transition :except_from => :parked
Notice that the above examples are the verbose equivalent of the examples described initially.
In addition to the state requirements for each transition, a condition can also be defined to help determine whether that transition is available. These options will work on both the normal and verbose syntax.
Configuration options:
Examples:
transition :parked => :idling, :if => :moving? transition :parked => :idling, :unless => :stopped? transition :idling => :first_gear, :first_gear => :second_gear, :if => :seatbelt_on? transition :from => :parked, :to => :idling, :if => :moving? transition :from => :parked, :to => :idling, :unless => :stopped?
Transitions are evaluated in the order in which they‘re defined. As a result, if more than one transition applies to a given object, then the first transition that matches will be performed.
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:
Add the various instance methods that can transition the object using the current event