Module: Expedite::Hooks

Included in:
Agent
Defined in:
lib/expedite/hooks.rb

Instance Method Summary collapse

Instance Method Details

#all_hooksObject

Returne all hooks as a hash



29
30
31
# File 'lib/expedite/hooks.rb', line 29

def all_hooks
  @all_hooks ||= Hash.new { |h, k| h[k] = [] }
end

#clear_hooks(name) ⇒ Object

Clears all hooks for the specified name

Parameters:

  • name (String)

    Name of the hook



16
17
18
# File 'lib/expedite/hooks.rb', line 16

def clear_hooks(name)
  all_hooks[name] = []
end

#hooks(name) ⇒ Object

Returns all hooks for the specified name

Parameters:

  • name (String)

    Name of the hook



23
24
25
# File 'lib/expedite/hooks.rb', line 23

def hooks(name)
  all_hooks[name]
end

#register_hook(name, block) ⇒ Object

Register a new hook with the given block to name

Parameters:

  • name (String)

    Name of the hook



6
7
8
9
10
11
# File 'lib/expedite/hooks.rb', line 6

def register_hook(name, block)
  return clear_hooks(name) if block.nil?

  block = Array(block)
  all_hooks[name].concat(block)
end

#run_hook(name, *args) ⇒ Object

Runs all Procs registered for the specified name

Parameters:

  • name (String)

    Name of the hook

  • @args

    Arguments passed to the Procs



37
38
39
40
41
42
43
44
# File 'lib/expedite/hooks.rb', line 37

def run_hook(name, *args)
  hks = hooks(name)
  return if hks.empty?

  hks.each do |hook|
    args.any? ? hook.call(*args) : hook.call
  end
end