Class: Gruf::Interceptors::Registry
- Inherits:
-
Object
- Object
- Gruf::Interceptors::Registry
- Defined in:
- lib/gruf/interceptors/registry.rb
Overview
Handles registration of interceptors
Defined Under Namespace
Classes: InterceptorNotFoundError
Instance Method Summary collapse
-
#clear ⇒ Object
Clear the registry.
-
#count ⇒ Integer
The number of interceptors currently loaded.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#insert_after(after_class, interceptor_class, options = {}) ⇒ Object
Insert an interceptor after another specified interceptor.
-
#insert_before(before_class, interceptor_class, options = {}) ⇒ Object
Insert an interceptor before another specified interceptor.
-
#list ⇒ Array<Class>
Return a list of the interceptor classes in the registry in their execution order.
-
#prepare(request, error) ⇒ Array<Gruf::Interceptors::Base>
Load and return all interceptors for the given request.
-
#remove(interceptor_class) ⇒ Object
Remove an interceptor from the registry.
-
#use(interceptor_class, options = {}) ⇒ Object
Add an interceptor to the registry.
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
24 25 26 |
# File 'lib/gruf/interceptors/registry.rb', line 24 def initialize @registry ||= [] end |
Instance Method Details
#clear ⇒ Object
Clear the registry
126 127 128 129 130 |
# File 'lib/gruf/interceptors/registry.rb', line 126 def clear interceptors_mutex do @registry = [] end end |
#count ⇒ Integer
Returns The number of interceptors currently loaded.
135 136 137 138 139 140 |
# File 'lib/gruf/interceptors/registry.rb', line 135 def count interceptors_mutex do @registry ||= [] @registry.count end end |
#insert_after(after_class, interceptor_class, options = {}) ⇒ Object
Insert an interceptor after another specified interceptor
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/gruf/interceptors/registry.rb', line 83 def insert_after(after_class, interceptor_class, = {}) interceptors_mutex do pos = @registry.find_index { |opts| opts.fetch(:klass, '') == after_class } raise InterceptorNotFoundError if pos.nil? @registry.insert( (pos + 1), klass: interceptor_class, options: ) end end |
#insert_before(before_class, interceptor_class, options = {}) ⇒ Object
Insert an interceptor before another specified interceptor
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/gruf/interceptors/registry.rb', line 63 def insert_before(before_class, interceptor_class, = {}) interceptors_mutex do pos = @registry.find_index { |opts| opts.fetch(:klass, '') == before_class } raise InterceptorNotFoundError if pos.nil? @registry.insert( pos, klass: interceptor_class, options: ) end end |
#list ⇒ Array<Class>
Return a list of the interceptor classes in the registry in their execution order
100 101 102 103 104 |
# File 'lib/gruf/interceptors/registry.rb', line 100 def list interceptors_mutex do @registry.map { |h| h[:klass] } end end |
#prepare(request, error) ⇒ Array<Gruf::Interceptors::Base>
Load and return all interceptors for the given request
113 114 115 116 117 118 119 120 121 |
# File 'lib/gruf/interceptors/registry.rb', line 113 def prepare(request, error) is = [] interceptors_mutex do @registry.each do |o| is << o[:klass].new(request, error, o[:options]) end end is end |
#remove(interceptor_class) ⇒ Object
Remove an interceptor from the registry
49 50 51 52 53 |
# File 'lib/gruf/interceptors/registry.rb', line 49 def remove(interceptor_class) pos = @registry.find_index { |opts| opts.fetch(:klass, '') == interceptor_class } raise InterceptorNotFoundError if pos.nil? @registry.delete_at(pos) end |
#use(interceptor_class, options = {}) ⇒ Object
Add an interceptor to the registry
34 35 36 37 38 39 40 41 |
# File 'lib/gruf/interceptors/registry.rb', line 34 def use(interceptor_class, = {}) interceptors_mutex do @registry << { klass: interceptor_class, options: } end end |