Skip to content

Instantly share code, notes, and snippets.

@nevans
Created November 29, 2011 20:20
Show Gist options
  • Save nevans/1406295 to your computer and use it in GitHub Desktop.
Save nevans/1406295 to your computer and use it in GitHub Desktop.
Maybe a good way to make resque job definition just a tad simpler.
module SugaredResqueJob
def self.new(queue, resque=Resque, &perform)
Module.new do
extend self
define_method :queue do queue end
define_method :enqueue do |*args| resque.enqueue(self, *args) end
define_method :perform do |*args| perform.call( *args) end
end
end
end
# for mocking
resque = Object.new
SimpleJob = SugaredResqueJob.new(:queue_name, resque) do |*args|
"performing on *args: #{args.inspect}"
end
describe SimpleJob do
it "should enqueue job with Resque" do
resque.should_receive(:enqueue).with(SimpleJob, :arg1, :arg2, :arg3)
SimpleJob.enqueue(:arg1, :arg2, :arg3)
end
it "should assign job to the appropriate queue" do
SimpleJob.queue.should == :queue_name
end
it "should assign job with the appropriate receiver name" do
# "cheating" by using ruby's semi-magical Module#to_s constant lookup
SimpleJob.to_s.should == "SimpleJob"
end
it "should run the appropriate code when performed" do
result = SimpleJob.perform(:arg1, :arg2, :arg3)
result.should == "performing on *args: [:arg1, :arg2, :arg3]"
end
end
@pjb3
Copy link

pjb3 commented Nov 29, 2011

I like it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment