module Bellbro::Hooks

Internal: Methods relating to supporting hooks around Sidekiq worker invocation.

Public Class Methods

included(base) click to toggle source
# File lib/bellbro/hooks.rb, line 4
def self.included(base)
  base.class_eval do
    extend ClassMethods
  end

  def aborted?
    !!@abort
  end

  def abort!
    @abort = true
  end

  def timer
    @timer ||= Bellbro::Timer.new(self.class.time_out_interval)
  end

  def timed_out?
    timer.timed_out?
  end
end

Public Instance Methods

abort!() click to toggle source
# File lib/bellbro/hooks.rb, line 13
def abort!
  @abort = true
end
aborted?() click to toggle source
# File lib/bellbro/hooks.rb, line 9
def aborted?
  !!@abort
end
timed_out?() click to toggle source
# File lib/bellbro/hooks.rb, line 21
def timed_out?
  timer.timed_out?
end
timer() click to toggle source
# File lib/bellbro/hooks.rb, line 17
def timer
  @timer ||= Bellbro::Timer.new(self.class.time_out_interval)
end

Private Instance Methods

run_after_hooks() click to toggle source

Internal: Run after hooks.

Returns nothing.

# File lib/bellbro/hooks.rb, line 269
def run_after_hooks
  run_hooks(self.class.after_hooks)
end
run_always_hooks() click to toggle source
# File lib/bellbro/hooks.rb, line 273
def run_always_hooks
  run_hooks(self.class.always_hooks, halt_on_abort: false)
end
run_around_hooks(&block) click to toggle source

Internal: Run around hooks.

Returns nothing.

# File lib/bellbro/hooks.rb, line 253
def run_around_hooks(&block)
  self.class.around_hooks.reverse.inject(block) { |chain, hook|
    proc { run_hook(hook, chain) }
  }.call
end
run_before_hooks() click to toggle source

Internal: Run before hooks.

Returns nothing.

# File lib/bellbro/hooks.rb, line 262
def run_before_hooks
  run_hooks(self.class.before_hooks)
end
run_hook(hook, *args) click to toggle source

Internal: Run an individual hook. The “run_hook” method is the common interface by which an individual hook is run. If the given hook is a symbol, the method is invoked whether public or private. If the hook is a proc, the proc is evaluated in the context of the current instance.

hook - A Symbol or Proc hook. args - Zero or more arguments to be passed as block arguments into the

given block or as arguments into the method described by the given
Symbol method name.

Returns nothing.

# File lib/bellbro/hooks.rb, line 301
def run_hook(hook, *args)
  hook.is_a?(Symbol) ? send(hook, *args) : instance_exec(*args, &hook)
end
run_hooks(hooks, halt_on_abort: true) click to toggle source

Internal: Run a colection of hooks. The “run_hooks” method is the common interface by which collections of either before or after hooks are run.

hooks - An Array of Symbol and Proc hooks.

Returns nothing.

# File lib/bellbro/hooks.rb, line 283
def run_hooks(hooks, halt_on_abort: true)
  hooks.each do |hook|
    run_hook(hook)
    break if aborted? && halt_on_abort
  end
end
with_hooks() { || ... } click to toggle source

Internal: Run around, before and after hooks around yielded execution. The required block is surrounded with hooks and executed.

Examples

class MyProcessor
  include Bellbro::Hooks

  def process_with_hooks
    with_hooks do
      process
    end
  end

  def process
    puts "processed!"
  end
end

Returns nothing.

# File lib/bellbro/hooks.rb, line 242
def with_hooks
  run_around_hooks do
    run_before_hooks
    yield
    run_after_hooks
  end
end