Skip to content

Commit

Permalink
Fixed RequestBuilderDelegator to delegate calls with blocks correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
tszolar committed Jul 3, 2014
1 parent acfccc1 commit a8dfd16
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/kosapi_client/request_builder_delegator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ def initialize(request_builder)

alias :super_method_missing :method_missing

def method_missing(method, *args)
def method_missing(method, *args, &block)
if @response
delegate_to_response(method, *args)
delegate_to_response(method, *args, &block)
else
delegate_to_builder(method, *args)
delegate_to_builder(method, *args, &block)
end
end

Expand All @@ -25,17 +25,17 @@ def respond_to_missing?(method, include_all)
end

private
def delegate_to_response(method, *args)
def delegate_to_response(method, *args, &block)
if @response.respond_to?(method)
@response.send(method, *args)
@response.send(method, *args, &block)
else
super_method_missing(method, *args)
end
end

def delegate_to_builder(method, *args)
def delegate_to_builder(method, *args, &block)
if @request_builder.respond_to?(method)
res = @request_builder.send(method, *args)
res = @request_builder.send(method, *args, &block)
if res.equal?(@request_builder)
self
else
Expand All @@ -44,7 +44,7 @@ def delegate_to_builder(method, *args)
else
@request_builder.finalize
@response = @request_builder.response
delegate_to_response(method, *args)
delegate_to_response(method, *args, &block)
end
end

Expand Down
17 changes: 17 additions & 0 deletions spec/kosapi_client/request_builder_delegator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

let(:response) { double(:response) }
let(:builder) { double(:builder, response: response, finalize: nil) }
let(:block) { -> { 1 + 1 } }
subject(:delegator) { KOSapiClient::RequestBuilderDelegator.new(builder) }

describe '#method_missing' do
Expand Down Expand Up @@ -35,6 +36,22 @@
expect(delegator.foo).to be delegator
end

it 'delegates call with block to builder' do
allow(builder).to receive(:foo).with(:x) do |arg, &block|
expect(block).not_to be_nil
end

delegator.foo(:x, &block)
end

it 'delegates call with block to response' do
allow(response).to receive(:foo).with(:x) do |arg, &block|
expect(block).not_to be_nil
end

delegator.foo(:x, &block)
end

end

describe '#respond_to_missing?' do
Expand Down

0 comments on commit a8dfd16

Please sign in to comment.