Class: BoltRb::Middleware::Chain

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

Overview

Executes a chain of middleware in order

The middleware chain follows the onion model where each middleware can execute code before and after the next middleware in the chain. This is similar to Rack middleware or Rails around_action filters.

Examples:

Basic usage

chain = Chain.new([LoggingMiddleware, AuthMiddleware])
chain.call(context) do
  # Handler code runs after all middleware have yielded
end

Instance Method Summary collapse

Constructor Details

#initialize(middleware_classes) ⇒ Chain

Creates a new middleware chain

Parameters:

  • middleware_classes (Array<Class>)

    Array of middleware classes to instantiate and run



20
21
22
# File 'lib/bolt_rb/middleware/chain.rb', line 20

def initialize(middleware_classes)
  @middleware_classes = middleware_classes
end

Instance Method Details

#call(context) { ... } ⇒ void

This method returns an undefined value.

Executes the middleware chain

Each middleware is instantiated and called in order. The provided block is called after all middleware have yielded. Middleware can stop the chain by not yielding.

Parameters:

Yields:

  • The handler to run after middleware processing



33
34
35
36
# File 'lib/bolt_rb/middleware/chain.rb', line 33

def call(context, &block)
  chain = @middleware_classes.map(&:new)
  run_chain(chain, context, &block)
end