Module: Farm::MessageSending::ClassMethods

Defined in:
lib/farm/message_sending.rb

Instance Method Summary collapse

Instance Method Details

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/farm/message_sending.rb', line 24

def handle_asynchronously(method, opts = {})
  aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
  with_method, without_method = "#{aliased_method}_with_farm#{punctuation}", "#{aliased_method}_without_farm#{punctuation}"
  define_method(with_method) do |*args|
    curr_opts = opts.clone
    curr_opts.each_key do |key|
      if (val = curr_opts[key]).is_a?(Proc)
        curr_opts[key] = if val.arity == 1
          val.call(self)
        else
          val.call
        end
      end
    end
    farm(curr_opts).__send__(without_method, *args)
  end
  alias_method_chain method, :farm
end