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:



388
389
390
391
392
393
# File 'lib/active_support/callbacks.rb', line 388

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.



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

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.



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

def expand(target, value, block)
  expanded = [@override_target || target, @override_block || block, @method_name]

  @arguments.each do |arg|
    case arg
    when :value then expanded << value
    when :target then expanded << target
    when :block then expanded << (block || raise(ArgumentError))
    end
  end

  expanded
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.



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

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.



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

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