lib/resque_spec.rb in resque_spec-0.5.1 vs lib/resque_spec.rb in resque_spec-0.6.0

- old
+ new

@@ -1,8 +1,63 @@ -require 'resque_spec/resque_spec' -require 'resque_spec/resque_scheduler_spec' +require 'resque_spec/ext' require 'resque_spec/helpers' require 'resque_spec/matchers' + +module ResqueSpec + extend self + + attr_accessor :inline + + def dequeue(queue_name, klass, *args) + queue_by_name(queue_name).delete_if do |job| + job[:klass] == klass.to_s && args.empty? || job[:args] == args + end + end + + def enqueue(queue_name, klass, *args) + if inline + klass.send(:perform, *args) + else + queue_by_name(queue_name) << { :klass => klass.to_s, :args => args } + end + end + + def queue_by_name(name) + queues[name] + end + + def queue_for(klass) + queue_by_name(queue_name(klass)) + end + + def queue_name(klass) + if klass.is_a?(String) + klass = Kernel.const_get(klass) rescue nil + end + + name_from_instance_var(klass) or + name_from_queue_accessor(klass) or + raise ::Resque::NoQueueError.new("Jobs must be placed onto a queue.") + end + + def queues + @queues ||= Hash.new {|h,k| h[k] = []} + end + + def reset! + queues.clear + end + + private + + def name_from_instance_var(klass) + klass.instance_variable_get(:@queue) + end + + def name_from_queue_accessor(klass) + klass.respond_to?(:queue) and klass.queue + end +end config = RSpec.configuration config.include ResqueSpec::Helpers World(ResqueSpec::Helpers) if defined?(World)