mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
allow access to route params in stream block, fixes #418
This commit is contained in:
parent
31a4f31569
commit
26cfcc2c23
2 changed files with 19 additions and 1 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue