class Gruf::Hooks::Registry
Handles registration of hooks
Public Class Methods
# File lib/gruf/hooks/registry.rb, line 26 def initialize @registry = [] end
Public Instance Methods
Clear the registry
# File lib/gruf/hooks/registry.rb, line 129 def clear hooks_mutex do @registry = [] end end
@return [Integer] The number of hooks currently loaded
# File lib/gruf/hooks/registry.rb, line 138 def count hooks_mutex do @registry ||= [] @registry.count end end
Insert a hook after another specified hook
@param [Class] after_class The hook to insert after @param [Class] hook_class The class of the hook to add @param [Hash] options A hash of options to pass into the hook during initialization @raise [HookNotFoundError] if the after hook is not found
# File lib/gruf/hooks/registry.rb, line 87 def insert_after(after_class, hook_class, options = {}) 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: options ) end end
Insert a hook before another specified hook
@param [Class] before_class The hook to insert before @param [Class] hook_class The class of the hook to add @param [Hash] options A hash of options to pass into the hook during initialization @raise [HookNotFoundError] if the before hook is not found
# File lib/gruf/hooks/registry.rb, line 66 def insert_before(before_class, hook_class, options = {}) 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: options ) end end
Return a list of the hook classes in the registry in their execution order
@return [Array<Class>]
# File lib/gruf/hooks/registry.rb, line 105 def list hooks_mutex do @registry.map { |h| h[:klass] } end end
Lazily load and return all hooks for the given request
@return [Array<Gruf::Hooks::Base>]
# 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 a hook from the registry
@param [Class] hook_class The hook class to remove @raise [HookNotFoundError] if the hook is not found
# 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
Add a hook to the registry
@param [Class] hook_class The class of the hook to add @param [Hash] options A hash of options to pass into the hook during initialization
# File lib/gruf/hooks/registry.rb, line 36 def use(hook_class, options = {}) hooks_mutex do @registry << { klass: hook_class, options: options } end end
Private Instance Methods
Handle mutations to the hook registry in a thread-safe manner
# File lib/gruf/hooks/registry.rb, line 150 def hooks_mutex(&block) @hooks_mutex ||= begin require 'monitor' Monitor.new end @hooks_mutex.synchronize(&block) end