Sha256: f92c613afc878d0ca166ca5e62f82651e3be7af73a681dd52b494985d28f1ed3

Contents?: true

Size: 1.72 KB

Versions: 16

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

16 entries across 16 versions & 1 rubygems

Version Path
ninja-model-0.8.1 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.8.0 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.7.3 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.7.2 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.7.1 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.7.0 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.6.2 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.6.1 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.6.0 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.5.7 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.5.6 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.5.5 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.5.4 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.5.3 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.5.2 lib/ninja_model/adapters/adapter_manager.rb
ninja-model-0.5.1 lib/ninja_model/adapters/adapter_manager.rb