| Class | StateMachine::StateCollection |
| In: |
lib/state_machine/state_collection.rb
|
| Parent: | NodeCollection |
Represents a collection of states in a state machine
Gets the order in which states should be displayed based on where they were first referenced. This will order states in the following priority:
This order will determine how the GraphViz visualizations are rendered.
Determines the current state of the given object as configured by this state machine. This will attempt to find a known state that matches the value of the attribute on the object.
class Vehicle
state_machine :initial => :parked do
other_states :idling
end
end
states = Vehicle.state_machine.states
vehicle = Vehicle.new # => #<Vehicle:0xb7c464b0 @state="parked">
states.match(vehicle) # => #<StateMachine::State name=:parked value="parked" initial=true>
vehicle.state = 'idling'
states.match(vehicle) # => #<StateMachine::State name=:idling value="idling" initial=true>
vehicle.state = 'invalid'
states.match(vehicle) # => nil
Determines the current state of the given object as configured by this state machine. If no state is found, then an ArgumentError will be raised.
class Vehicle
state_machine :initial => :parked do
other_states :idling
end
end
states = Vehicle.state_machine.states
vehicle = Vehicle.new # => #<Vehicle:0xb7c464b0 @state="parked">
states.match!(vehicle) # => #<StateMachine::State name=:parked value="parked" initial=true>
vehicle.state = 'invalid'
states.match!(vehicle) # => ArgumentError: "invalid" is not a known state value
Determines whether the given object is in a specific state. If the object‘s current value doesn‘t match the state, then this will return false, otherwise true. If the given state is unknown, then an IndexError will be raised.
class Vehicle
state_machine :initial => :parked do
other_states :idling
end
end
states = Vehicle.state_machine.states
vehicle = Vehicle.new # => #<Vehicle:0xb7c464b0 @state="parked">
states.matches?(vehicle, :parked) # => true
states.matches?(vehicle, :idling) # => false
states.matches?(vehicle, :invalid) # => IndexError: :invalid is an invalid key for :name index