Sha256: e525e7eaa78b64c81a4f7be921639f81d6ed8e9c53e9e3efcc0bab5ea9968cf2

Contents?: true

Size: 1.72 KB

Versions: 13

Compression:

Stored size: 1.72 KB

Contents

module NinjaModel
  module Adapters
    class AdapterManager

      class_attribute :registered
      self.registered = {}

      class << self
        def registered?(name)
          registered.key?(name.to_sym)
        end

        def register_adapter_class(name, klass)
          registered[name.to_sym] = klass
        end
      end

      attr_reader :adapter_pools

      def initialize(pools = {})
        @adapter_pools = pools
        @registered = {}
      end

      def create_adapter(name, spec)
        @adapter_pools[name] = Adapters::AdapterPool.new(spec)
      end

      def release_active_adapters!
        @adapter_pools.each_value do |pool|
          pool.release_instance
        end
      end

      def release_all_adapters!
        @adapter_pools.each_value do |pool|
          pool.shutdown!
        end
      end

      def retrieve_adapter(klass)
        pool = retrieve_adapter_pool(klass)
        (pool && pool.instance) or raise StandardError, "Pool is empty or instance is null"
      end

      def remove_adapter(klass)
        pool = @adapter_pools[klass.name]
        return nil unless pool
        @adapter_pools.delete_if { |key, value| value == pool }
        pool.shutdown!
        pool.spec.config
      end

      def retrieve_adapter_pool(klass)
        pool = @adapter_pools[klass.name]
        return pool if pool
        return nil if NinjaModel::Base == klass
        retrieve_adapter_pool klass.superclass
      end
    end

    class AdapterManagement
      def initialize(app)
        @app = app
      end

      def call(env)
        @app.call(env)
      ensure
        unless env.key?('rack.test')
          NinjaModel::Base.adapter_manager.release_active_adapters!
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ninja-model-1.0.5 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-1.0.4 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-1.0.3 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-1.0.2 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-1.0.1 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-1.0.0 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.9.6 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.9.5 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.9.4 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.9.3 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.9.2 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.9.1 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.9.0 lib/ninja_model/adapters/adapter_manager.rb