refactor #invoke
This commit is contained in:
parent
75001c2966
commit
8f1c7bca96
|
@ -725,34 +725,15 @@ module Sinatra
|
||||||
# Run the block with 'throw :halt' support and apply result to the response.
|
# Run the block with 'throw :halt' support and apply result to the response.
|
||||||
def invoke
|
def invoke
|
||||||
res = catch(:halt) { yield }
|
res = catch(:halt) { yield }
|
||||||
return if res.nil?
|
res = [res] if Fixnum === res or String === res
|
||||||
|
if Array === res and Fixnum === res.first
|
||||||
case
|
raise TypeError, "#{res.inspect} not supported" if res.length > 3
|
||||||
when res.respond_to?(:to_str)
|
status(res.shift)
|
||||||
@response.body = [res]
|
body(res.pop)
|
||||||
when res.respond_to?(:to_ary)
|
headers(*res)
|
||||||
res = res.to_ary
|
elsif res.respond_to? :each
|
||||||
if Fixnum === res.first
|
body res
|
||||||
if res.length == 3
|
|
||||||
@response.status, headers, body = res
|
|
||||||
@response.body = body if body
|
|
||||||
headers.each { |k, v| @response.headers[k] = v } if headers
|
|
||||||
elsif res.length == 2
|
|
||||||
@response.status = res.first
|
|
||||||
@response.body = res.last
|
|
||||||
else
|
|
||||||
raise TypeError, "#{res.inspect} not supported"
|
|
||||||
end
|
|
||||||
else
|
|
||||||
@response.body = res
|
|
||||||
end
|
|
||||||
when res.respond_to?(:each)
|
|
||||||
@response.body = res
|
|
||||||
when (100..599) === res
|
|
||||||
@response.status = res
|
|
||||||
end
|
end
|
||||||
|
|
||||||
res
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Dispatch a request with error handling.
|
# Dispatch a request with error handling.
|
||||||
|
|
|
@ -221,9 +221,12 @@ class AfterFilterTest < Test::Unit::TestCase
|
||||||
count = 2
|
count = 2
|
||||||
base = Class.new(Sinatra::Base)
|
base = Class.new(Sinatra::Base)
|
||||||
base.after { count *= 2 }
|
base.after { count *= 2 }
|
||||||
mock_app(base) {
|
mock_app(base) do
|
||||||
get('/foo') { count += 2 }
|
get('/foo') do
|
||||||
}
|
count += 2
|
||||||
|
"ok"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
get '/foo'
|
get '/foo'
|
||||||
assert_equal 8, count
|
assert_equal 8, count
|
||||||
|
|
Loading…
Reference in New Issue