Module: OmniService::Async::Convenience

Defined in:
lib/omni_service/async.rb

Overview

A helper module to simplify async operation creation.

Examples:

Basic usage


class MyOperation
  extend OmniService::Convenience
  extend OmniService::Async::Convenience[queue: 'important', retry: 3]

  def self.system
    @system ||= sequence(...)
  end
end

MyOperation.system_async.call!(...)

With custom job class


class MyOperation
  extend OmniService::Convenience
  extend OmniService::Async::Convenience[job_class: MyCustomJob]

  def self.system
    @system ||= sequence(...)
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/omni_service/async.rb', line 68

def method_missing(name, ...)
  name_without_suffix = name.to_s.delete_suffix('_async').to_sym

  if name.to_s.end_with?('_async') && respond_to?(name_without_suffix)
    ivar_name = :"@#{name}"

    if instance_variable_defined?(ivar_name)
      instance_variable_get(ivar_name)
    else
      instance_variable_set(
        ivar_name,
        OmniService::Async.new(
          self,
          name_without_suffix,
          job_class: @job_class || OperationJob,
          job_options: @job_options || {}
        )
      )
    end
  else
    super
  end
end

Class Method Details

.[](**job_options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/omni_service/async.rb', line 56

def self.[](**job_options)
  job_class = job_options.delete(:job_class) || OperationJob

  Module.new do
    define_singleton_method(:extended) do |base|
      base.extend OmniService::Async::Convenience
      base.instance_variable_set(:@job_options, job_options)
      base.instance_variable_set(:@job_class, job_class)
    end
  end
end

Instance Method Details

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/omni_service/async.rb', line 92

def respond_to_missing?(name, *)
  (name.to_s.end_with?('_async') && respond_to?(name.to_s.delete_suffix('_async').to_sym)) || super
end