Class | StateMachine::Branch |
In: |
lib/state_machine/branch.rb
|
Parent: | Object |
Represents a set of requirements that must be met in order for a transition or callback to occur. Branches verify that the event, from state, and to state of the transition match, in addition to if/unless conditionals for an object‘s state.
event_requirement | [R] | The requirement for verifying the event being matched |
if_condition | [R] | The condition that must be met on an object |
known_states | [R] |
A list of all of the states known to this branch. This will pull states
from the following options (in the same order):
|
state_requirements | [R] | One or more requirements for verifying the states being matched. All requirements contain a mapping of {:from => matcher, :to => matcher}. |
unless_condition | [R] | The condition that must not be met on an 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:
…and the to state is parked, then the following edges will be created:
Each edge will be labeled with the name of the event that would cause the transition.
The collection of edges generated on the graph will be returned.
Attempts to match the given object / query against the set of requirements configured for this branch. In addition to matching the event, from state, and to state, this will also check whether the configured :if/:unless conditions pass on the given object.
If a match is found, then the event/state requirements that the query passed successfully will be returned. Otherwise, nil is returned if there was no match.
Query options:
branch = StateMachine::Branch.new(:parked => :idling, :on => :ignite) branch.match(object, :on => :ignite) # => {:to => ..., :from => ..., :on => ...} branch.match(object, :on => :park) # => nil
Determines whether the given object / query matches the requirements configured for this branch. In addition to matching the event, from state, and to state, this will also check whether the configured :if/:unless conditions pass on the given object.
branch = StateMachine::Branch.new(:parked => :idling, :on => :ignite) # Successful branch.matches?(object, :on => :ignite) # => true branch.matches?(object, :from => nil) # => true branch.matches?(object, :from => :parked) # => true branch.matches?(object, :to => :idling) # => true branch.matches?(object, :from => :parked, :to => :idling) # => true branch.matches?(object, :on => :ignite, :from => :parked, :to => :idling) # => true # Unsuccessful branch.matches?(object, :on => :park) # => false branch.matches?(object, :from => :idling) # => false branch.matches?(object, :to => :first_gear) # => false branch.matches?(object, :from => :parked, :to => :first_gear) # => false branch.matches?(object, :on => :park, :from => :parked, :to => :idling) # => false
Builds a matcher strategy to use for the given options. If neither a whitelist nor a blacklist option is specified, then an AllMatcher is built.
Verifies that the state requirements match the given query. If a matching requirement is found, then it is returned.