Module: Resque::Serializable::Job

Included in:
Job
Defined in:
lib/resque-serializable/job.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
57
58
59
60
# File 'lib/resque-serializable/job.rb', line 5

def self.included(base)
    
  base.class_eval do
    
    # Alias Resque's Job.perform to Job.perform_after_deserializing
    alias_method :perform_after_deserializing, :perform
    
    # Deserializes serialized arguments from YAML.
    # Looks for an argument of type Hash with a key :serialize and deserializes its value.
    
    def perform
      args.collect! do |arg|
        if arg.is_a?(Hash) && arg.key?('serialize')
          if arg['serialize'].is_a?(Hash)
            arg['serialize'].values.collect{|a| YAML::load(a) }
          elsif arg['serialize'].is_a?(Array)
            arg['serialize'].collect{|a| YAML::load(a) }
          end
        else
          arg
        end
      end
      args.flatten!
      perform_after_deserializing
    end # perform
    
    class << self
      
      # Alias Resque's Job.create to Job.create_after_serializing
      alias_method :create_after_serializing, :create
      
      # Attempts to serialize job arguments before creating a new job, by looking for
      # an argument of type Hash with a key :serialize and serializing its value to YAML.
      #
      # @param [Symbol] Current Resque job's queue name
      # @param [Class] Resque job worker class
      # @param [Array] anonymous arguments passed to the Resque worker class. A hash argument with key :serialize will be serialized to YAML
      def create(queue, klass, *args)
        args.collect! do |arg|
          if arg.is_a?(Hash) && arg.key?(:serialize)
            if arg[:serialize].is_a?(Hash)
              arg[:serialize].each{|k,v| arg[:serialize][k] = v.to_yaml }
            elsif arg[:serialize].is_a?(Array)
              arg[:serialize].collect!{|a| a.to_yaml }
            end
          end
          arg
        end
        create_after_serializing(queue, klass, *args)
      end #create
  
    end #class << self
    
  end# base.class_eval

end