Sha256: 73f58c69756d8b1f1f752aeed9f7e12dd1d2aed34c35535aab2a957e30e3e587

Contents?: true

Size: 1.57 KB

Versions: 9

Compression:

Stored size: 1.57 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

9 entries across 9 versions & 1 rubygems

Version Path
active_encode-1.2.3 lib/active_encode/engine_adapter.rb
active_encode-1.2.2 lib/active_encode/engine_adapter.rb
active_encode-1.2.1 lib/active_encode/engine_adapter.rb
active_encode-1.2.0 lib/active_encode/engine_adapter.rb
active_encode-1.1.3 lib/active_encode/engine_adapter.rb
active_encode-1.1.2 lib/active_encode/engine_adapter.rb
active_encode-1.1.1 lib/active_encode/engine_adapter.rb
active_encode-1.1.0 lib/active_encode/engine_adapter.rb
active_encode-1.0.0 lib/active_encode/engine_adapter.rb