Skip to content
Browse files

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

  • Loading branch information...
1 parent 31a4f31 commit 26cfcc2c233b9fa369ddf3001340bceb4416843e @rkh rkh committed Dec 30, 2011
Showing with 19 additions and 1 deletion.
  1. +11 −1 lib/sinatra/base.rb
  2. +8 −0 test/streaming_test.rb
View
12 lib/sinatra/base.rb
@@ -287,8 +287,18 @@ def callback(&block)
# 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
8 test/streaming_test.rb
@@ -112,4 +112,12 @@ def defer!(*) @defer.pop.call until @defer.empty? end
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

0 comments on commit 26cfcc2

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