Module: Delayed::MessageSending::ClassMethods

Defined in:
lib/delayed/message_sending.rb

Instance Method Summary collapse

Instance Method Details

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



98
99
100
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
# File 'lib/delayed/message_sending.rb', line 98

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

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

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

  generated_delayed_methods.class_eval(<<-EOF, __FILE__, __LINE__ + 1)
    def #{without_method}(*args)
      send(:#{method}, *args, synchronous: true)
    end
    #{visibility} :#{without_method}

    def #{method}(*args, synchronous: #{!default_async})
      if synchronous
        super(*args)
      else
        send_later_enqueue_args(:#{method}, #{enqueue_args.inspect}, *args, synchronous: true)
      end
    end
    #{visibility} :#{method}
  EOF
end

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



129
130
131
# File 'lib/delayed/message_sending.rb', line 129

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

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



137
138
139
# File 'lib/delayed/message_sending.rb', line 137

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



133
134
135
# File 'lib/delayed/message_sending.rb', line 133

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