Class: Backburner::Hooks

Inherits:
Object
  • Object
show all
Defined in:
lib/backburner/hooks.rb

Class Method Summary collapse

Class Method Details

.around_hook_events(hookable, event, *args, &block) ⇒ Object

Triggers all method hooks that match given around event type. Used for ‘around’ hooks that stack over the original task cumulatively onto one another.

The final block will be the one that actually invokes the original task after calling all other around blocks.

Examples:

around_hook_events(hookable, :around_perform) { hookable.perform }


25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/backburner/hooks.rb', line 25

def around_hook_events(hookable, event, *args, &block)
  raise "Please pass a block to hook events!" unless block_given?
  around_hooks = find_hook_events(hookable, event).reverse
  aggregate_filter = Proc.new { |&blk| blk.call }
  around_hooks.each do |ah|
    prior_around_filter = aggregate_filter
    aggregate_filter = Proc.new do |&blk|
      hookable.method(ah).call(*args) do
        prior_around_filter.call(&blk)
      end
    end
  end
  aggregate_filter.call(&block)
end

.invoke_hook_events(hookable, event, *args) ⇒ Object

Triggers all method hooks that match the given event type with specified arguments.

Examples:

invoke_hook_events(hookable, :before_enqueue, 'some', 'args')
invoke_hook_events(hookable, :after_perform, 5)


10
11
12
13
14
# File 'lib/backburner/hooks.rb', line 10

def invoke_hook_events(hookable, event, *args)
  res = find_hook_events(hookable, event).map { |e| hookable.send(e, *args) }
  return false if res.any? { |result| result == false }
  res
end