class EnumStateMachine::Callback

Callbacks represent hooks into objects that allow logic to be triggered before, after, or around a specific set of transitions.

Attributes

bind_to_object[RW]

Determines whether to automatically bind the callback to the object being transitioned. This only applies to callbacks that are defined as lambda blocks (or Procs). Some integrations handle callbacks by executing them bound to the object involved, while other integrations, such as ActiveRecord, pass the object as an argument to the callback. This can be configured on an application-wide basis by setting this configuration to true or false. The default value is false.

Examples

When not bound to the object:

class Vehicle
  state_machine do
    before_transition do |vehicle|
      vehicle.set_alarm
    end
  end

  def set_alarm
    ...
  end
end

When bound to the object:

EnumStateMachine::Callback.bind_to_object = true

class Vehicle
  state_machine do
    before_transition do
      self.set_alarm
    end
  end

  def set_alarm
    ...
  end
end
terminator[RW]

The application-wide terminator to use for callbacks when not explicitly defined. Terminators determine whether to cancel a callback chain based on the return value of the callback.

See EnumStateMachine::Callback#terminator for more information.

branch[R]

The branch that determines whether or not this callback can be invoked based on the context of the transition. The event, from state, and to state must all match in order for the branch to pass.

See EnumStateMachine::Branch for more information.

terminator[R]

An optional block for determining whether to cancel the callback chain based on the return value of the callback. By default, the callback chain never cancels based on the return value (i.e. there is no implicit terminator). Certain integrations, such as ActiveRecord, change this default value.

Examples

Canceling the callback chain without a terminator:

class Vehicle
  state_machine do
    before_transition do |vehicle|
      throw :halt
    end
  end
end

Canceling the callback chain with a terminator value of false:

class Vehicle
  state_machine do
    before_transition do |vehicle|
      false
    end
  end
end
type[RW]

The type of callback chain this callback is for. This can be one of the following:

  • before

  • after

  • around

  • failure

Public Class Methods

new(type, *args, &block) click to toggle source

Creates a new callback that can get called based on the configured options.

In addition to the possible configuration options for branches, the following options can be configured:

  • :bind_to_object - Whether to bind the callback to the object involved. If set to false, the object will be passed as a parameter instead. Default is integration-specific or set to the application default.

  • :terminator - A block/proc that determines what callback results should cause the callback chain to halt (if not using the default throw :halt technique).

More information about how those options affect the behavior of the callback can be found in their attribute definitions.

    # File lib/enum_state_machine/callback.rb
119 def initialize(type, *args, &block)
120   @type = type
121   raise ArgumentError, 'Type must be :before, :after, :around, or :failure' unless [:before, :after, :around, :failure].include?(type)
122   
123   options = args.last.is_a?(Hash) ? args.pop : {}
124   @methods = args
125   @methods.concat(Array(options.delete(:do)))
126   @methods << block if block_given?
127   raise ArgumentError, 'Method(s) for callback must be specified' unless @methods.any?
128   
129   options = {:bind_to_object => self.class.bind_to_object, :terminator => self.class.terminator}.merge(options)
130   
131   # Proxy lambda blocks so that they're bound to the object
132   bind_to_object = options.delete(:bind_to_object)
133   @methods.map! do |method|
134     bind_to_object && method.is_a?(Proc) ? bound_method(method) : method
135   end
136   
137   @terminator = options.delete(:terminator)
138   @branch = Branch.new(options)
139 end

Public Instance Methods

call(object, context = {}, *args, &block) click to toggle source

Runs the callback as long as the transition context matches the branch requirements configured for this callback. If a block is provided, it will be called when the last method has run.

If a terminator has been configured and it matches the result from the evaluated method, then the callback chain should be halted.

    # File lib/enum_state_machine/callback.rb
153 def call(object, context = {}, *args, &block)
154   if @branch.matches?(object, context)
155     run_methods(object, context, 0, *args, &block)
156     true
157   else
158     false
159   end
160 end
known_states() click to toggle source

Gets a list of the states known to this callback by looking at the branch's known states

    # File lib/enum_state_machine/callback.rb
143 def known_states
144   branch.known_states
145 end

Private Instance Methods

bound_method(block) click to toggle source

Generates a method that can be bound to the object being transitioned when the callback is invoked

    # File lib/enum_state_machine/callback.rb
195 def bound_method(block)
196   type = self.type
197   arity = block.arity
198   arity += 1 if arity >= 0 # Make sure the object gets passed
199   arity += 1 if arity == 1 && type == :around  # Make sure the block gets passed
200   
201   method = if RUBY_VERSION >= '1.9'
202     lambda do |object, *args|
203       object.instance_exec(*args, &block)
204     end
205   else
206     # Generate a thread-safe unbound method that can be used on any object.
207     # This is a workaround for not having Ruby 1.9's instance_exec
208     unbound_method = Object.class_eval do
209       time = Time.now
210       method_name = "__bind_#{time.to_i}_#{time.usec}"
211       define_method(method_name, &block)
212       method = instance_method(method_name)
213       remove_method(method_name)
214       method
215     end
216     
217     # Proxy calls to the method so that the method can be bound *and*
218     # the arguments are adjusted
219     lambda do |object, *args|
220       unbound_method.bind(object).call(*args)
221     end
222   end
223   
224   # Proxy arity to the original block
225   (class << method; self; end).class_eval do
226     define_method(:arity) { arity }
227   end
228   
229   method
230 end
run_methods(object, context = {}, index = 0, *args) { || ... } click to toggle source

Runs all of the methods configured for this callback.

When running around callbacks, this will evaluate each method and yield when the last method has yielded. The callback will only halt if one of the methods does not yield.

For all other types of callbacks, this will evaluate each method in order. The callback will only halt if the resulting value from the method passes the terminator.

    # File lib/enum_state_machine/callback.rb
172 def run_methods(object, context = {}, index = 0, *args, &block)
173   if type == :around
174     if current_method = @methods[index]
175       yielded = false
176       evaluate_method(object, current_method, *args) do
177         yielded = true
178         run_methods(object, context, index + 1, *args, &block)
179       end
180       
181       throw :halt unless yielded
182     else
183       yield if block_given?
184     end
185   else
186     @methods.each do |method|
187       result = evaluate_method(object, method, *args)
188       throw :halt if @terminator && @terminator.call(result)
189     end
190   end
191 end