Sha256: 27d8a311c0448a3df7e8fd1b5428aa7ff2dc686699d80d94d2d88b398be69520

Contents?: true

Size: 1.35 KB

Versions: 21

Compression:

Stored size: 1.35 KB

Contents

module DbCharmer
  class ConnectionFactory
    @@connection_classes = {}

    def self.reset!
      @@connection_classes = {}
    end

    def self.connect(db_name, should_exist = false)
      @@connection_classes[db_name.to_s] ||= establish_connection(db_name.to_s, should_exist)
#      DbCharmer.logger.warn("ConnectionFactory.connect(#{db_name}) = #{@@connection_classes[db_name.to_s]}")
#      return @@connection_classes[db_name.to_s]
    end

    def self.establish_connection(db_name, should_exist = false)
#      DbCharmer.logger.debug("Creating a connection proxy for #{db_name}")
      abstract_class = generate_abstract_class(db_name, should_exist)
      DbCharmer::ConnectionProxy.new(abstract_class)
    end

    def self.generate_abstract_class(db_name, should_exist = false)
#      DbCharmer.logger.info("Generating abstract connection class for #{db_name}: #{abstract_connection_class_name db_name}")

      module_eval <<-EOF, __FILE__, __LINE__ + 1
        class #{abstract_connection_class_name db_name} < ActiveRecord::Base
          self.abstract_class = true
          establish_real_connection_if_exists(:#{db_name}, #{!!should_exist})
        end
      EOF

      abstract_connection_class_name(db_name).constantize
    end

    def self.abstract_connection_class_name(db_name)
      "::AutoGeneratedAbstractConnectionClass#{db_name.camelize}"
    end
  end
end

Version data entries

21 entries across 21 versions & 3 rubygems

Version Path
glebpom-db-charmer-1.1.5 lib/db_charmer/connection_factory.rb
glebpom-db-charmer-1.3.0 lib/db_charmer/connection_factory.rb
kovyrin-db-charmer-1.1.0 lib/db_charmer/connection_factory.rb
kovyrin-db-charmer-1.1.2 lib/db_charmer/connection_factory.rb
kovyrin-db-charmer-1.1.3 lib/db_charmer/connection_factory.rb
kovyrin-db-charmer-1.2.0 lib/db_charmer/connection_factory.rb
db-charmer-1.5.1 lib/db_charmer/connection_factory.rb
db-charmer-1.5.0 lib/db_charmer/connection_factory.rb
db-charmer-1.4.6 lib/db_charmer/connection_factory.rb
db-charmer-1.4.5 lib/db_charmer/connection_factory.rb
db-charmer-1.4.4 lib/db_charmer/connection_factory.rb
db-charmer-1.4.3 lib/db_charmer/connection_factory.rb
db-charmer-1.4.1 lib/db_charmer/connection_factory.rb
db-charmer-1.4.0 lib/db_charmer/connection_factory.rb
db-charmer-1.3.4 lib/db_charmer/connection_factory.rb
db-charmer-1.3.3 lib/db_charmer/connection_factory.rb
db-charmer-1.3.2 lib/db_charmer/connection_factory.rb
db-charmer-1.3.1 lib/db_charmer/connection_factory.rb
glebpom-db-charmer-1.4.3 lib/db_charmer/connection_factory.rb
glebpom-db-charmer-1.4.2 lib/db_charmer/connection_factory.rb