Sha256: 996f17c3e158eae55422167c5e3cd16b20e276d68ae7c1b5560a1e2b5ee3dfc0

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require 'uri'

module Moneta
  module Adapters
    class ActiveRecord
      # @api private
      class Backend
        @connection_lock = ::Mutex.new

        class << self
          attr_reader :connection_lock
        end

        attr_reader :table_name
        delegate :connection_handler, to: ::ActiveRecord::Base

        def initialize(table:, connection: nil, **options)
          @table_name = table
          @connection = connection
          if connection
            @owner_name =
              case connection
              when Symbol, String
                connection.to_s
              when Hash
                hash = connection.reject { |key| [:username, 'username', :password, 'password'].member?(key) }
                'moneta?' + URI.encode_www_form(hash.to_a.sort)
              when nil
                nil
              else
                raise "Unexpected connection: #{connection}"
              end
          end
        end

        def connection_pool
          if @connection
            connection_handler.retrieve_connection_pool(@owner_name) ||
              self.class.connection_lock.synchronize do
                connection_handler.retrieve_connection_pool(@owner_name) ||
                  connection_handler.establish_connection(@connection, owner_name: @owner_name)
              end
          else
            ::ActiveRecord::Base.connection_pool
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
moneta-1.6.0 lib/moneta/adapters/activerecord/backend.rb