Skip to content

Instantly share code, notes, and snippets.

@pietern
Created November 5, 2009 20:47
Show Gist options
  • Save pietern/227363 to your computer and use it in GitHub Desktop.
Save pietern/227363 to your computer and use it in GitHub Desktop.
module Resque
class Payload
def initialize(klass, &blk)
@__klass = klass
@__calls = []
instance_eval &blk
end
def method_missing(method, *args)
@__calls << { :method => method, :args => args }
end
def to_resque
{ :class => @__klass.to_s, :call => @__calls }
end
end
end
module Resque
class Job
def self.enqueue(klass, *args, &blk)
p = if block_given?
Payload.new(klass, &blk)
else
Payload.new(klass) do
perform *args
end
end
p.to_resque
end
def perform
payload['call'].each do |obj|
method, args = obj['method'], obj['args']
payload_class.send(method, *args)
end
end
end
end
=begin
require 'lib/resque/payload'
Resque::Job.enqueue(String, :method, 'arg1')
=> {:call=>[{:method=>:perform, :args=>[:method, "arg1"]}], :class=>"String"}
Resque::Job.enqueue(String) do
action 'arg1', 'arg2'
end
=> {:call=>[{:method=>:action, :args=>["arg1", "arg2"]}], :class=>"String"}
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment