Class: Mongo::Sugar::Connection::Manager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mongo-sugar/connection/manager.rb

Overview

Manages multiple database connections

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManager

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

#connectionsObject (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

Parameters:

  • connection (String)

    name

  • connection (String)

    URI (optional. default: ENV)

Returns:



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

Parameters:

  • connection (String)

    name

  • database (String) (defaults to: nil)

    to connect. (optional, inherit from your database URI, if specified)



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

Parameters:

  • connection (String)

    name

Returns:

  • (Mongo::Connection, nil)

    connection or nil, if connection doesn’t have been created



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