Sha256: df4482cc751d295559497f7e5b5f5d5388a8725d22df9aed3bf0c0ae0d4e5ed9

Contents?: true

Size: 1.57 KB

Versions: 4

Compression:

Stored size: 1.57 KB

Contents

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

4 entries across 4 versions & 1 rubygems

Version Path
active_encode-0.5.0 lib/active_encode/engine_adapter.rb
active_encode-0.4.1 lib/active_encode/engine_adapter.rb
active_encode-0.4 lib/active_encode/engine_adapter.rb
active_encode-0.2 lib/active_encode/engine_adapter.rb