Class: MultipleConnectionHandler
- Inherits:
-
Object
- Object
- MultipleConnectionHandler
- Defined in:
- lib/multiple_connection_handler.rb
Overview
grants access to db connections for several environments at once. implemented as a singleton accessed through class methods.
Defined Under Namespace
Classes: DbKey, DoubleInitializationError, UnrecognizedDatabaseError
Constant Summary collapse
- @@static_initialized =
false
Instance Attribute Summary collapse
-
#configurations ⇒ Object
Returns the value of attribute configurations.
-
#db_config_file ⇒ Object
Returns the value of attribute db_config_file.
-
#established_connections ⇒ Object
Returns the value of attribute established_connections.
-
#handler ⇒ Object
Returns the value of attribute handler.
Class Method Summary collapse
-
.connection(spec_name) ⇒ Object
return a db connection for the specified name, establishing new pools with the AR connection handler if necessary —————————————————————————–.
-
.db_name(spec_name) ⇒ Object
returns the db name for the given spec name —————————————————————————–.
-
.init(options = {}) ⇒ Object
initialize the connections handler.
Instance Method Summary collapse
-
#connection(spec_name) ⇒ Object
the meat of the work is done here.
Instance Attribute Details
#configurations ⇒ Object
Returns the value of attribute configurations.
79 80 81 |
# File 'lib/multiple_connection_handler.rb', line 79 def configurations @configurations end |
#db_config_file ⇒ Object
Returns the value of attribute db_config_file.
79 80 81 |
# File 'lib/multiple_connection_handler.rb', line 79 def db_config_file @db_config_file end |
#established_connections ⇒ Object
Returns the value of attribute established_connections.
80 81 82 |
# File 'lib/multiple_connection_handler.rb', line 80 def established_connections @established_connections end |
#handler ⇒ Object
Returns the value of attribute handler.
79 80 81 |
# File 'lib/multiple_connection_handler.rb', line 79 def handler @handler end |
Class Method Details
.connection(spec_name) ⇒ Object
return a db connection for the specified name, establishing new pools with the AR connection handler if necessary
44 45 46 |
# File 'lib/multiple_connection_handler.rb', line 44 def self.connection(spec_name) instance.connection(spec_name) end |
.db_name(spec_name) ⇒ Object
returns the db name for the given spec name
50 51 52 |
# File 'lib/multiple_connection_handler.rb', line 50 def self.db_name(spec_name) instance.configurations[spec_name.to_s]['database'] end |
.init(options = {}) ⇒ Object
initialize the connections handler. only necessary to be called if overriding any default configuration. recommend against using this method explicitly and just allowing default.
options:
- :config_file => the database config file to use. default is the
rails default of ..../config/database.yml
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/multiple_connection_handler.rb', line 63 def self.init( = {}) # don't allow re-initializing. just ignore if we're already initialized # unless we're asked to use a different config file if @@static_initialized if @@instantiated && [:config_file] && [:config_file] != instance.db_config_file raise DoubleInitializationError end else @@db_config_file = [:config_file] || "#{RAILS_ROOT}/config/database.yml" @@static_initialized = true end end |
Instance Method Details
#connection(spec_name) ⇒ Object
the meat of the work is done here. retrieve a connection using cached connections or get a new one.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/multiple_connection_handler.rb', line 88 def connection(spec_name) db_key = DbKey.new(spec_name.to_s) unless established_connections.member? db_key unless configurations.include? db_key.name raise UnrecognizedDatabaseError.new(db_key.name, db_config_file) end spec = configurations[db_key.name] handler.establish_connection(db_key.name, ActiveRecord::Base::ConnectionSpecification.new(spec, "#{spec['adapter']}_connection")) established_connections.add db_key.name end # end connection not established yet handler.retrieve_connection db_key end |