Method: Pry::Hooks#add_hook

Defined in:
lib/pry/hooks.rb

#add_hook(event_name, hook_name, callable = nil) { ... } ⇒ Pry:Hooks

Add a new hook to be executed for the `event_name` event.

Parameters:

  • event_name (Symbol)

    The name of the event.

  • hook_name (Symbol)

    The name of the hook.

  • callable (#call) (defaults to: nil)

    The callable.

Yields:

  • The block to use as the callable (if no `callable` provided).

Returns:

  • (Pry:Hooks)

    The receiver.

Raises:

  • (ArgumentError)

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/pry/hooks.rb', line 81

def add_hook(event_name, hook_name, callable = nil, &block)
  event_name = event_name.to_s

  # do not allow duplicates, but allow multiple `nil` hooks
  # (anonymous hooks)
  if hook_exists?(event_name, hook_name) && !hook_name.nil?
    raise ArgumentError, "Hook with name '#{hook_name}' already defined!"
  end

  raise ArgumentError, "Must provide a block or callable." if !block && !callable

  # ensure we only have one anonymous hook
  @hooks[event_name].delete_if { |h, _k| h.nil? } if hook_name.nil?

  if block
    @hooks[event_name] << [hook_name, block]
  elsif callable
    @hooks[event_name] << [hook_name, callable]
  end

  self
end