allow access to route params in stream block, fixes #418

This commit is contained in:
Konstantin Haase 2011-12-30 12:51:20 +01:00
parent 31a4f31569
commit 26cfcc2c23
2 changed files with 19 additions and 1 deletions

View File

@ -287,8 +287,18 @@ module Sinatra
# The close parameter specifies whether Stream#close should be called
# after the block has been executed. This is only relevant for evented
# servers like Thin or Rainbows.
def stream(keep_open = false, &block)
def stream(keep_open = false)
scheduler = env['async.callback'] ? EventMachine : Stream
current = @params.dup
block = proc do |out|
begin
original, @params = @params, current
yield(out)
ensure
@params = original if original
end
end
body Stream.new(scheduler, keep_open, &block)
end

View File

@ -112,4 +112,12 @@ class StreamingTest < Test::Unit::TestCase
stream = Stream.new { |out| out.callback { out.close }}
stream.each { |str| }
end
it 'gives access to route specific params' do
mock_app do
get('/:name') { stream { |o| o << params[:name] }}
end
get '/foo'
assert_body 'foo'
end
end