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) ⇒ Object

Return a strategy type registry via a hash accessor syntax



52
53
54
# File 'lib/gruf/hooks/registry.rb', line 52

def [](name)
  _registry[name.to_sym]
end

.add(name, hook = nil, &block) ⇒ Class

Add an authentication strategy, either through a class or a block

Parameters:

Returns:

  • (Class)

Raises:

  • (NoMethodError)


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gruf/hooks/registry.rb', line 36

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

Returns:

  • (Boolean)


75
76
77
# File 'lib/gruf/hooks/registry.rb', line 75

def any?
  count > 0
end

.clearHash

Returns:

  • (Hash)


89
90
91
# File 'lib/gruf/hooks/registry.rb', line 89

def clear
  @_registry = {}
end

.countInteger

Returns:

  • (Integer)


82
83
84
# File 'lib/gruf/hooks/registry.rb', line 82

def count
  to_h.keys.count
end

.eachObject

Iterate over each hook in the registry



59
60
61
62
63
# File 'lib/gruf/hooks/registry.rb', line 59

def each
  _registry.each do |name, s|
    yield name, s
  end
end

.to_hHash<Class>

Returns:

  • (Hash<Class>)


68
69
70
# File 'lib/gruf/hooks/registry.rb', line 68

def to_h
  _registry
end