Module: DbCharmer::ConnectionFactory

Defined in:
lib/db_charmer/connection_factory.rb

Constant Summary collapse

@@connection_classes =
{}

Class Method Summary collapse

Class Method Details

.abstract_connection_class_name(connection_name) ⇒ Object

Generates unique names for our abstract AR classes



85
86
87
# File 'lib/db_charmer/connection_factory.rb', line 85

def self.abstract_connection_class_name(connection_name)
  "::AutoGeneratedAbstractConnectionClass#{connection_name.to_s.gsub(/\W+/, '_').camelize}"
end

.connect(connection_or_name, should_exist = true) ⇒ Object

Uses the passed in connection class or establishes a new one connection if one exists it will return an existing one from cache



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

def self.connect(connection_or_name, should_exist = true)
  connection_or_name = connection_or_name.to_s unless connection_or_name.respond_to?(:connection)
  @@connection_classes[connection_or_name] ||= establish_connection(connection_or_name, should_exist)
end

.connect_to_db(connection_name, config) ⇒ Object

Establishes connection or return an existing one from cache (not using AR database configs)



22
23
24
25
# File 'lib/db_charmer/connection_factory.rb', line 22

def self.connect_to_db(connection_name, config)
  connection_name = connection_name.to_s
  @@connection_classes[connection_name] ||= establish_connection_to_db(connection_name, config)
end

.establish_connection(connection_or_name, should_exist = true) ⇒ Object

Establish connection with a specified name



36
37
38
39
40
41
42
43
44
# File 'lib/db_charmer/connection_factory.rb', line 36

def self.establish_connection(connection_or_name, should_exist = true)
  abstract_class =
      if connection_or_name.respond_to?(:connection)
        connection_or_name
      else
        generate_abstract_class(connection_or_name, should_exist)
      end
  DbCharmer::ConnectionProxy.new(abstract_class, connection_or_name.to_s)
end

.establish_connection_to_db(connection_name, config) ⇒ Object

Establish connection with a specified name (not using AR database configs)



47
48
49
50
# File 'lib/db_charmer/connection_factory.rb', line 47

def self.establish_connection_to_db(connection_name, config)
  abstract_class = generate_abstract_class_for_db(connection_name, config)
  DbCharmer::ConnectionProxy.new(abstract_class, connection_name)
end

.generate_abstract_class(connection_name, should_exist = true) ⇒ Object

Generate an abstract AR class with specified connection established



53
54
55
56
57
58
59
60
61
62
# File 'lib/db_charmer/connection_factory.rb', line 53

def self.generate_abstract_class(connection_name, should_exist = true)
  # Generate class
  klass = generate_empty_abstract_ar_class(abstract_connection_class_name(connection_name))

  # Establish connection
  klass.establish_real_connection_if_exists(connection_name.to_sym, !!should_exist)

  # Return the class
  return klass
end

.generate_abstract_class_for_db(connection_name, config) ⇒ Object

Generate an abstract AR class with specified connection established (not using AR database configs)



65
66
67
68
69
70
71
72
73
74
# File 'lib/db_charmer/connection_factory.rb', line 65

def self.generate_abstract_class_for_db(connection_name, config)
  # Generate class
  klass = generate_empty_abstract_ar_class(abstract_connection_class_name(connection_name))

  # Establish connection
  klass.establish_connection(config)

  # Return the class
  return klass
end

.generate_empty_abstract_ar_class(klass) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/db_charmer/connection_factory.rb', line 76

def self.generate_empty_abstract_ar_class(klass)
  # Define class
  module_eval "class #{klass} < ::ActiveRecord::Base; self.abstract_class = true; end"

  # Return class
  klass.constantize
end

.reset!Object



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

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

.reset_connection(connection_name) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/db_charmer/connection_factory.rb', line 27

def self.reset_connection(connection_name)
  connection_name = connection_name.to_s
  if @@connection_classes[connection_name]
    @@connection_classes[connection_name].disconnect!
    @@connection_classes.delete(connection_name)
  end
end