Sha256: 82c6b537aa55ba6a535336ac620ccfe5095c628cf8a341a8465d9c3b7af1d889

Contents?: true

Size: 1.59 KB

Versions: 5

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true
require 'active_encode/engine_adapters'
require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/string/inflections'

module ActiveEncode
  # The <tt>ActiveEncode::EngineAdapter</tt> module is used to load the
  # correct adapter. The default engine adapter is the :active_job engine.
  module EngineAdapter #:nodoc:
    extend ActiveSupport::Concern

    included do
      class_attribute :_engine_adapter, instance_accessor: false, instance_predicate: false
      self.engine_adapter = :test
    end

    # Includes the setter method for changing the active engine adapter.
    module ClassMethods
      def engine_adapter
        _engine_adapter
      end

      # Specify the backend engine provider. The default engine adapter
      # is the :inline engine. See QueueAdapters for more
      # information.
      def engine_adapter=(name_or_adapter_or_class)
        self._engine_adapter = interpret_adapter(name_or_adapter_or_class)
      end

      private

        def interpret_adapter(name_or_adapter_or_class)
          case name_or_adapter_or_class
          when Symbol, String
            ActiveEncode::EngineAdapters.lookup(name_or_adapter_or_class).new
          else
            name_or_adapter_or_class if engine_adapter?(name_or_adapter_or_class)
            raise ArgumentError unless engine_adapter?(name_or_adapter_or_class)
          end
        end

        ENGINE_ADAPTER_METHODS = %i[create find cancel].freeze

        def engine_adapter?(object)
          ENGINE_ADAPTER_METHODS.all? { |meth| object.respond_to?(meth) }
        end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
active_encode-0.8.2 lib/active_encode/engine_adapter.rb
active_encode-0.8.1 lib/active_encode/engine_adapter.rb
active_encode-0.8.0 lib/active_encode/engine_adapter.rb
active_encode-0.7.0 lib/active_encode/engine_adapter.rb
active_encode-0.6.0 lib/active_encode/engine_adapter.rb