Class: Insika::MiddlewareStack

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

Overview

Rack-like composition: registration order = execution order (the first is the outermost link). It does NOT rescue (an exception propagates as a turn failure) nor does it have a special halt mechanism — the short-circuit is structural (the link does not call nxt).

Instance Method Summary collapse

Constructor Details

#initialize(middlewares = []) ⇒ MiddlewareStack

Returns a new instance of MiddlewareStack.



25
26
27
# File 'lib/insika/middleware.rb', line 25

def initialize(middlewares = [])
  @middlewares = middlewares
end

Instance Method Details

#call(state, &terminal) ⇒ Object



29
30
31
32
33
34
# File 'lib/insika/middleware.rb', line 29

def call(state, &terminal)
  chain = @middlewares.reverse.reduce(terminal) do |nxt, mw|
    proc { |s| mw.call(s, &nxt) }
  end
  chain.call(state)
end