Class: Gruf::Hooks::Registry

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

Overview

Handles registration of hooks

Defined Under Namespace

Classes: HookNotFoundError

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



26
27
28
# File 'lib/gruf/hooks/registry.rb', line 26

def initialize
  @registry = []
end

Instance Method Details

#clearObject

Clear the registry



129
130
131
132
133
# File 'lib/gruf/hooks/registry.rb', line 129

def clear
  hooks_mutex do
    @registry = []
  end
end

#countInteger

Returns The number of hooks currently loaded.

Returns:

  • (Integer)

    The number of hooks currently loaded



138
139
140
141
142
143
# File 'lib/gruf/hooks/registry.rb', line 138

def count
  hooks_mutex do
    @registry ||= []
    @registry.count
  end
end

#insert_after(after_class, hook_class, options = {}) ⇒ Object

Insert a hook after another specified hook

Parameters:

  • after_class (Class)

    The hook to insert after

  • hook_class (Class)

    The class of the hook to add

  • options (Hash) (defaults to: {})

    A hash of options to pass into the hook during initialization

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
# 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_before(before_class, hook_class, options = {}) ⇒ Object

Insert a hook before another specified hook

Parameters:

  • before_class (Class)

    The hook to insert before

  • hook_class (Class)

    The class of the hook to add

  • options (Hash) (defaults to: {})

    A hash of options to pass into the hook during initialization

Raises:



66
67
68
69
70
71
72
73
74
75
76
77
# 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

#listArray<Class>

Return a list of the hook classes in the registry in their execution order

Returns:

  • (Array<Class>)


105
106
107
108
109
# File 'lib/gruf/hooks/registry.rb', line 105

def list
  hooks_mutex do
    @registry.map { |h| h[:klass] }
  end
end

#prepareArray<Gruf::Hooks::Base>

Lazily load and return all hooks for the given request

Returns:



116
117
118
119
120
121
122
123
124
# 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(hook_class) ⇒ Object

Remove a hook from the registry

Parameters:

  • hook_class (Class)

    The hook class to remove

Raises:



51
52
53
54
55
56
# 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

#use(hook_class, options = {}) ⇒ Object

Add a hook to the registry

Parameters:

  • hook_class (Class)

    The class of the hook to add

  • options (Hash) (defaults to: {})

    A hash of options to pass into the hook during initialization



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

def use(hook_class, options = {})
  hooks_mutex do
    @registry << {
      klass: hook_class,
      options: options
    }
  end
end