Class: Gruf::Hooks::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/gruf/hooks/registry.rb

Overview

Registry of all hooks added

Defined Under Namespace

Classes: HookDescendantError

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Gruf::Instrumentation::Base|NilClass

Return a hook via a hash accessor syntax

Returns:



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

Parameters:

  • name (String)

    The name to represent the hook as

  • hook (Gruf::Hooks::Base|NilClass) (defaults to: nil)

    The strategy class to add. If nil, will expect

Returns:

  • (Class)

    The hook that was added

Raises:

  • (NoMethodError)


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

Returns:

  • (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

.clearHash

Returns Clear all existing hooks from the registry.

Returns:

  • (Hash)

    Clear all existing hooks from the registry



95
96
97
# File 'lib/gruf/hooks/registry.rb', line 95

def clear
  @_registry = {}
end

.countInteger

Return the number of registered hooks

Returns:

  • (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

.eachObject

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_hHash<Class>

Return the registry represented as a Hash object

Returns:

  • (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