File: message_sending.rb

package info (click to toggle)
ruby-delayed-job 4.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 388 kB
  • sloc: ruby: 2,780; makefile: 8
file content (74 lines) | stat: -rw-r--r-- 2,371 bytes parent folder | download | duplicates (2)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
module Delayed
  class DelayProxy < BasicObject
    # What additional methods exist on BasicObject has changed over time
    (::BasicObject.instance_methods - [:__id__, :__send__, :instance_eval, :instance_exec]).each do |method|
      undef_method method
    end

    # Let DelayProxy raise exceptions.
    def raise(*args)
      ::Object.send(:raise, *args)
    end

    def initialize(payload_class, target, options)
      @payload_class = payload_class
      @target = target
      @options = options
    end

    # rubocop:disable MethodMissing
    def method_missing(method, *args)
      Job.enqueue({:payload_object => @payload_class.new(@target, method.to_sym, args)}.merge(@options))
    end
    # rubocop:enable MethodMissing
  end

  module MessageSending
    def delay(options = {})
      DelayProxy.new(PerformableMethod, self, options)
    end
    alias_method :__delay__, :delay

    def send_later(method, *args)
      warn '[DEPRECATION] `object.send_later(:method)` is deprecated. Use `object.delay.method'
      __delay__.__send__(method, *args)
    end

    def send_at(time, method, *args)
      warn '[DEPRECATION] `object.send_at(time, :method)` is deprecated. Use `object.delay(:run_at => time).method'
      __delay__(:run_at => time).__send__(method, *args)
    end
  end

  module MessageSendingClassMethods
    def handle_asynchronously(method, opts = {}) # rubocop:disable PerceivedComplexity
      aliased_method = method.to_s.sub(/([?!=])$/, '')
      punctuation = $1 # rubocop:disable PerlBackrefs
      with_method = "#{aliased_method}_with_delay#{punctuation}"
      without_method = "#{aliased_method}_without_delay#{punctuation}"
      define_method(with_method) do |*args|
        curr_opts = opts.clone
        curr_opts.each_key do |key|
          next unless (val = curr_opts[key]).is_a?(Proc)
          curr_opts[key] = if val.arity == 1
            val.call(self)
          else
            val.call
          end
        end
        delay(curr_opts).__send__(without_method, *args)
      end

      alias_method without_method, method
      alias_method method, with_method

      if public_method_defined?(without_method)
        public method
      elsif protected_method_defined?(without_method)
        protected method
      elsif private_method_defined?(without_method)
        private method
      end
    end
  end
end