Skip to content

Instantly share code, notes, and snippets.

@ralphos
Forked from croaky/delayed_job_matcher.rb
Created March 26, 2014 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ralphos/9783074 to your computer and use it in GitHub Desktop.
Save ralphos/9783074 to your computer and use it in GitHub Desktop.
module DelayedJob
module Matchers
def enqueue_delayed_job(handler)
DelayedJobMatcher.new handler
end
class DelayedJobMatcher
def initialize(handler)
@handler = handler
@attributes = {}
@priority = 0
@failure_message = ''
end
def description
"enqueue a #{@handler} delayed job"
end
def failure_message
@failure_message || <<-message.strip_heredoc
Expected #{@handler} to be enqueued as a delayed job. Try:
Delayed::Job.enqueue #{@handler}.new
message
end
def matches?(subject)
@subject = subject
enqueued? && correct_attributes? && correct_priority?
end
def priority(priority)
@priority = priority
self
end
def with_attributes(attributes)
@attributes = attributes
self
end
private
def correct_attributes?
@attributes.each do |key, value|
payload_object.send(key).should == value
end
end
def correct_priority?
if @priority == job.priority
true
else
@failure_message = <<-message.strip_heredoc
Expected priority to be #{@priority} but was #{job.priority}"
message
false
end
end
def enqueued?
payload_object.kind_of? @handler.constantize
end
def job
Delayed::Job.last
end
def payload_object
job.payload_object
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment