Sha256: df4b394aa8688cd0c17a013d477231d0bf7e96e48b0a1fb7b8e71d9f521603fa

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require 'resque'

module ResqueSpec
  extend self

  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

  module Resque
    extend self

    def reset!
      ResqueSpec.reset!
    end

    module Job
      extend self

      def self.included(base)
        base.instance_eval do

          def create(queue, klass, *args)
            raise ::Resque::NoQueueError.new("Jobs must be placed onto a queue.") if !queue
            raise ::Resque::NoClassError.new("Jobs must be given a class.") if klass.to_s.empty?
            ResqueSpec.queues[queue] << {:klass => klass.to_s, :args => args}
          end

          def destroy(queue, klass, *args)
            raise ::Resque::NoQueueError.new("Jobs must have been placed onto a queue.") if !queue
            raise ::Resque::NoClassError.new("Jobs must have been given a class.") if klass.to_s.empty?

            old_count = ResqueSpec.queues[queue].size

            if args.empty?
              ResqueSpec.queues[queue].delete_if{ |job| job[:klass] == klass.to_s }
            else
              ResqueSpec.queues[queue].delete_if{ |job| job[:klass] == klass.to_s and job[:args].to_a == args.to_a }
            end
            old_count - ResqueSpec.queues[queue].size
          end

        end
      end

    end

  end
end

Resque.extend ResqueSpec::Resque
Resque::Job.send :include, ResqueSpec::Resque::Job

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
resque_spec-0.5.1 lib/resque_spec/resque_spec.rb