move functionality to Connection#db, set up default database name to use when absolutely nothing is provided
This commit is contained in:
parent
11a5d8bd53
commit
9d4429221e
@ -34,6 +34,7 @@ module Mongo
|
||||
|
||||
DEFAULT_HOST = 'localhost'
|
||||
DEFAULT_PORT = 27017
|
||||
DEFAULT_DB_NAME = 'test'
|
||||
GENERIC_OPTS = [:ssl, :auths, :pool_size, :pool_timeout, :timeout, :op_timeout, :connect_timeout, :safe, :logger, :connect]
|
||||
CONNECTION_OPTS = [:slave_ok]
|
||||
|
||||
@ -312,7 +313,13 @@ module Mongo
|
||||
# @return [Mongo::DB]
|
||||
#
|
||||
# @core databases db-instance_method
|
||||
def db(db_name, opts={})
|
||||
def db(db_name=nil, opts={})
|
||||
if !db_name && uri = ENV['MONGODB_URI']
|
||||
db_name = uri[%r{/([^/\?]+)(\?|$)}, 1]
|
||||
end
|
||||
|
||||
db_name ||= DEFAULT_DB_NAME
|
||||
|
||||
DB.new(db_name, self, opts)
|
||||
end
|
||||
|
||||
@ -327,22 +334,6 @@ module Mongo
|
||||
DB.new(db_name, self)
|
||||
end
|
||||
|
||||
# Return the database specified in ENV['MONGODB_URI']
|
||||
#
|
||||
# @param [String] uri the uri to use
|
||||
# @param [Hash] opts options to be passed to the DB constructor.
|
||||
#
|
||||
# @return [Mongo::DB]
|
||||
#
|
||||
# @core databases db_from_uri-instance_method
|
||||
def db_from_uri(uri=ENV['MONGODB_URI'], opts={})
|
||||
if db_name = uri[%r{/([^/\?]+)(\?|$)}, 1]
|
||||
DB.new(db_name, self, opts)
|
||||
else
|
||||
raise ArgumentError.new("No database name found in #{uri}")
|
||||
end
|
||||
end
|
||||
|
||||
# Drop a database.
|
||||
#
|
||||
# @param [String] name name of an existing database.
|
||||
|
@ -89,7 +89,7 @@ class TestConnection < Test::Unit::TestCase
|
||||
old_mongodb_uri = ENV['MONGODB_URI']
|
||||
ENV['MONGODB_URI'] = "mongodb://#{host_port}/#{db_name}"
|
||||
con = Connection.from_uri
|
||||
db = con.db_from_uri
|
||||
db = con.db
|
||||
assert_equal db.name, db_name
|
||||
ensure
|
||||
ENV['MONGODB_URI'] = old_mongodb_uri
|
||||
@ -103,7 +103,7 @@ class TestConnection < Test::Unit::TestCase
|
||||
old_mongodb_uri = ENV['MONGODB_URI']
|
||||
ENV['MONGODB_URI'] = "mongodb://#{host_port}/#{db_name}?"
|
||||
con = Connection.from_uri
|
||||
db = con.db_from_uri
|
||||
db = con.db
|
||||
assert_equal db.name, db_name
|
||||
ensure
|
||||
ENV['MONGODB_URI'] = old_mongodb_uri
|
||||
@ -115,8 +115,8 @@ class TestConnection < Test::Unit::TestCase
|
||||
old_mongodb_uri = ENV['MONGODB_URI']
|
||||
ENV['MONGODB_URI'] = "mongodb://#{host_port}/"
|
||||
con = Connection.from_uri
|
||||
|
||||
assert_raise ArgumentError do con.db_from_uri end
|
||||
db = con.db
|
||||
assert_equal db.name, Mongo::Connection::DEFAULT_DB_NAME
|
||||
ensure
|
||||
ENV['MONGODB_URI'] = old_mongodb_uri
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user