Sha256: 4e3fb0c1ff5d9503a50aa16ee4a8f3e8e366cb7bc51104cac56be46f0123b012

Contents?: true

Size: 997 Bytes

Versions: 3

Compression:

Stored size: 997 Bytes

Contents

module Slaver
  class PoolsHandler
    include Singleton

    def pools
      @pools ||= {}
    end

    def for_config(klass, config_name)
      @klass = klass

      initialize_pool(config_name) unless pools[config_name]

      self
    end

    private

    def initialize_pool(config_name)
      if config_name == Rails.env
        pools[config_name] = @klass.connection_pool_without_proxy
      else
        pools[config_name] = create_connection_pool(config_name)
      end
    end

    def create_connection_pool(config_name)
      config = ::ActiveRecord::Base.configurations[config_name]
      config.symbolize_keys!
      if ActiveRecord::VERSION::MAJOR < 4
        spec = ActiveRecord::Base::ConnectionSpecification.new(config, "#{config[:adapter]}_connection")
      else
        spec = ActiveRecord::ConnectionAdapters::ConnectionSpecification.new(config, "#{config[:adapter]}_connection")
      end

      ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
slaver-0.2.3 lib/slaver/pools_handler.rb
slaver-0.2.2 lib/slaver/pools_handler.rb
slaver-0.2.0 lib/slaver/pools_handler.rb