pietern (owner)

Revisions

gist: 227363 Download_button fork
public
Public Clone URL: git://gist.github.com/227363.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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