Class: Gruf::Hooks::Registry
- Inherits:
-
Object
- Object
- Gruf::Hooks::Registry
- Defined in:
- lib/gruf/hooks/registry.rb
Overview
Registry of all hooks added
Defined Under Namespace
Classes: HookDescendantError
Class Method Summary collapse
-
.[](name) ⇒ Gruf::Instrumentation::Base|NilClass
Return a hook via a hash accessor syntax.
-
.add(name, hook = nil, &block) ⇒ Class
Add an authentication strategy, either through a class or a block.
-
.any? ⇒ Boolean
Return true if there are any registered hooks.
-
.clear ⇒ Hash
Clear all existing hooks from the registry.
-
.count ⇒ Integer
Return the number of registered hooks.
-
.each ⇒ Object
Iterate over each hook in the registry.
-
.to_h ⇒ Hash<Class>
Return the registry represented as a Hash object.
Class Method Details
.[](name) ⇒ Gruf::Instrumentation::Base|NilClass
Return a hook via a hash accessor syntax
58 59 60 |
# File 'lib/gruf/hooks/registry.rb', line 58 def [](name) _registry[name.to_sym] end |
.add(name, hook = nil, &block) ⇒ Class
Add an authentication strategy, either through a class or a block
a block that can be built as a hook instead
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/gruf/hooks/registry.rb', line 40 def add(name, hook = nil, &block) base = Gruf::Hooks::Base hook ||= Class.new(base) hook.class_eval(&block) if block_given? # all hooks require either the before, after, or around method raise NoMethodError unless hook.method_defined?(:before) || hook.method_defined?(:after) || hook.method_defined?(:around) || hook.method_defined?(:outer_around) raise HookDescendantError, "Hooks must descend from #{base}" unless hook.ancestors.include?(base) _registry[name.to_sym] = hook end |
.any? ⇒ Boolean
Return true if there are any registered hooks
81 82 83 |
# File 'lib/gruf/hooks/registry.rb', line 81 def any? count > 0 end |
.clear ⇒ Hash
Returns Clear all existing hooks from the registry.
95 96 97 |
# File 'lib/gruf/hooks/registry.rb', line 95 def clear @_registry = {} end |
.count ⇒ Integer
Return the number of registered hooks
88 89 90 |
# File 'lib/gruf/hooks/registry.rb', line 88 def count to_h.keys.count end |
.each ⇒ Object
Iterate over each hook in the registry
65 66 67 68 69 |
# File 'lib/gruf/hooks/registry.rb', line 65 def each _registry.each do |name, s| yield name, s end end |
.to_h ⇒ Hash<Class>
Return the registry represented as a Hash object
74 75 76 |
# File 'lib/gruf/hooks/registry.rb', line 74 def to_h _registry end |