app.rb
module Hook
def self.included(klass)
klass.extend DSLMethod
end
def initialize(app = nil)
@app = app
end
def call(env)
path = env["PATH_INFO"] ||= "/"
puts self.class
["200", { "Content-Type" => "text/html" }, ["Hello, %s via %s" % [path,self.class]]]
end
module DSLMethod
def self.extended(klass)
klass.instance_exec do
@builder = Rack::Builder.new
@builder.run(klass.new)
end
end
def use(*args, &block)
@builder.use(*args, &block)
end
def map(*args, &block)
print "maped ", args
@builder.map(*args, &block)
end
end
end
class Admin
include Hook
end
class Main
include Hook
map "/admin" do
use Admin
run Admin.new
end
end
config.ru
require './app.rb'
run Main.new
想实现以 admin 开始的都由 class Admin 来处理 但是实际上都是 class Main 处理,Rack::Builder.map 不起作用