Class: Gruf::Hooks::Registry
- Inherits:
-
Object
- Object
- Gruf::Hooks::Registry
- Defined in:
- lib/gruf/hooks/registry.rb
Overview
Handles registration of hooks
Defined Under Namespace
Classes: HookNotFoundError
Instance Method Summary collapse
-
#clear ⇒ Object
Clear the registry.
-
#count ⇒ Integer
The number of hooks currently loaded.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#insert_after(after_class, hook_class, options = {}) ⇒ Object
Insert a hook after another specified hook.
-
#insert_before(before_class, hook_class, options = {}) ⇒ Object
Insert a hook before another specified hook.
-
#list ⇒ Array<Class>
Return a list of the hook classes in the registry in their execution order.
-
#prepare ⇒ Array<Gruf::Hooks::Base>
Lazily load and return all hooks for the given request.
-
#remove(hook_class) ⇒ Object
Remove a hook from the registry.
-
#use(hook_class, options = {}) ⇒ Object
Add a hook to the registry.
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
26 27 28 |
# File 'lib/gruf/hooks/registry.rb', line 26 def initialize @registry = [] end |
Instance Method Details
#clear ⇒ Object
Clear the registry
129 130 131 132 133 |
# File 'lib/gruf/hooks/registry.rb', line 129 def clear hooks_mutex do @registry = [] end end |
#count ⇒ Integer
Returns The number of hooks currently loaded.
138 139 140 141 142 143 |
# File 'lib/gruf/hooks/registry.rb', line 138 def count hooks_mutex do @registry ||= [] @registry.count end end |
#insert_after(after_class, hook_class, options = {}) ⇒ Object
Insert a hook after another specified hook
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/gruf/hooks/registry.rb', line 87 def insert_after(after_class, hook_class, = {}) hooks_mutex do pos = @registry.find_index { |opts| opts.fetch(:klass, '') == after_class } raise HookNotFoundError if pos.nil? @registry.insert( (pos + 1), klass: hook_class, options: ) end end |
#insert_before(before_class, hook_class, options = {}) ⇒ Object
Insert a hook before another specified hook
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/gruf/hooks/registry.rb', line 66 def insert_before(before_class, hook_class, = {}) hooks_mutex do pos = @registry.find_index { |opts| opts.fetch(:klass, '') == before_class } raise HookNotFoundError if pos.nil? @registry.insert( pos, klass: hook_class, options: ) end end |
#list ⇒ Array<Class>
Return a list of the hook classes in the registry in their execution order
105 106 107 108 109 |
# File 'lib/gruf/hooks/registry.rb', line 105 def list hooks_mutex do @registry.map { |h| h[:klass] } end end |
#prepare ⇒ Array<Gruf::Hooks::Base>
Lazily load and return all hooks for the given request
116 117 118 119 120 121 122 123 124 |
# File 'lib/gruf/hooks/registry.rb', line 116 def prepare is = [] hooks_mutex do @registry.each do |o| is << o[:klass].new(options: o[:options]) end end is end |
#remove(hook_class) ⇒ Object
Remove a hook from the registry
51 52 53 54 55 56 |
# File 'lib/gruf/hooks/registry.rb', line 51 def remove(hook_class) pos = @registry.find_index { |opts| opts.fetch(:klass, '') == hook_class } raise HookNotFoundError if pos.nil? @registry.delete_at(pos) end |
#use(hook_class, options = {}) ⇒ Object
Add a hook to the registry
36 37 38 39 40 41 42 43 |
# File 'lib/gruf/hooks/registry.rb', line 36 def use(hook_class, = {}) hooks_mutex do @registry << { klass: hook_class, options: } end end |