module Bellbro::Hooks
Internal: Methods relating to supporting hooks around Sidekiq worker invocation.
Public Class Methods
# 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
# File lib/bellbro/hooks.rb, line 13 def abort! @abort = true end
# File lib/bellbro/hooks.rb, line 9 def aborted? !!@abort end
# File lib/bellbro/hooks.rb, line 21 def timed_out? timer.timed_out? end
# File lib/bellbro/hooks.rb, line 17 def timer @timer ||= Bellbro::Timer.new(self.class.time_out_interval) end
Private Instance Methods
Internal: Run after hooks.
Returns nothing.
# File lib/bellbro/hooks.rb, line 269 def run_after_hooks run_hooks(self.class.after_hooks) end
# File lib/bellbro/hooks.rb, line 273 def run_always_hooks run_hooks(self.class.always_hooks, halt_on_abort: false) end
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
Internal: Run before hooks.
Returns nothing.
# File lib/bellbro/hooks.rb, line 262 def run_before_hooks run_hooks(self.class.before_hooks) end
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
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
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