Sha256: ec16b3bd204636714ddd1da1c1b206189a6fc9cad43783c74ee7a39e4175101a

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

# Volt::MiddlewareStack provides an interface where app code can add custom
# rack middleware.  Volt.current_app.middleware returns an instance of
# Volt::MiddlewareStack, and apps can call #use to add in more middleware.

module Volt
  class MiddlewareStack
    attr_reader :middlewares

    def initialize
      # Setup the next app
      @middlewares = []
      @maps = []
    end

    # Set the app that gets called after the middleware runs
    # def set_app(app)
    #   @app = app
    # end

    def use(*args, &block)
      @middlewares << [args, block]

      # invalidate builder, so it gets built again
      @builder = nil
    end

    def map(path, &block)
      @maps << [path, block]
    end

    def run(app)
      @app = app
    end

    # Builds a new Rack::Builder with the middleware and the app
    def build
      @builder = Rack::Builder.new

      @maps.each do |path, block|
        @builder.map(path, &block)
      end

      @middlewares.each do |middleware|
        @builder.use(*middleware[0], &middleware[1])
      end

      @builder.run(@app)
    end

    def call(env)
      unless @builder
        build
      end

      @builder.call(env)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
volt-0.9.3 lib/volt/server/middleware/middleware_stack.rb
volt-0.9.3.pre6 lib/volt/server/middleware/middleware_stack.rb
volt-0.9.3.pre5 lib/volt/server/middleware/middleware_stack.rb
volt-0.9.3.pre4 lib/volt/server/middleware/middleware_stack.rb