Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
make dsl work with raw rack apps and also rack web frameworks
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerry Cheung committed May 25, 2012
1 parent 7072808 commit ed7b959
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
26 changes: 23 additions & 3 deletions lib/rack/stream/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Rack
class Stream
# DSL to access Rack::Stream::App methods
# DSL to access Rack::Stream::App methods.
#
# ## Example
# ```ruby
Expand All @@ -23,15 +23,35 @@ class Stream
#
# run App.new
# ```
#
# ## Rack Frameworks
#
# If you mix this module into a class that already responds to `#call`,
# then you need to make `env` available so that methods can be delegated to
# `env['rack.stream']`. There is no need to declare a `stream` block in this case.
#
# For example, Sinatra makes `env` available to its endpoints:
#
# ```ruby
# class App < Sinatra::Base
# include Rack::Stream::DSL
#
# get '/' do
# chunk "Hello" # no need to declare stream block b/c `env` is available
# end
# end
# ```
module DSL
def self.included(base)
base.extend ClassMethods
base.extend Forwardable

base.class_eval do
include InstanceMethods
unless base.respond_to? :call
include InstanceMethods
attr_reader :env
end

attr_reader :env
def_delegators :"env['rack.stream']", :after_open, :before_chunk, :chunk, :after_chunk, :before_close, :close, :after_close, :stream_transport
end
end
Expand Down
52 changes: 37 additions & 15 deletions spec/lib/rack/stream/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,55 @@
EM.stop # TODO: would prefer to have only stream_spec require em
end

context '#call' do
it 'should declare #call' do
subject.respond_to?(:call).should be_true
context 'raw rack' do
context '#call' do
it 'should declare #call' do
subject.respond_to?(:call).should be_true
end

it 'should raise error if no run block is defined' do
expect {
subject.call(mock)
}.to raise_error(Rack::Stream::DSL::StreamBlockNotDefined)
end
end

it 'should raise error if no run block is defined' do
expect {
context '.run' do
subject do
Class.new {
include Rack::Stream::DSL

attr_reader :run_called
stream do
@run_called = true
end
}.new
end

it 'should eval run block for new requests' do
subject.call(mock)
}.to raise_error(Rack::Stream::DSL::StreamBlockNotDefined)
subject.run_called.should be_true
end
end
end

context '.run' do
context 'rack framework' do
subject do
Class.new {
include Rack::Stream::DSL

attr_reader :run_called
stream do
@run_called = true
# mock of a rack web framework that already knows call
def call(env)
end

include Rack::Stream::DSL
}.new
end

it 'should eval run block for new requests' do
subject.call(mock)
subject.run_called.should be_true
context '#call' do
it 'should raise error if no run block is defined' do
expect {
subject.call(mock)
}.to_not raise_error(Rack::Stream::DSL::StreamBlockNotDefined)
end
end
end
end

0 comments on commit ed7b959

Please sign in to comment.