Module: DbCharmer::ConnectionFactory
- Defined in:
- lib/db_charmer/connection_factory.rb
Class Method Summary collapse
-
.abstract_connection_class_name(connection_name) ⇒ Object
Generates unique names for our abstract AR classes.
-
.connect(connection_name, should_exist = true) ⇒ Object
Establishes connection or return an existing one from cache.
-
.connect_to_db(connection_name, config) ⇒ Object
Establishes connection or return an existing one from cache (not using AR database configs).
- .connection_classes ⇒ Object
- .connection_classes=(val) ⇒ Object
-
.establish_connection(connection_name, should_exist = true) ⇒ Object
Establish connection with a specified name.
-
.establish_connection_to_db(connection_name, config) ⇒ Object
Establish connection with a specified name (not using AR database configs).
-
.generate_abstract_class(connection_name, should_exist = true) ⇒ Object
Generate an abstract AR class with specified connection established.
-
.generate_abstract_class_for_db(connection_name, config) ⇒ Object
Generate an abstract AR class with specified connection established (not using AR database configs).
- .generate_empty_abstract_ar_class(klass) ⇒ Object
- .reset! ⇒ Object
Class Method Details
.abstract_connection_class_name(connection_name) ⇒ Object
Generates unique names for our abstract AR classes
78 79 80 81 82 |
# File 'lib/db_charmer/connection_factory.rb', line 78 def self.abstract_connection_class_name(connection_name) conn_name_klass = connection_name.to_s.gsub(/\W+/, '_').camelize thread = Thread.current.object_id.abs # need to make sure it is non-negative "::AutoGeneratedAbstractConnectionClass#{conn_name_klass}ForThread#{thread}" end |
.connect(connection_name, should_exist = true) ⇒ Object
Establishes connection or return an existing one from cache
22 23 24 25 |
# File 'lib/db_charmer/connection_factory.rb', line 22 def self.connect(connection_name, should_exist = true) connection_name = connection_name.to_s connection_classes[connection_name] ||= establish_connection(connection_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)
28 29 30 31 |
# File 'lib/db_charmer/connection_factory.rb', line 28 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 |
.connection_classes ⇒ Object
9 10 11 |
# File 'lib/db_charmer/connection_factory.rb', line 9 def self.connection_classes Thread.current[:db_charmer_generated_connection_classes] ||= {} end |
.connection_classes=(val) ⇒ Object
13 14 15 |
# File 'lib/db_charmer/connection_factory.rb', line 13 def self.connection_classes=(val) Thread.current[:db_charmer_generated_connection_classes] = val end |
.establish_connection(connection_name, should_exist = true) ⇒ Object
Establish connection with a specified name
34 35 36 37 |
# File 'lib/db_charmer/connection_factory.rb', line 34 def self.establish_connection(connection_name, should_exist = true) abstract_class = generate_abstract_class(connection_name, should_exist) DbCharmer::ConnectionProxy.new(abstract_class, connection_name) end |
.establish_connection_to_db(connection_name, config) ⇒ Object
Establish connection with a specified name (not using AR database configs)
40 41 42 43 |
# File 'lib/db_charmer/connection_factory.rb', line 40 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
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/db_charmer/connection_factory.rb', line 46 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)
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/db_charmer/connection_factory.rb', line 58 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
69 70 71 72 73 74 75 |
# File 'lib/db_charmer/connection_factory.rb', line 69 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
17 18 19 |
# File 'lib/db_charmer/connection_factory.rb', line 17 def self.reset! self.connection_classes = {} end |