Class: PuppetDebugger::Hooks

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-debugger/hooks.rb

Overview

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.

Examples:

Adding a hook for the ‘:before_session` event.

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

Instance Method Summary collapse

Constructor Details

#initializeHooks

Returns a new instance of Hooks.



16
17
18
# File 'lib/puppet-debugger/hooks.rb', line 16

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

Instance Method Details

#add_hook(event_name, hook_name, callable = nil) { ... } ⇒ PuppetDebugger::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:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/puppet-debugger/hooks.rb', line 71

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

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

  # 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) ⇒ void

This method returns an undefined value.

Clear all hooks functions for a given event.

Parameters:

  • event_name (String)

    The name of the event.



157
158
159
# File 'lib/puppet-debugger/hooks.rb', line 157

def clear_event_hooks(event_name)
  @hooks[event_name.to_s] = []
end

#delete_hook(event_name, hook_name) ⇒ #call

Returns The deleted hook.

Parameters:

  • event_name (Symbol)

    The name of the event.

  • hook_name (Symbol)

    The name of the hook. to delete.

Returns:

  • (#call)

    The deleted hook.



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/puppet-debugger/hooks.rb', line 139

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

#errorsObject



30
31
32
# File 'lib/puppet-debugger/hooks.rb', line 30

def errors
  @errors ||= []
end

#exec_hook(event_name, *args, &block) ⇒ Object

Execute the list of hooks for the ‘event_name` event.

Parameters:

  • event_name (Symbol)

    The name of the event.

  • args (Array)

    The arguments to pass to each hook function.

Returns:

  • (Object)

    The return value of the last executed hook.



100
101
102
103
104
105
106
107
108
109
# File 'lib/puppet-debugger/hooks.rb', line 100

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) ⇒ #call

Returns a specific hook for a given event.

Parameters:

  • event_name (Symbol)

    The name of the event.

  • hook_name (Symbol)

    The name of the hook

Returns:

  • (#call)

    a specific hook for a given event.



120
121
122
123
124
125
# File 'lib/puppet-debugger/hooks.rb', line 120

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 if hook
end

#get_hooks(event_name) ⇒ Hash

Note:

Modifying the returned hash does not alter the hooks, use

‘add_hook`/`delete_hook` for that.

Parameters:

  • event_name (Symbol)

    The name of the event.

Returns:

  • (Hash)

    The hash of hook names / hook functions.



131
132
133
# File 'lib/puppet-debugger/hooks.rb', line 131

def get_hooks(event_name)
  Hash[@hooks[event_name.to_s]]
end

#hook_count(event_name) ⇒ Fixnum

Returns The number of hook functions for ‘event_name`.

Parameters:

  • event_name (Symbol)

    The name of the event.

Returns:

  • (Fixnum)

    The number of hook functions for ‘event_name`.



113
114
115
# File 'lib/puppet-debugger/hooks.rb', line 113

def hook_count(event_name)
  @hooks[event_name.to_s].size
end

#hook_exists?(event_name, hook_name) ⇒ Boolean

Returns Whether the hook by the name ‘hook_name`.

Parameters:

  • event_name (Symbol)

    Name of the event.

  • hook_name (Symbol)

    Name of the hook.

Returns:

  • (Boolean)

    Whether the hook by the name ‘hook_name`.



164
165
166
# File 'lib/puppet-debugger/hooks.rb', line 164

def hook_exists?(event_name, hook_name)
  @hooks[event_name.to_s].map(&:first).include?(hook_name)
end

#initialize_copy(orig) ⇒ Object

Ensure that duplicates have their @hooks object.



21
22
23
24
25
26
27
28
# File 'lib/puppet-debugger/hooks.rb', line 21

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) ⇒ PuppetDebugger::Hooks

Returns a new ‘PuppetDebugger::Hooks` instance containing a merge of the contents of two `PuppetDebugger::Hooks` instances.

Examples:

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

Parameters:

Returns:

  • (PuppetDebugger::Hooks)

    a new ‘PuppetDebugger::Hooks` instance containing a merge of the contents of two `PuppetDebugger::Hooks` instances.



59
60
61
62
63
# File 'lib/puppet-debugger/hooks.rb', line 59

def merge(other)
  self.dup.tap do |v|
    v.merge!(other)
  end
end

#merge!(other) ⇒ PuppetDebugger::Hooks

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

Parameters:

Returns:

See Also:

  • PuppetDebugger::Hooks.{{#merge}


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/puppet-debugger/hooks.rb', line 39

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