lib/basket/handle_add.rb in basket-0.0.3 vs lib/basket/handle_add.rb in basket-0.0.4
- old
+ new
@@ -5,45 +5,60 @@
end
def initialize(queue, data)
@queue = queue
@data = data
- @queue_collection = Basket.queue_collection
end
- def call(data = @data)
- queue_length = @queue_collection.push(@queue, data)
- queue_class = class_for_queue
- queue_instance = queue_class.new
+ def call
+ setup_batchers
+ add_to_basket
+ perform if basket_full?(@queue_length, @queue_class)
+ rescue => error
+ maybe_raise_basket_error(error)
+ failure(error)
+ end
- queue_instance.define_singleton_method(:element) { data }
- queue_instance.on_add
+ private
- return unless basket_full?(queue_length, queue_class)
+ def setup_batchers
+ @queue_collection = Basket.queue_collection
+ @queue_class = class_for_queue
+ @queue_instance = @queue_class.new
+ end
- queue_instance.perform
- queue_instance.on_success
+ def add_to_basket(data = @data)
+ @queue_length = @queue_collection.push(@queue, data)
+ @queue_instance.define_singleton_method(:element) { data }
+ @queue_instance.on_add
+ end
+
+ def perform
+ @queue_instance.perform
+ @queue_instance.on_success
@queue_collection.clear(@queue)
- rescue => e
- raise e if basket_error?(e)
+ end
- queue_instance.define_singleton_method(:error) { e }
- queue_instance.on_failure
+ def failure(error)
+ @queue_instance.define_singleton_method(:error) { error }
+ @queue_instance.on_failure
end
- private
-
def class_for_queue
+ raise_basket_not_found unless Object.const_defined?(@queue)
Object.const_get(@queue)
- rescue NameError => e
- raise Basket::BasketNotFoundError, "We couldn't find that basket anywhere, please make sure it is defined. | #{e.message}"
end
+ def raise_basket_not_found
+ raise Basket::BasketNotFoundError, "We couldn't find that basket anywhere, please make sure it is defined."
+ end
+
def basket_full?(queue_length, queue_class)
queue_length == queue_class.basket_options_hash[:size]
end
- def basket_error?(e)
- e.instance_of?(Basket::Error) || e.instance_of?(Basket::BasketNotFoundError)
+ def maybe_raise_basket_error(e)
+ raise e if e.instance_of?(Basket::Error)
+ raise e if e.instance_of?(Basket::BasketNotFoundError)
end
end
end