Class: Roda::RodaPlugins::MiddlewareStack::Stack

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

Overview

Represents the applications middleware as a stack, allowing for easily removing middleware or finding places to insert new middleware.

Instance Method Summary collapse

Constructor Details

#initialize(app, middleware) ⇒ Stack

Returns a new instance of Stack.



42
43
44
45
# File 'lib/roda/plugins/middleware_stack.rb', line 42

def initialize(app, middleware)
  @app = app
  @middleware = middleware
end

Instance Method Details

#after(&block) ⇒ Object

Return a StackPosition representing the position after the middleware where the block returns true. Yields the middleware and any middleware arguments given, but not the middleware block. It the block never returns true, returns a StackPosition that will insert new middleware at the end of the stack.



52
53
54
# File 'lib/roda/plugins/middleware_stack.rb', line 52

def after(&block)
  handle(1, &block)
end

#before(&block) ⇒ Object

Return a StackPosition representing the position before the middleware where the block returns true. Yields the middleware and any middleware arguments given, but not the middleware block. It the block never returns true, returns a StackPosition that will insert new middleware at the end of the stack.



61
62
63
# File 'lib/roda/plugins/middleware_stack.rb', line 61

def before(&block)
  handle(0, &block)
end

#removeObject

Removes any middleware where the block returns true. Yields the middleware and any middleware arguments given, but not the middleware block



67
68
69
70
71
72
73
# File 'lib/roda/plugins/middleware_stack.rb', line 67

def remove
  @middleware.delete_if do |m, _|
    yield(*m)
  end
  @app.send(:build_rack_app)
  nil
end