Define routes as methods on Sinatra::Base...
This makes deciphering a stacktrace much easier RecordNotFound - No Post for id 3 ./x.rb:9:in `route { GET /posts/:id }' ./lib/sinatra/base.rb:366:in `send' ./lib/sinatra/base.rb:366:in `invoke' ./lib/sinatra/base.rb:362:in `catch' ...
This commit is contained in:
parent
59abc94454
commit
524f78b3f2
|
@ -326,7 +326,7 @@ module Sinatra
|
||||||
original_params = Hash.new{ |hash,k| hash[k.to_s] if Symbol === k }
|
original_params = Hash.new{ |hash,k| hash[k.to_s] if Symbol === k }
|
||||||
original_params.merge! @request.params
|
original_params.merge! @request.params
|
||||||
|
|
||||||
routes.each do |pattern, keys, conditions, block|
|
routes.each do |pattern, keys, conditions, method_name|
|
||||||
if pattern =~ path
|
if pattern =~ path
|
||||||
values = $~.captures.map{|val| val && unescape(val) }
|
values = $~.captures.map{|val| val && unescape(val) }
|
||||||
params =
|
params =
|
||||||
|
@ -350,7 +350,7 @@ module Sinatra
|
||||||
catch(:pass) {
|
catch(:pass) {
|
||||||
conditions.each { |cond|
|
conditions.each { |cond|
|
||||||
throw :pass if instance_eval(&cond) == false }
|
throw :pass if instance_eval(&cond) == false }
|
||||||
return invoke(block)
|
return invoke(method_name)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -358,8 +358,14 @@ module Sinatra
|
||||||
raise NotFound
|
raise NotFound
|
||||||
end
|
end
|
||||||
|
|
||||||
def invoke(block)
|
def invoke(handler)
|
||||||
res = catch(:halt) { instance_eval(&block) }
|
res = catch(:halt) {
|
||||||
|
if handler.respond_to?(:call)
|
||||||
|
instance_eval(&handler)
|
||||||
|
else
|
||||||
|
send(handler)
|
||||||
|
end
|
||||||
|
}
|
||||||
case
|
case
|
||||||
when res.respond_to?(:to_str)
|
when res.respond_to?(:to_str)
|
||||||
@response.body = [res]
|
@response.body = [res]
|
||||||
|
@ -528,10 +534,10 @@ module Sinatra
|
||||||
|
|
||||||
def get(path, opts={}, &block)
|
def get(path, opts={}, &block)
|
||||||
conditions = @conditions.dup
|
conditions = @conditions.dup
|
||||||
route 'GET', path, opts, &block
|
_, _, _, method_name = route('GET', path, opts, &block)
|
||||||
|
|
||||||
@conditions = conditions
|
@conditions = conditions
|
||||||
head(path, opts) { invoke(block) ; [] }
|
head(path, opts) { invoke(method_name) ; [] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def put(path, opts={}, &bk); route 'PUT', path, opts, &bk; end
|
def put(path, opts={}, &bk); route 'PUT', path, opts, &bk; end
|
||||||
|
@ -547,8 +553,12 @@ module Sinatra
|
||||||
|
|
||||||
pattern, keys = compile(path)
|
pattern, keys = compile(path)
|
||||||
conditions, @conditions = @conditions, []
|
conditions, @conditions = @conditions, []
|
||||||
|
method_name = "route { #{method} #{path} }"
|
||||||
|
|
||||||
|
define_method(method_name, &block)
|
||||||
|
|
||||||
(routes[method] ||= []).
|
(routes[method] ||= []).
|
||||||
push [pattern, keys, conditions, block]
|
push([pattern, keys, conditions, method_name]).last
|
||||||
end
|
end
|
||||||
|
|
||||||
def compile(path)
|
def compile(path)
|
||||||
|
|
Loading…
Reference in New Issue