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 37 37: def map(path, &block) 38: if @ins.last.kind_of? Hash 39: @ins.last[path] = Rack::Builder.new(&block).to_app 40: else 41: @ins << {} 42: map(path, &block) 43: end 44: end
# File lib/rack/builder.rb, line 33 33: def run(app) 34: @ins << app #lambda { |nothing| app } 35: end
# File lib/rack/builder.rb, line 46 46: def to_app 47: @ins[-1] = Rack::URLMap.new(@ins.last) if Hash === @ins.last 48: inner_app = @ins.last 49: @ins[0...-1].reverse.inject(inner_app) { |a, e| e.call(a) } 50: end