Module: Delayed::MessageSending::ClassMethods

Defined in:
lib/delayed/message_sending.rb

Instance Method Summary collapse

Instance Method Details

#handle_asynchronously(method_name, **enqueue_args) ⇒ Object


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

def handle_asynchronously(method_name, **enqueue_args)
  aliased_method, punctuation = method_name.to_s.sub(/([?!=])$/, ''), $1

  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(method_name, -> (*args, synchronous: false, **kwargs) do
        if synchronous
          super(*args, **kwargs)
        else
          delay(sender: __calculate_sender_for_delay, **enqueue_args).method_missing(method_name, *args, synchronous: true, **kwargs)
        end
      end)
    end
  else
    generated_delayed_methods.class_eval do
      define_method(method_name, -> (*args, synchronous: false) do
        if synchronous
          super(*args)
        else
          delay(**enqueue_args).method_missing(method_name, *args, synchronous: true)
        end
      end)
    end
  end
  generated_delayed_methods.send(visibility, method_name)
end