Class | StateMachine::MachineCollection |
In: |
lib/state_machine/machine_collection.rb
|
Parent: | Hash |
Represents a collection of state machines for a class
Runs one or more event attributes in parallel during the invocation of an action on the given object. after_transition callbacks can be optionally disabled if the events are being only partially fired (for example, when validating records in ORM integrations).
The event attributes that will be fired are based on which machines match the action that is being invoked.
class Vehicle include DataMapper::Resource property :id, Serial state_machine :initial => :parked do event :ignite do transition :parked => :idling end end state_machine :alarm_state, :namespace => 'alarm', :initial => :active do event :disable do transition all => :off end end end
With valid events:
vehicle = Vehicle.create # => #<Vehicle id=1 state="parked" alarm_state="active"> vehicle.state_event = 'ignite' vehicle.alarm_state_event = 'disable' Vehicle.state_machines.fire_event_attributes(vehicle, :save) { true } vehicle.state # => "idling" vehicle.state_event # => nil vehicle.alarm_state # => "off" vehicle.alarm_state_event # => nil
With invalid events:
vehicle = Vehicle.create # => #<Vehicle id=1 state="parked" alarm_state="active"> vehicle.state_event = 'park' vehicle.alarm_state_event = 'disable' Vehicle.state_machines.fire_event_attributes(vehicle, :save) { true } vehicle.state # => "parked" vehicle.state_event # => nil vehicle.alarm_state # => "active" vehicle.alarm_state_event # => nil vehicle.errors # => #<DataMapper::Validate::ValidationErrors:0xb7af9abc @errors={"state_event"=>["is invalid"]}>
With partial firing:
vehicle = Vehicle.create # => #<Vehicle id=1 state="parked" alarm_state="active"> vehicle.state_event = 'ignite' Vehicle.state_machines.fire_event_attributes(vehicle, :save, false) { true } vehicle.state # => "idling" vehicle.state_event # => "ignite" vehicle.state_event_transition # => #<StateMachine::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>
Runs one or more events in parallel on the given object. See StateMachine::InstanceMethods#fire_events for more information.