Sha256: 375bb35dbe4727feed96c9bafa7ef4b428056290e4b6547f623b27ab176f9dab

Contents?: true

Size: 841 Bytes

Versions: 7

Compression:

Stored size: 841 Bytes

Contents

require 'active_support/concern'
require 'global_id'

begin
  require 'sidekiq'
  require 'sidekiq/api'
rescue LoadError
  raise Services::BackgroundProcessorNotFound
end

module Services
  module Asyncable
    extend ActiveSupport::Concern

    ASYNC_METHOD_SUFFIXES = %i(async in at).freeze

    included do
      include Sidekiq::Worker
    end

    module ClassMethods
      ASYNC_METHOD_SUFFIXES.each do |async_method_suffix|
        define_method "call_#{async_method_suffix}" do |*args|
          args.map! do |arg|
            arg.respond_to?(:to_global_id) ? arg.to_global_id : arg
          end
          self.public_send "perform_#{async_method_suffix}", *args
        end
      end
    end

    def perform(*args)
      args.map! do |arg|
        GlobalID::Locator.locate(arg) || arg
      end
      call *args
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
services-7.0.1 lib/services/asyncable.rb
services-7.0.0 lib/services/asyncable.rb
services-6.0.5 lib/services/asyncable.rb
services-6.0.4 lib/services/asyncable.rb
services-6.0.3 lib/services/asyncable.rb
services-6.0.2 lib/services/asyncable.rb
services-6.0.1 lib/services/asyncable.rb