Class: Mongo::Sugar::Connection::Connection
- Inherits:
-
Object
- Object
- Mongo::Sugar::Connection::Connection
- Defined in:
- lib/mongo-sugar/connection.rb
Instance Attribute Summary collapse
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#default_db ⇒ Object
readonly
Returns the value of attribute default_db.
Instance Method Summary collapse
-
#database(name = nil) ⇒ Mongo::DB
Get database reference from connection.
-
#initialize(uri) ⇒ Connection
constructor
A new instance of Connection.
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
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
10 11 12 |
# File 'lib/mongo-sugar/connection.rb', line 10 def conn @conn end |
#default_db ⇒ Object (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
26 27 28 |
# File 'lib/mongo-sugar/connection.rb', line 26 def database(name=nil) @conn.db(name || @default_db) end |