Class: PuppetDebugServer::Hooks
- Inherits:
-
Object
- Object
- PuppetDebugServer::Hooks
- Defined in:
- lib/puppet-debugserver/hooks.rb
Overview
Both puppet-debugger and pry are licensed with MIT raw.githubusercontent.com/nwops/puppet-debugger/master/LICENSE.txt raw.githubusercontent.com/pry/pry/master/LICENSE
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 ⇒ Object
Ensure that duplicates have their @hooks object.
Constructor Details
#initialize ⇒ Hooks
Returns a new instance of Hooks.
11 12 13 |
# File 'lib/puppet-debugserver/hooks.rb', line 11 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.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/puppet-debugserver/hooks.rb', line 35 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) raise ArgumentError, "Hook with name '#{hook_name}' already defined!" if hook_exists?(event_name, hook_name) && !hook_name.nil? 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.
117 118 119 |
# File 'lib/puppet-debugserver/hooks.rb', line 117 def clear_event_hooks(event_name) @hooks[event_name.to_s] = [] end |
#delete_hook(event_name, hook_name) ⇒ #call
Returns The deleted hook.
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/puppet-debugserver/hooks.rb', line 99 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
25 26 27 |
# File 'lib/puppet-debugserver/hooks.rb', line 25 def errors @errors ||= [] end |
#exec_hook(event_name, *args, &block) ⇒ Object
Execute the list of hooks for the ‘event_name` event.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/puppet-debugserver/hooks.rb', line 60 def exec_hook(event_name, *args, &block) PuppetDebugServer.(:debug, "Starting to execute hook #{event_name}") unless event_name == :hook_log_message @hooks[event_name.to_s].map do |_hook_name, callable| callable.call(*args, &block) rescue ::RuntimeError => e errors << e e end.last PuppetDebugServer.(:debug, "Finished executing hook #{event_name}") unless event_name == :hook_log_message end |
#get_hook(event_name, hook_name) ⇒ #call
Returns a specific hook for a given event.
80 81 82 83 84 85 |
# File 'lib/puppet-debugserver/hooks.rb', line 80 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
Modifying the returned hash does not alter the hooks, use
‘add_hook`/`delete_hook` for that.
91 92 93 |
# File 'lib/puppet-debugserver/hooks.rb', line 91 def get_hooks(event_name) (@hooks[event_name.to_s]).to_h end |
#hook_count(event_name) ⇒ Fixnum
Returns The number of hook functions for ‘event_name`.
73 74 75 |
# File 'lib/puppet-debugserver/hooks.rb', line 73 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`.
124 125 126 |
# File 'lib/puppet-debugserver/hooks.rb', line 124 def hook_exists?(event_name, hook_name) @hooks[event_name.to_s].map(&:first).include?(hook_name) end |
#initialize_copy ⇒ Object
Ensure that duplicates have their @hooks object.
16 17 18 19 20 21 22 23 |
# File 'lib/puppet-debugserver/hooks.rb', line 16 def initialize_copy hooks_dup = @hooks.dup @hooks.each do |k, v| hooks_dup[k] = v.dup end @hooks = hooks_dup end |