| Class | Rack::Builder |
| In: |
lib/rack/builder.rb
|
| Parent: | Object |
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.
# File lib/rack/builder.rb, line 20
20: def initialize(&block)
21: @ins = []
22: instance_eval(&block)
23: end
# File lib/rack/builder.rb, line 33
33: def map(path, &block)
34: if @ins.last.kind_of? Hash
35: @ins.last[path] = Rack::Builder.new(&block).to_app
36: else
37: @ins << {}
38: map(path, &block)
39: end
40: end
# File lib/rack/builder.rb, line 29
29: def run(app)
30: @ins << app #lambda { |nothing| app }
31: end
# File lib/rack/builder.rb, line 42
42: def to_app
43: @ins[-1] = Rack::URLMap.new(@ins.last) if Hash === @ins.last
44: inner_app = @ins.pop
45: @ins.reverse.inject(inner_app) { |a, e| e.call(a) }
46: end