lib/ethon/easies/response_callbacks.rb in ethon-0.1.0 vs lib/ethon/easies/response_callbacks.rb in ethon-0.2.0
- old
+ new
@@ -1,25 +1,48 @@
module Ethon
module Easies
# This module contains the logic for the response callbacks.
# The on_complete callback is the only one at the moment.
+ #
+ # You can set multiple callbacks, which are then executed
+ # in the same order.
+ #
+ # easy.on_complete { p 1 }
+ # easy.on_complete { p 2 }
+ # easy.complete
+ # #=> 1
+ # #=> 2
+ #
+ # You can clear the callbacks:
+ #
+ # easy.on_complete { p 1 }
+ # easy.on_complete { p 2 }
+ # easy.on_complete.clear
+ # easy.on_complete
+ # #=> []
module ResponseCallbacks
- # Execute preset complete callback.
+
+ # Set on_complete callback.
#
- # @example Execute complete callback.
- # easy.complete
- def complete
- return if !defined?(@complete) || @complete.nil?
- @complete.call(self)
+ # @example Set on_complete.
+ # request.on_complete { p "yay" }
+ #
+ # @param [ Block ] block The block to execute.
+ def on_complete(&block)
+ @on_complete ||= []
+ @on_complete << block if block_given?
+ @on_complete
end
- # Set complete callback.
+ # Execute on_complete callbacks.
#
- # @example Set complete callback.
- # easy.on_complete = block
- def on_complete(&block)
- @complete = block
+ # @example Execute on_completes.
+ # request.complete
+ def complete
+ if defined?(@on_complete)
+ @on_complete.map{ |callback| callback.call(self) }
+ end
end
end
end
end