Module: Delayed::MessageSending::ClassMethods

Defined in:
lib/delayed/message_sending.rb

Instance Method Summary collapse

Instance Method Details

#add_send_later_methods(method_name, enqueue_args = {}, default_async = false) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/delayed/message_sending.rb', line 101

def add_send_later_methods(method_name, enqueue_args={}, default_async=false)
  aliased_method, punctuation = method_name.to_s.sub(/([?!=])$/, ''), $1

  # we still need this for backwards compatibility
  without_method = "#{aliased_method}_without_send_later#{punctuation}"

  if public_method_defined?(method_name)
    visibility = :public
  elsif private_method_defined?(method_name)
    visibility = :private
  else
    visibility = :protected
  end

  if has_kwargs? method_name
    generated_delayed_methods.class_eval do
      define_method without_method do |*args, **kwargs|
        send(method_name, *args, synchronous: true, **kwargs)
      end

      define_method(method_name, -> (*args, synchronous: !default_async, **kwargs) do
        if synchronous
          super(*args, **kwargs)
        else
          send_later_enqueue_args(method_name, enqueue_args, *args, synchronous: true, **kwargs)
        end
      end)
    end
  else
    generated_delayed_methods.class_eval do
      define_method without_method do |*args|
        send(method_name, *args, synchronous: true)
      end

      define_method(method_name, -> (*args, synchronous: !default_async) do
        if synchronous
          super(*args)
        else
          send_later_enqueue_args(method_name, enqueue_args, *args, synchronous: true)
        end
      end)
    end
  end
  generated_delayed_methods.send(visibility, without_method)
  generated_delayed_methods.send(visibility, method_name)
end

#handle_asynchronously(method, enqueue_args = {}) ⇒ Object



148
149
150
# File 'lib/delayed/message_sending.rb', line 148

def handle_asynchronously(method, enqueue_args={})
  add_send_later_methods(method, enqueue_args, true)
end

#handle_asynchronously_if_production(method, enqueue_args = {}) ⇒ Object



156
157
158
# File 'lib/delayed/message_sending.rb', line 156

def handle_asynchronously_if_production(method, enqueue_args={})
  add_send_later_methods(method, enqueue_args, Rails.env.production?)
end

#handle_asynchronously_with_queue(method, queue) ⇒ Object



152
153
154
# File 'lib/delayed/message_sending.rb', line 152

def handle_asynchronously_with_queue(method, queue)
  add_send_later_methods(method, {:queue => queue}, true)
end