Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Request#on_complete can hold multiple callbacks.

  • Loading branch information...
commit e85f1e3f7e715c58e65b45fdf9c029dca0fc3d1e 1 parent 4e084d4
@i0rek i0rek authored
View
12 lib/typhoeus/requests/callbacks.rb
@@ -13,15 +13,19 @@ module Callbacks
#
# @param [ Block ] block The block to execute.
def on_complete(&block)
- @on_complete = block
+ @on_complete ||= []
+ @on_complete << block if block_given?
+ @on_complete
end
- # Execute on_complete callback.
+ # Execute on_complete callbacks.
#
- # @example Execute on_complete.
+ # @example Execute on_completes.
# request.complete
def complete
- @on_complete.call(self) if defined?(@on_complete)
+ if defined?(@on_complete)
+ @on_complete.map{ |callback| callback.call(self) }
+ end
end
end
end
View
2  spec/typhoeus/hydras/easy_factory_spec.rb
@@ -40,7 +40,7 @@
end
it "runs requests complete callback" do
- request.instance_variable_set(:@on_complete, mock(:call))
+ request.instance_variable_set(:@on_complete, [mock(:call)])
easy_factory.set_callback
easy_factory.easy.complete
end
View
27 spec/typhoeus/requests/callbacks_spec.rb
@@ -4,18 +4,39 @@
let(:request) { Typhoeus::Request.new("fubar") }
describe "#on_complete" do
- it "responds to" do
+ it "responds" do
request.should respond_to(:on_complete)
end
+
+ context "when no block given" do
+ it "returns @on_complete" do
+ request.on_complete.should eq([])
+ end
+ end
+
+ context "when block given" do
+ it "stores" do
+ request.on_complete { p 1 }
+ request.instance_variable_get(:@on_complete).should have(1).items
+ end
+ end
+
+ context "when multiple blocks given" do
+ it "stores" do
+ request.on_complete { p 1 }
+ request.on_complete { p 2 }
+ request.instance_variable_get(:@on_complete).should have(2).items
+ end
+ end
end
describe "#complete" do
before do
request.on_complete {|r| String.new(r.url) }
- String.expects(:new).with(request.url)
end
- it "executes block and passes self" do
+ it "executes blocks and passes self" do
+ String.expects(:new).with(request.url)
request.complete
end
end
View
2  spec/typhoeus/requests/marshal_spec.rb
@@ -27,7 +27,7 @@
end
it "doesn't include #{name}" do
- loaded.send(name).should be_nil
+ loaded.instance_variable_get("@#{name}").should be_nil
end
end
end
View
2  spec/typhoeus/requests/operations_spec.rb
@@ -33,7 +33,7 @@
end
it "calls on_complete" do
- request.instance_variable_set(:@on_complete, mock(:call))
+ request.instance_variable_set(:@on_complete, [mock(:call)])
request.run
end
Please sign in to comment.
Something went wrong with that request. Please try again.