Class: Telebugs::MiddlewareStack

Inherits:
Object
  • Object
show all
Defined in:
lib/telebugs/middleware_stack.rb

Overview

MiddlewareStack represents an ordered array of middleware.

A middleware is an object that responds to #call (typically a Proc or a class that implements the call method). The #call method must accept exactly one argument: the report object.

When you add a new middleware to the stack, it gets inserted according to its weight. Smaller weight means the middleware will be somewhere in the beginning of the array. Larger - in the end.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMiddlewareStack

Returns a new instance of MiddlewareStack.



16
17
18
# File 'lib/telebugs/middleware_stack.rb', line 16

def initialize
  @middlewares = []
end

Instance Attribute Details

#middlewaresObject (readonly)

Returns the value of attribute middlewares.



14
15
16
# File 'lib/telebugs/middleware_stack.rb', line 14

def middlewares
  @middlewares
end

Instance Method Details

#call(report) ⇒ Object



28
29
30
31
32
# File 'lib/telebugs/middleware_stack.rb', line 28

def call(report)
  @middlewares.each do |middleware|
    middleware.call(report)
  end
end

#delete(middleware_class) ⇒ Object



24
25
26
# File 'lib/telebugs/middleware_stack.rb', line 24

def delete(middleware_class)
  @middlewares.delete_if { |middleware| middleware.instance_of?(middleware_class) }
end

#use(new_middleware) ⇒ Object



20
21
22
# File 'lib/telebugs/middleware_stack.rb', line 20

def use(new_middleware)
  @middlewares = (@middlewares << new_middleware).sort_by(&:weight)
end