Class: EndpointFlux::Config::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/endpoint_flux/config/middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass = nil, options = {}, &block) ⇒ Middleware

Returns a new instance of Middleware.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/endpoint_flux/config/middleware.rb', line 6

def initialize(klass = nil, options = {}, &block)
  if klass
    unless klass.methods.include?(:perform)
      raise "The [#{klass}] class should define perform class method"
    end
  else
    raise 'You must provide block or existing klass' unless block_given?
  end

  @klass   = klass
  @options = options
  @handler = block
end

Instance Attribute Details

#handlerObject

Returns the value of attribute handler.



4
5
6
# File 'lib/endpoint_flux/config/middleware.rb', line 4

def handler
  @handler
end

#klassObject

Returns the value of attribute klass.



4
5
6
# File 'lib/endpoint_flux/config/middleware.rb', line 4

def klass
  @klass
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/endpoint_flux/config/middleware.rb', line 4

def options
  @options
end

Instance Method Details

#==(other) ⇒ Object



30
31
32
33
34
35
# File 'lib/endpoint_flux/config/middleware.rb', line 30

def ==(other)
  return true if klass && klass == other.klass
  return true if handler && handler == other.handler

  false
end

#run(attrs) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/endpoint_flux/config/middleware.rb', line 20

def run(attrs)
  if klass
    klass.perform(*attrs, options, &handler)
  elsif handler
    handler.call(*attrs, options)
  else
    raise "Unknown middleware type [#{inspect}]"
  end
end