Class: PuppetDebugger::Hooks
- Inherits:
-
Object
- Object
- PuppetDebugger::Hooks
- 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.
Instance Method Summary collapse
-
#add_hook(event_name, hook_name, callable = nil) { ... } ⇒ PuppetDebugger::Hooks
Add a new hook to be executed for the ‘event_name` event.
-
#clear_event_hooks(event_name) ⇒ void
Clear all hooks functions for a given event.
-
#delete_hook(event_name, hook_name) ⇒ #call
The deleted hook.
- #errors ⇒ Object
-
#exec_hook(event_name, *args, &block) ⇒ Object
Execute the list of hooks for the ‘event_name` event.
-
#get_hook(event_name, hook_name) ⇒ #call
A specific hook for a given event.
-
#get_hooks(event_name) ⇒ Hash
‘add_hook`/`delete_hook` for that.
-
#hook_count(event_name) ⇒ Fixnum
The number of hook functions for ‘event_name`.
-
#hook_exists?(event_name, hook_name) ⇒ Boolean
Whether the hook by the name ‘hook_name`.
-
#initialize ⇒ Hooks
constructor
A new instance of Hooks.
-
#initialize_copy(_orig) ⇒ Object
Ensure that duplicates have their @hooks object.
-
#merge(other) ⇒ PuppetDebugger::Hooks
A new ‘PuppetDebugger::Hooks` instance containing a merge of the contents of two `PuppetDebugger::Hooks` instances.
-
#merge!(other) ⇒ PuppetDebugger::Hooks
Destructively merge the contents of two ‘PuppetDebugger::Hooks` instances.
Constructor Details
#initialize ⇒ Hooks
Returns a new instance of Hooks.
18 19 20 |
# File 'lib/puppet-debugger/hooks.rb', line 18 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.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# 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) ⇒ void
This method returns an undefined value.
Clear all hooks functions for a given event.
158 159 160 |
# 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) ⇒ #call
Returns The deleted hook.
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# 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 ⇒ Object
32 33 34 |
# File 'lib/puppet-debugger/hooks.rb', line 32 def errors @errors ||= [] end |
#exec_hook(event_name, *args, &block) ⇒ Object
Execute the list of hooks for the ‘event_name` event.
101 102 103 104 105 106 107 108 109 110 |
# 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) ⇒ #call
Returns a specific hook for a given event.
121 122 123 124 125 126 |
# 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) ⇒ Hash
Modifying the returned hash does not alter the hooks, use
‘add_hook`/`delete_hook` for that.
132 133 134 |
# File 'lib/puppet-debugger/hooks.rb', line 132 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`.
114 115 116 |
# 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) ⇒ Boolean
Returns Whether the hook by the name ‘hook_name`.
165 166 167 |
# 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) ⇒ Object
Ensure that duplicates have their @hooks object.
23 24 25 26 27 28 29 30 |
# 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) ⇒ PuppetDebugger::Hooks
Returns a new ‘PuppetDebugger::Hooks` instance containing a merge of the contents of two `PuppetDebugger::Hooks` instances.
62 63 64 65 66 |
# File 'lib/puppet-debugger/hooks.rb', line 62 def merge(other) dup.tap do |v| v.merge!(other) end end |
#merge!(other) ⇒ PuppetDebugger::Hooks
Destructively merge the contents of two ‘PuppetDebugger::Hooks` instances.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# 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 |