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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Connection

Returns a new instance of Connection.



12
13
14
15
16
17
18
19
# File 'lib/mongo-sugar/connection.rb', line 12

def initialize(uri)
  raise "Connection#initialize: you must create a config/mongodb.yml file, or set ENV['MONGODB_URI']." if uri.nil?
  @conn = Mongo::Connection.from_uri(uri)

  # If URI doens't contain database part, get default connection's database name
  default_db = File.basename(uri)
  @default_db = default_db.index(':') ? @conn.db.name : default_db
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



10
11
12
# File 'lib/mongo-sugar/connection.rb', line 10

def conn
  @conn
end

#default_dbObject (readonly)

Returns the value of attribute default_db.



10
11
12
# File 'lib/mongo-sugar/connection.rb', line 10

def default_db
  @default_db
end

Instance Method Details

#database(name = nil) ⇒ Mongo::DB

Get database reference from connection

Parameters:

  • database (String)

    name

Returns:

  • (Mongo::DB)

    datbase reference



26
27
28
# File 'lib/mongo-sugar/connection.rb', line 26

def database(name=nil)
  @conn.db(name || @default_db)
end