Class: BBServer::ExecutionStack

Inherits:
Object
  • Object
show all
Defined in:
lib/bbserver/execution_stack.rb

Instance Method Summary collapse

Constructor Details

#initialize(handler_class, middlewares) ⇒ ExecutionStack

Returns a new instance of ExecutionStack.



5
6
7
8
# File 'lib/bbserver/execution_stack.rb', line 5

def initialize(handler_class, middlewares)
  @handler_class = handler_class
  @middlewares = middlewares
end

Instance Method Details

#execute(context) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bbserver/execution_stack.rb', line 10

def execute(context)
  chain = @middlewares.dup
  
  executor = Class.new do
    def initialize(chain, handler_class, ctx)
      @chain = chain
      @handler_class = handler_class
      @ctx = ctx
    end

    def call
      if @chain.empty?
        handler = @handler_class.new(@ctx)
        handler.handle
      else
        middleware_class = @chain.shift.call
        middleware = middleware_class.new(@ctx, self)
        middleware.call
      end
    end
  end.new(chain, @handler_class, context)
  
  executor.call
end