class Roda::RodaPlugins::MiddlewareStack::Stack
Represents the applications middleware as a stack, allowing for easily removing middleware or finding places to insert new middleware.
Public Class Methods
Source
# File lib/roda/plugins/middleware_stack.rb, line 42 def initialize(app, middleware) @app = app @middleware = middleware end
Public Instance Methods
Source
# File lib/roda/plugins/middleware_stack.rb, line 52 def after(&block) handle(1, &block) end
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.
Source
# File lib/roda/plugins/middleware_stack.rb, line 61 def before(&block) handle(0, &block) end
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.
Source
# 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
Removes any middleware where the block returns true. Yields the middleware and any middleware arguments given, but not the middleware block
Private Instance Methods
Source
# File lib/roda/plugins/middleware_stack.rb, line 78 def handle(offset) @middleware.each_with_index do |(m, _), i| if yield(*m) return StackPosition.new(@app, @middleware, i+offset) end end StackPosition.new(@app, @middleware, @middleware.length) end
Internals of before and after.