Class: Rabbitek::Utils::HookWalker

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/rabbitek/utils/hook_walker.rb

Overview

Utility to work down the hooks setup

Instance Method Summary collapse

Methods included from Loggable

#debug, #error, #info, #logger, logger, #warn

Constructor Details

#initialize(hooks = []) ⇒ HookWalker

Returns a new instance of HookWalker.



10
11
12
# File 'lib/rabbitek/utils/hook_walker.rb', line 10

def initialize(hooks = [])
  @hooks = hooks.clone
end

Instance Method Details

#call!(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rabbitek/utils/hook_walker.rb', line 14

def call!(*args)
  return yield(*args) unless hooks.any?
  hook = hooks.pop

  debug "Calling hook: #{hook.class}"

  begin
    return_args = hook.call(*args) do |*new_args|
      hooks.any? ? call!(*new_args) { |*next_args| yield(*next_args) } : yield(*new_args)
    end
  ensure
    debug "Finishing hook: #{hook.class}"
  end

  return_args
end