Skip to content
Browse files

refactor #invoke

  • Loading branch information...
1 parent 75001c2 commit 8f1c7bca96e12d51b1eb66df80fc4658c3bb8ebc @rkh rkh committed Jun 4, 2011
Showing with 14 additions and 30 deletions.
  1. +8 −27 lib/sinatra/base.rb
  2. +6 −3 test/filter_test.rb
View
35 lib/sinatra/base.rb
@@ -725,34 +725,15 @@ def indifferent_hash
# Run the block with 'throw :halt' support and apply result to the response.
def invoke
res = catch(:halt) { yield }
- return if res.nil?
-
- case
- when res.respond_to?(:to_str)
- @response.body = [res]
- when res.respond_to?(:to_ary)
- res = res.to_ary
- if Fixnum === res.first
- 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
+ res = [res] if Fixnum === res or String === res
+ if Array === res and Fixnum === res.first
+ raise TypeError, "#{res.inspect} not supported" if res.length > 3
+ status(res.shift)
+ body(res.pop)
+ headers(*res)
+ elsif res.respond_to? :each
+ body res
end
-
- res
end
# Dispatch a request with error handling.
View
9 test/filter_test.rb
@@ -221,9 +221,12 @@ class AfterFilterTest < Test::Unit::TestCase
count = 2
base = Class.new(Sinatra::Base)
base.after { count *= 2 }
- mock_app(base) {
- get('/foo') { count += 2 }
- }
+ mock_app(base) do
+ get('/foo') do
+ count += 2
+ "ok"
+ end
+ end
get '/foo'
assert_equal 8, count

0 comments on commit 8f1c7bc

Please sign in to comment.
Something went wrong with that request. Please try again.