Class: ActiveSupport::Callbacks::CallbackSequence

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

Overview

Execute before and after filters in a sequence instead of chaining them with nested lambda calls, see: github.com/rails/rails/issues/18011

Instance Method Summary collapse

Constructor Details

#initialize(&call) ⇒ CallbackSequence

Returns a new instance of CallbackSequence.



477
478
479
480
481
# File 'lib/active_support/callbacks.rb', line 477

def initialize(&call)
  @call = call
  @before = []
  @after = []
end

Instance Method Details

#after(&after) ⇒ Object



488
489
490
491
# File 'lib/active_support/callbacks.rb', line 488

def after(&after)
  @after.push(after)
  self
end

#around(&around) ⇒ Object



493
494
495
496
497
498
499
# File 'lib/active_support/callbacks.rb', line 493

def around(&around)
  CallbackSequence.new do |*args|
    around.call(*args) {
      self.call(*args)
    }
  end
end

#before(&before) ⇒ Object



483
484
485
486
# File 'lib/active_support/callbacks.rb', line 483

def before(&before)
  @before.unshift(before)
  self
end

#call(*args) ⇒ Object



501
502
503
504
505
506
# File 'lib/active_support/callbacks.rb', line 501

def call(*args)
  @before.each { |b| b.call(*args) }
  value = @call.call(*args)
  @after.each { |a| a.call(*args) }
  value
end