class Stacko::Builder

Public Class Methods

app(default_app = nil, &block) click to toggle source
# File lib/stacko/builder.rb, line 3
def self.app(default_app = nil, &block)
  self.new(default_app, &block).to_app
end
new(default_app = nil, &block) click to toggle source
# File lib/stacko/builder.rb, line 7
def initialize(default_app = nil, &block)
  @run = default_app
  @use = []
  @map = nil
  instance_eval(&block) if block_given?
end

Public Instance Methods

call(env) click to toggle source
# File lib/stacko/builder.rb, line 39
def call(env)
  to_app.call(env)
end
map(method_name, &block) click to toggle source
# File lib/stacko/builder.rb, line 27
def map(method_name, &block)
  @map ||= {}
  @map[method_name] = block
end
run(app) click to toggle source
# File lib/stacko/builder.rb, line 23
def run(app)
  @run = app
end
to_app() click to toggle source
# File lib/stacko/builder.rb, line 32
def to_app
  app = @map ? generate_map(@run, @map) : @run
  fail "missing run or map statement" unless app
  app = @use.reverse.inject(app) { |a,e| e[a] }
  app
end
use(middleware, *args, &block) click to toggle source
# File lib/stacko/builder.rb, line 14
def use(middleware, *args, &block)
  if @map
    mapping = @map
    @map = nil
    @use << proc { |app| generate_map(app, mapping) }
  end
  @use << proc { |app| middleware.new(app, *args, &block) }
end

Private Instance Methods

generate_map(default_app, mapping) click to toggle source
# File lib/stacko/builder.rb, line 45
def generate_map(default_app, mapping)
  mapped = default_app ? {:call => default_app} : {}
  mapping.each { |r,b| mapped[r] = self.class.new(default_app, &b) }
  MethodMap.new(mapped)
end