Module: Services::Asyncable

Extended by:
ActiveSupport::Concern
Defined in:
lib/services/asyncable.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ASYNC_METHOD_SUFFIXES =
%i(async in at).freeze

Instance Method Summary collapse

Instance Method Details

#perform(*args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/services/asyncable.rb', line 42

def perform(*args)
  args = args.map(&Services.method(:replace_global_ids_with_records))

  call_method = method(:call)

  # Find the first class that inherits from `Services::Base`.
  while !(call_method.owner < Services::Base)
    call_method = call_method.super_method
  end

  # If the `call` method takes any kwargs and the last argument is a hash, pass them to the method as kwargs.
  kwargs = if call_method.parameters.map(&:first).grep(/\Akey/).any? && args.last.is_a?(Hash)
    args.pop.symbolize_keys
  else
    {}
  end

  # Save args and kwargs in ivars so they can be used
  # in the service, i.e. for rescheduling.
  @_call_args, @_call_kwargs = args, kwargs

  call *args, **kwargs
end