Sha256: 210e33ee57ee92368e71c198c54f3d7795e8fdb5073f9e3db27b3bc81c91d715

Contents?: true

Size: 1.27 KB

Versions: 28

Compression:

Stored size: 1.27 KB

Contents

module Rack
  # Rack::Builder implements a small DSL to iteratively construct Rack
  # applications.
  #
  # Example:
  #
  #  app = Rack::Builder.new {
  #    use Rack::CommonLogger
  #    use Rack::ShowExceptions
  #    map "/lobster" do
  #      use Rack::Lint
  #      run Rack::Lobster.new
  #    end
  #  }
  #
  # +use+ adds a middleware to the stack, +run+ dispatches to an application.
  # You can use +map+ to construct a Rack::URLMap in a convenient way.

  class Builder
    def initialize(&block)
      @ins = []
      instance_eval(&block) if block_given?
    end

    def use(middleware, *args, &block)
      @ins << if block_given?
        lambda { |app| middleware.new(app, *args, &block) }
      else
        lambda { |app| middleware.new(app, *args) }
      end
    end

    def run(app)
      @ins << app #lambda { |nothing| app }
    end

    def map(path, &block)
      if @ins.last.kind_of? Hash
        @ins.last[path] = Rack::Builder.new(&block).to_app
      else
        @ins << {}
        map(path, &block)
      end
    end

    def to_app
      @ins[-1] = Rack::URLMap.new(@ins.last)  if Hash === @ins.last
      inner_app = @ins.last
      @ins[0...-1].reverse.inject(inner_app) { |a, e| e.call(a) }
    end

    def call(env)
      to_app.call(env)
    end
  end
end

Version data entries

28 entries across 28 versions & 6 rubygems

Version Path
merb-core-1.1.3 spec10/public/webrat/test_app/gems/gems/rack-0.4.0/lib/rack/builder.rb
merb-core-1.1.2 spec10/public/webrat/test_app/gems/gems/rack-0.4.0/lib/rack/builder.rb
merb-core-1.1.1 spec10/public/webrat/test_app/gems/gems/rack-0.4.0/lib/rack/builder.rb
merb-core-1.1.0 spec10/public/webrat/test_app/gems/gems/rack-0.4.0/lib/rack/builder.rb
merb-core-1.1.0.rc1 spec10/public/webrat/test_app/gems/gems/rack-0.4.0/lib/rack/builder.rb
merb-core-1.1.0.pre spec10/public/webrat/test_app/gems/gems/rack-0.4.0/lib/rack/builder.rb
mack-0.8.2 lib/gems/rack-0.4.0/lib/rack/builder.rb
rack-0.4.0 lib/rack/builder.rb