Parent

Class Index [+]

Quicksearch

Rack::Builder

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
 }

Or

 app = Rack::Builder.app do
   use Rack::CommonLogger
   lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'OK'] }
 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.

Public Class Methods

app(&block) click to toggle source
    # File lib/rack/builder.rb, line 49
49:     def self.app(&block)
50:       self.new(&block).to_app
51:     end
new(&block) click to toggle source
    # File lib/rack/builder.rb, line 44
44:     def initialize(&block)
45:       @ins = []
46:       instance_eval(&block) if block_given?
47:     end
parse_file(config, opts = Server::Options.new) click to toggle source
    # File lib/rack/builder.rb, line 27
27:     def self.parse_file(config, opts = Server::Options.new)
28:       options = {}
29:       if config =~ /\.ru$/
30:         cfgfile = ::File.read(config)
31:         if cfgfile[/^#\\(.*)/] && opts
32:           options = opts.parse! $1.split(/\s+/)
33:         end
34:         cfgfile.sub!(/^__END__\n.*/, '')
35:         app = eval "Rack::Builder.new {( " + cfgfile + "\n )}.to_app",
36:           TOPLEVEL_BINDING, config
37:       else
38:         require config
39:         app = Object.const_get(::File.basename(config, '.rb').capitalize)
40:       end
41:       return app, options
42:     end

Public Instance Methods

call(env) click to toggle source
    # File lib/rack/builder.rb, line 76
76:     def call(env)
77:       to_app.call(env)
78:     end
map(path, &block) click to toggle source
    # File lib/rack/builder.rb, line 61
61:     def map(path, &block)
62:       if @ins.last.kind_of? Hash
63:         @ins.last[path] = self.class.new(&block).to_app
64:       else
65:         @ins << {}
66:         map(path, &block)
67:       end
68:     end
run(app) click to toggle source
    # File lib/rack/builder.rb, line 57
57:     def run(app)
58:       @ins << app #lambda { |nothing| app }
59:     end
to_app() click to toggle source
    # File lib/rack/builder.rb, line 70
70:     def to_app
71:       @ins[1] = Rack::URLMap.new(@ins.last)  if Hash === @ins.last
72:       inner_app = @ins.last
73:       @ins[0...1].reverse.inject(inner_app) { |a, e| e.call(a) }
74:     end
use(middleware, *args, &block) click to toggle source
    # File lib/rack/builder.rb, line 53
53:     def use(middleware, *args, &block)
54:       @ins << lambda { |app| middleware.new(app, *args, &block) }
55:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.