Class: Mongo::Sugar::Connection::Manager
- Inherits:
-
Object
- Object
- Mongo::Sugar::Connection::Manager
- Includes:
- Singleton
- Defined in:
- lib/mongo-sugar/connection/manager.rb
Overview
Manages multiple database connections
Instance Attribute Summary collapse
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
Class Method Summary collapse
-
.create(name, uri = nil) ⇒ Mongo::Sugar::Connection
Create MongoDB connection.
-
.database(name, database = nil) ⇒ Object
Get database instance.
-
.get(name) ⇒ Mongo::Connection?
Get MongoDB connection.
Instance Method Summary collapse
-
#initialize ⇒ Manager
constructor
A new instance of Manager.
Constructor Details
#initialize ⇒ Manager
Returns a new instance of Manager.
14 15 16 |
# File 'lib/mongo-sugar/connection/manager.rb', line 14 def initialize @connections = {} end |
Instance Attribute Details
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
12 13 14 |
# File 'lib/mongo-sugar/connection/manager.rb', line 12 def connections @connections end |
Class Method Details
.create(name, uri = nil) ⇒ Mongo::Sugar::Connection
Create MongoDB connection
24 25 26 |
# File 'lib/mongo-sugar/connection/manager.rb', line 24 def self.create(name, uri=nil) self.instance.connections[name] = Connection.new(Config.instance.get(name) || ENV['MONGODB_URI']) end |
.database(name, database = nil) ⇒ Object
Get database instance
return [Mongo::DB] database instance
43 44 45 |
# File 'lib/mongo-sugar/connection/manager.rb', line 43 def self.database(name, database=nil) self.get(name).database(database) end |
.get(name) ⇒ Mongo::Connection?
Get MongoDB connection
33 34 35 |
# File 'lib/mongo-sugar/connection/manager.rb', line 33 def self.get(name) self.instance.connections[name] or self.create(name) end |