class PuppetDebugger::Hooks

This code was borrowed from Pry hooks file Implements a hooks system for Puppet Debugger. A hook is a callable that is associated with an event. A number of events are currently provided by Pry, these include: `:when_started`, `:before_session`, `:after_session`. A hook must have a name, and is connected with an event by the `PuppetDebugger::Hooks#add_hook` method.

@example Adding a hook for the `:before_session` event.

debugger.hooks.add_hook(:before_session, :say_hi) do
  puts "hello"
end

Attributes

hooks[R]

Public Class Methods

new() click to toggle source
# File lib/puppet-debugger/hooks.rb, line 18
def initialize
  @hooks = Hash.new { |h, k| h[k] = [] }
end

Public Instance Methods

add_hook(event_name, hook_name, callable = nil, &block) click to toggle source

Add a new hook to be executed for the `event_name` event. @param [Symbol] event_name The name of the event. @param [Symbol] hook_name The name of the hook. @param [#call] callable The callable. @yield The block to use as the callable (if no `callable` provided). @return [PuppetDebugger::Hooks] The receiver.

# File lib/puppet-debugger/hooks.rb, line 74
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
clear_event_hooks(event_name) click to toggle source

Clear all hooks functions for a given event.

@param [String] event_name The name of the event. @return [void]

# File lib/puppet-debugger/hooks.rb, line 158
def clear_event_hooks(event_name)
  @hooks[event_name.to_s] = []
end
delete_hook(event_name, hook_name) click to toggle source

@param [Symbol] event_name The name of the event. @param [Symbol] hook_name The name of the hook.

to delete.

@return [#call] The deleted hook.

# File lib/puppet-debugger/hooks.rb, line 140
def delete_hook(event_name, hook_name)
  deleted_callable = nil

  @hooks[event_name.to_s].delete_if do |current_hook_name, callable|
    if current_hook_name == hook_name
      deleted_callable = callable
      true
    else
      false
    end
  end
  deleted_callable
end
errors() click to toggle source
# File lib/puppet-debugger/hooks.rb, line 32
def errors
  @errors ||= []
end
exec_hook(event_name, *args, &block) click to toggle source

Execute the list of hooks for the `event_name` event. @param [Symbol] event_name The name of the event. @param [Array] args The arguments to pass to each hook function. @return [Object] The return value of the last executed hook.

# File lib/puppet-debugger/hooks.rb, line 101
def exec_hook(event_name, *args, &block)
  @hooks[event_name.to_s].map do |_hook_name, callable|
    begin
      callable.call(*args, &block)
    rescue PuppetDebugger::Exception::Error, ::RuntimeError => e
      errors << e
      e
    end
  end.last
end
get_hook(event_name, hook_name) click to toggle source

@param [Symbol] event_name The name of the event. @param [Symbol] hook_name The name of the hook @return [#call] a specific hook for a given event.

# File lib/puppet-debugger/hooks.rb, line 121
def get_hook(event_name, hook_name)
  hook = @hooks[event_name.to_s].find do |current_hook_name, _callable|
    current_hook_name == hook_name
  end
  hook&.last
end
get_hooks(event_name) click to toggle source

@param [Symbol] event_name The name of the event. @return [Hash] The hash of hook names / hook functions. @note Modifying the returned hash does not alter the hooks, use `add_hook`/`delete_hook` for that.

# File lib/puppet-debugger/hooks.rb, line 132
def get_hooks(event_name)
  Hash[@hooks[event_name.to_s]]
end
hook_count(event_name) click to toggle source

@param [Symbol] event_name The name of the event. @return [Fixnum] The number of hook functions for `event_name`.

# File lib/puppet-debugger/hooks.rb, line 114
def hook_count(event_name)
  @hooks[event_name.to_s].size
end
hook_exists?(event_name, hook_name) click to toggle source

@param [Symbol] event_name Name of the event. @param [Symbol] hook_name Name of the hook. @return [Boolean] Whether the hook by the name `hook_name`.

# File lib/puppet-debugger/hooks.rb, line 165
def hook_exists?(event_name, hook_name)
  @hooks[event_name.to_s].map(&:first).include?(hook_name)
end
initialize_copy(_orig) click to toggle source

Ensure that duplicates have their @hooks object.

# File lib/puppet-debugger/hooks.rb, line 23
def initialize_copy(_orig)
  hooks_dup = @hooks.dup
  @hooks.each do |k, v|
    hooks_dup[k] = v.dup
  end

  @hooks = hooks_dup
end
merge(other) click to toggle source

@example

hooks = PuppetDebugger::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
PuppetDebugger::Hooks.new.merge(hooks)

@param [PuppetDebugger::Hooks] other The `PuppetDebugger::Hooks` instance to merge @return [PuppetDebugger::Hooks] a new `PuppetDebugger::Hooks` instance containing a merge of the

contents of two `PuppetDebugger::Hooks` instances.
# File lib/puppet-debugger/hooks.rb, line 62
def merge(other)
  dup.tap do |v|
    v.merge!(other)
  end
end
merge!(other) click to toggle source

Destructively merge the contents of two `PuppetDebugger::Hooks` instances.

@param [PuppetDebugger::Hooks] other The `PuppetDebugger::Hooks` instance to merge @return [PuppetDebugger::Hooks] The receiver. @see {#merge}

# File lib/puppet-debugger/hooks.rb, line 41
def merge!(other)
  @hooks.merge!(other.dup.hooks) do |_key, array, other_array|
    temp_hash = {}
    output = []

    (array + other_array).reverse_each do |pair|
      temp_hash[pair.first] ||= output.unshift(pair)
    end

    output
  end

  self
end