Class: ActiveSupport::Callbacks::CallTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/active_support/callbacks.rb

Overview

A future invocation of user-supplied code (either as a callback, or a condition filter).

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, method, arguments, block) ⇒ CallTemplate

:nodoc:



385
386
387
388
389
390
# File 'lib/active_support/callbacks.rb', line 385

def initialize(target, method, arguments, block)
  @override_target = target
  @method_name = method
  @arguments = arguments
  @override_block = block
end

Class Method Details

.build(filter, callback) ⇒ Object

Filters support:

Symbols:: A method to call.
Procs::   A proc to call with the object.
Objects:: An object with a <tt>before_foo</tt> method on it to call.

All of these objects are converted into a CallTemplate and handled the same after this point.



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/active_support/callbacks.rb', line 449

def self.build(filter, callback)
  case filter
  when Symbol
    new(nil, filter, [], nil)
  when Conditionals::Value
    new(filter, :call, [:target, :value], nil)
  when ::Proc
    if filter.arity > 1
      new(nil, :instance_exec, [:target, :block], filter)
    elsif filter.arity > 0
      new(nil, :instance_exec, [:target], filter)
    else
      new(nil, :instance_exec, [], filter)
    end
  else
    method_to_call = callback.current_scopes.join("_")

    new(filter, method_to_call, [:target], nil)
  end
end

Instance Method Details

#expand(target, value, block) ⇒ Object

Return the parts needed to make this call, with the given input values.

Returns an array of the form:

[target, block, method, *arguments]

This array can be used as such:

target.send(method, *arguments, &block)

The actual invocation is left up to the caller to minimize call stack pollution.



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/active_support/callbacks.rb', line 405

def expand(target, value, block)
  result = @arguments.map { |arg|
    case arg
    when :value; value
    when :target; target
    when :block; block || raise(ArgumentError)
    end
  }

  result.unshift @method_name
  result.unshift @override_block || block
  result.unshift @override_target || target

  # target, block, method, *arguments = result
  # target.send(method, *arguments, &block)
  result
end

#inverted_lambdaObject

Return a lambda that will make this call when given the input values, but then return the boolean inverse of that result.



434
435
436
437
438
439
# File 'lib/active_support/callbacks.rb', line 434

def inverted_lambda
  lambda do |target, value, &block|
    target, block, method, *arguments = expand(target, value, block)
    ! target.send(method, *arguments, &block)
  end
end

#make_lambdaObject

Return a lambda that will make this call when given the input values.



425
426
427
428
429
430
# File 'lib/active_support/callbacks.rb', line 425

def make_lambda
  lambda do |target, value, &block|
    target, block, method, *arguments = expand(target, value, block)
    target.send(method, *arguments, &block)
  end
end