Class: Gruf::Interceptors::Registry

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

Overview

Handles registration of interceptors

Defined Under Namespace

Classes: InterceptorNotFoundError

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



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

def initialize
  @registry = []
end

Instance Method Details

#clearObject

Clear the registry



131
132
133
134
135
# File 'lib/gruf/interceptors/registry.rb', line 131

def clear
  interceptors_mutex do
    @registry = []
  end
end

#countInteger

Returns The number of interceptors currently loaded.

Returns:

  • (Integer)

    The number of interceptors currently loaded



140
141
142
143
144
145
# File 'lib/gruf/interceptors/registry.rb', line 140

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

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

Insert an interceptor after another specified interceptor

Parameters:

  • after_class (Class)

    The interceptor to insert after

  • interceptor_class (Class)

    The class of the interceptor to add

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

    A hash of options to pass into the interceptor during initialization

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/gruf/interceptors/registry.rb', line 87

def insert_after(after_class, interceptor_class, options = {})
  interceptors_mutex do
    pos = @registry.find_index { |opts| opts.fetch(:klass, '') == after_class }
    raise InterceptorNotFoundError if pos.nil?

    @registry.insert(
      (pos + 1),
      klass: interceptor_class,
      options: options
    )
  end
end

#insert_before(before_class, interceptor_class, options = {}) ⇒ Object

Insert an interceptor before another specified interceptor

Parameters:

  • before_class (Class)

    The interceptor to insert before

  • interceptor_class (Class)

    The class of the interceptor to add

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

    A hash of options to pass into the interceptor during initialization

Raises:



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gruf/interceptors/registry.rb', line 66

def insert_before(before_class, interceptor_class, options = {})
  interceptors_mutex do
    pos = @registry.find_index { |opts| opts.fetch(:klass, '') == before_class }
    raise InterceptorNotFoundError if pos.nil?

    @registry.insert(
      pos,
      klass: interceptor_class,
      options: options
    )
  end
end

#listArray<Class>

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

Returns:

  • (Array<Class>)


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

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

#prepare(request, error) ⇒ Array<Gruf::Interceptors::Base>

Load and return all interceptors for the given request

Parameters:

Returns:



118
119
120
121
122
123
124
125
126
# File 'lib/gruf/interceptors/registry.rb', line 118

def prepare(request, error)
  is = []
  interceptors_mutex do
    @registry.each do |o|
      is << o[:klass].new(request, error, o[:options])
    end
  end
  is
end

#remove(interceptor_class) ⇒ Object

Remove an interceptor from the registry

Parameters:

  • interceptor_class (Class)

    The interceptor class to remove

Raises:



51
52
53
54
55
56
# File 'lib/gruf/interceptors/registry.rb', line 51

def remove(interceptor_class)
  pos = @registry.find_index { |opts| opts.fetch(:klass, '') == interceptor_class }
  raise InterceptorNotFoundError if pos.nil?

  @registry.delete_at(pos)
end

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

Add an interceptor to the registry

Parameters:

  • interceptor_class (Class)

    The class of the interceptor to add

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

    A hash of options to pass into the interceptor during initialization



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

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