Class: DbCharmer::ConnectionFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/db_charmer/connection_factory.rb

Constant Summary collapse

@@connection_classes =
{}

Class Method Summary collapse

Class Method Details

.abstract_connection_class_name(db_name) ⇒ Object



34
35
36
# File 'lib/db_charmer/connection_factory.rb', line 34

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

.connect(db_name, should_exist = false) ⇒ Object



9
10
11
12
13
# File 'lib/db_charmer/connection_factory.rb', line 9

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

.establish_connection(db_name, should_exist = false) ⇒ Object



15
16
17
18
19
# File 'lib/db_charmer/connection_factory.rb', line 15

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

.generate_abstract_class(db_name, should_exist = false) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/db_charmer/connection_factory.rb', line 21

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

.reset!Object



5
6
7
# File 'lib/db_charmer/connection_factory.rb', line 5

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