lib/backgrounded/handler/workling_handler.rb in backgrounded-0.6.2 vs lib/backgrounded/handler/workling_handler.rb in backgrounded-0.7.0
- old
+ new
@@ -1,17 +1,33 @@
class Backgrounded::Handler::WorklingHandler
+ class BackgroundedWorker < Workling::Base
+ def perform(options = {})
+ find_instance(options[:class], options[:id], options[:method]).send(options[:method], *options[:params])
+ end
+
+ private
+ def find_instance(clazz, id, method)
+ clazz = clazz.constantize
+ clazz.respond_to?(method) ? clazz : clazz.find(id)
+ end
+ end
+
def request(object, method, *args)
+ instance, id = instance_identifiers(object)
options = {
- :class => object.class.name,
- :id => object.id,
+ :class => instance,
+ :id => id,
:method => method,
:params => args
}
BackgroundedWorker.async_perform options
end
- class BackgroundedWorker < Workling::Base
- def perform(options = {})
- options[:class].constantize.find(options[:id]).send(options[:method], *options[:params])
+ private
+ def instance_identifiers(object)
+ instance, id = if object.is_a?(Class)
+ [object.name, -1]
+ else
+ [object.class.name, object.id]
end
end
end