lib/state_machine/event.rb in state_machine-0.4.0 vs lib/state_machine/event.rb in state_machine-0.4.1
- old
+ new
@@ -18,11 +18,11 @@
# The list of guards that determine what state this event transitions
# objects to when fired
attr_reader :guards
# A list of all of the states known to this event using the configured
- # guards/transitions as the source.
+ # guards/transitions as the source
attr_reader :known_states
# Creates a new event within the context of the given machine
def initialize(machine, name) #:nodoc:
@machine = machine
@@ -43,11 +43,11 @@
# Creates a new transition that will be evaluated when the event is fired.
#
# Configuration options:
# * +to+ - The state that being transitioned to. If not specified, then the transition will not change the state.
- # * +from+ - A state or array of states that can be transitioned from. If not specified, then the transition can occur for *any* from state
+ # * +from+ - A state or array of states that can be transitioned from. If not specified, then the transition can occur for *any* from state.
# * +except_from+ - A state or array of states that *cannot* be transitioned from.
# * +if+ - Specifies a method, proc or string to call to determine if the transition should occur (e.g. :if => :moving?, or :if => Proc.new {|car| car.speed > 60}). The method, proc or string should return or evaluate to a true or false value.
# * +unless+ - Specifies a method, proc or string to call to determine if the transition should not occur (e.g. :unless => :stopped?, or :unless => Proc.new {|car| car.speed <= 60}). The method, proc or string should return or evaluate to a true or false value.
#
# == Order of operations
@@ -66,12 +66,14 @@
#
# transition :to => lambda {Time.now}
#
# == Examples
#
+ # transition :from => nil, :to => 'parked'
# transition :from => %w(first_gear reverse)
# transition :except_from => 'parked'
+ # transition :to => nil
# transition :to => 'parked'
# transition :to => lambda {Time.now}
# transition :to => 'parked', :from => 'first_gear'
# transition :to => 'parked', :from => %w(first_gear reverse)
# transition :to => 'parked', :from => 'first_gear', :if => :moving?
@@ -98,10 +100,10 @@
def next_transition(object)
from = object.send(machine.attribute)
if guard = guards.find {|guard| guard.matches?(object, :from => from)}
# Guard allows for the transition to occur
- to = guard.requirements[:to] || from
+ to = guard.requirements[:to] ? guard.requirements[:to].first : from
to = to.call if to.is_a?(Proc)
Transition.new(object, machine, name, from, to)
end
end