Method: StateMachine::StateCollection#match

Defined in:
lib/state_machine/state_collection.rb

#match(object) ⇒ Object

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.

Examples

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


55
56
57
58
# File 'lib/state_machine/state_collection.rb', line 55

def match(object)
  value = machine.read(object, :state)
  self[value, :value] || detect {|state| state.matches?(value)}
end