Class: CoreExt::Callbacks::CallbackSequence

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/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.



422
423
424
425
426
# File 'lib/core_ext/callbacks.rb', line 422

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

Instance Method Details

#after(&after) ⇒ Object



433
434
435
436
# File 'lib/core_ext/callbacks.rb', line 433

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

#around(&around) ⇒ Object



438
439
440
441
442
443
444
# File 'lib/core_ext/callbacks.rb', line 438

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

#before(&before) ⇒ Object



428
429
430
431
# File 'lib/core_ext/callbacks.rb', line 428

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

#call(arg) ⇒ Object



446
447
448
449
450
451
# File 'lib/core_ext/callbacks.rb', line 446

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