Module: Ducktape::Hookable::InstanceMethods

Defined in:
lib/ducktape/hookable.rb

Instance Method Summary collapse

Instance Method Details

#add_hook(event, hook = nil, &block) ⇒ Object

Registers a block, a named method, or any object that responds to call to be triggered when the event occurs.

Raises:

  • (ArgumentError)


109
110
111
112
113
114
115
# File 'lib/ducktape/hookable.rb', line 109

def add_hook(event, hook = nil, &block)
  hook = block if block #block has precedence

  raise ArgumentError, 'no hook was passed' unless hook
  hook = hook.to_s unless hook.respond_to?(:call)
  hooks[event.to_s].unshift(hook)
  hook
end

#clear_hooks(event = nil) ⇒ Object

Removes all hooks from the specified event. If an event wasn’t passed, removes all hooks from all events.



126
127
128
129
130
131
132
133
134
# File 'lib/ducktape/hookable.rb', line 126

def clear_hooks(event = nil)
  if event
    hooks.delete(event.to_s) if hooks.has_key?(event.to_s)
    return
  end

  hooks.clear
  nil
end

#remove_hook(event, hook) ⇒ Object

Removes the specified hook. Returns nil if the hook wasn’t found.



118
119
120
121
122
# File 'lib/ducktape/hookable.rb', line 118

def remove_hook(event, hook)
  return unless hooks.has_key?(event.to_s)
  hook = hook.to_s unless hook.respond_to?(:call)
  hooks[event.to_s].delete(hook)
end