lib/state_machine/machine.rb in state_machine-0.7.1 vs lib/state_machine/machine.rb in state_machine-0.7.2
- old
+ new
@@ -923,24 +923,43 @@
# Creates a callback that will be invoked *before* a transition is
# performed so long as the given requirements match the transition.
#
# == The callback
#
- # Callbacks must be defined as either the only argument, in the :do option,
- # or as a block. For example,
+ # Callbacks must be defined as either an argument, in the :do option, or
+ # as a block. For example,
#
# class Vehicle
# state_machine do
# before_transition :set_alarm
- # before_transition all => :parked :do => :set_alarm
+ # before_transition :set_alarm, all => :parked
+ # before_transition all => :parked, :do => :set_alarm
# before_transition all => :parked do |vehicle, transition|
# vehicle.set_alarm
# end
# ...
# end
# end
#
+ # Notice that the first three callbacks are the same in terms of how the
+ # methods to invoke are defined. However, using the <tt>:do</tt> can
+ # provide for a more fluid DSL.
+ #
+ # In addition, multiple callbacks can be defined like so:
+ #
+ # class Vehicle
+ # state_machine do
+ # before_transition :set_alarm, :lock_doors, all => :parked
+ # before_transition all => :parked, :do => [:set_alarm, :lock_doors]
+ # before_transition :set_alarm do |vehicle, transition|
+ # vehicle.lock_doors
+ # end
+ # end
+ # end
+ #
+ # Notice that the different ways of configuring methods can be mixed.
+ #
# == State requirements
#
# Callbacks can require that the machine be transitioning from and to
# specific states. These requirements use a Hash syntax to map beginning
# states to ending states. For example,
@@ -1015,12 +1034,12 @@
# :unless => lambda {|user| user.signup_step <= 2}). The method, proc or
# string should return or evaluate to a true or false value.
#
# Examples:
#
- # before_transition :parked => :idling, :if => :moving?
- # before_transition :on => :ignite, :unless => :seatbelt_on?
+ # before_transition :parked => :idling, :if => :moving?, :do => ...
+ # before_transition :on => :ignite, :unless => :seatbelt_on?, :do => ...
#
# === Accessing the transition
#
# In addition to passing the object being transitioned, the actual
# transition describing the context (e.g. event, from, to) can be accessed
@@ -1029,14 +1048,18 @@
#
# For example,
#
# class Vehicle
# # Only specifies one parameter (the object being transitioned)
- # before_transition :to => :parked, :do => lambda {|vehicle| vehicle.set_alarm}
+ # before_transition :to => :parked do |vehicle|
+ # vehicle.set_alarm
+ # end
#
# # Specifies 2 parameters (object being transitioned and actual transition)
- # before_transition :to => :parked, :do => lambda {|vehicle, transition| vehicle.set_alarm(transition)}
+ # before_transition :to => :parked do |vehicle, transition|
+ # vehicle.set_alarm(transition)
+ # end
# end
#
# *Note* that the object in the callback will only be passed in as an
# argument if callbacks are configured to *not* be bound to the object
# involved. This is the default and may change on a per-integration basis.
@@ -1222,10 +1245,12 @@
attribute = self.attribute
# Tracks the event / transition to invoke when the action is called
@instance_helper_module.class_eval do
attr_writer "#{attribute}_event"
- attr_accessor "#{attribute}_event_transition"
+
+ protected
+ attr_accessor "#{attribute}_event_transition"
end
# Interpret non-blank events as present
define_instance_method("#{attribute}_event") do |machine, object|
event = object.instance_variable_get("@#{attribute}_event")