Method: StateMachine::Callback#initialize

Defined in:
lib/state_machine/callback.rb

#initialize(type, *args, &block) ⇒ Callback

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.

Raises:

  • (ArgumentError)


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/state_machine/callback.rb', line 123

def initialize(type, *args, &block)
  @type = type
  raise ArgumentError, 'Type must be :before, :after, :around, or :failure' unless [:before, :after, :around, :failure].include?(type)
  
  options = args.last.is_a?(Hash) ? args.pop : {}
  @methods = args
  @methods.concat(Array(options.delete(:do)))
  @methods << block if block_given?
  raise ArgumentError, 'Method(s) for callback must be specified' unless @methods.any?
  
  options = {:bind_to_object => self.class.bind_to_object, :terminator => self.class.terminator}.merge(options)
  
  # Proxy lambda blocks so that they're bound to the object
  bind_to_object = options.delete(:bind_to_object)
  @methods.map! do |method|
    bind_to_object && method.is_a?(Proc) ? bound_method(method) : method
  end
  
  @terminator = options.delete(:terminator)
  @branch = Branch.new(options)
end