Module: Delayed::MessageSendingClassMethods

Defined in:
lib/delayed/message_sending.rb

Instance Method Summary collapse

Instance Method Details

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

rubocop:disable PerceivedComplexity



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

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