Sha256: 63fdf962a529f9e3d05aef2d7a3c772851343a4ead1e257dc195f4113873563f

Contents?: true

Size: 937 Bytes

Versions: 3

Compression:

Stored size: 937 Bytes

Contents

module DbCharmer
  module ConnectionSwitch
    module ClassMethods
      def coerce_to_connection_proxy(conn, should_exist = true)
        return nil if conn.nil? || !DbCharmer.switch_connections?

        if conn.kind_of?(Symbol) || conn.kind_of?(String)
          return DbCharmer::ConnectionFactory.connect(conn, should_exist)
        end

        if conn.respond_to?(:db_charmer_connection_proxy)
          return conn.db_charmer_connection_proxy
        end

        if conn.kind_of?(ActiveRecord::ConnectionAdapters::AbstractAdapter)
          return conn
        end

        raise "Unsupported connection type: #{conn.class}"
      end

      def switch_connection_to(conn, require_config_to_exist = true)
#        puts "DEBUG: Assigning connection proxy for #{self}"
        self.db_charmer_connection_proxy = coerce_to_connection_proxy(conn, require_config_to_exist)
        self.hijack_connection!
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
glebpom-db-charmer-1.4.3 lib/db_charmer/connection_switch.rb
glebpom-db-charmer-1.4.2 lib/db_charmer/connection_switch.rb
glebpom-db-charmer-1.4.0 lib/db_charmer/connection_switch.rb