RUBY-260 methods using DB#collection_names should account for symbols.

This commit is contained in:
Kyle Banker 2011-05-09 12:28:07 -04:00
parent 74faed3f38
commit 0051b9446a
3 changed files with 45 additions and 19 deletions

View File

@ -91,7 +91,7 @@ module Mongo
# Return a sub-collection of this collection by name. If 'users' is a collection, then
# 'users.comments' is a sub-collection of users.
#
# @param [String] name
# @param [String, Symbol] name
# the collection to return
#
# @raise [Mongo::InvalidNSName]
@ -101,7 +101,8 @@ module Mongo
# the specified sub-collection
def [](name)
name = "#{self.name}.#{name}"
return Collection.new(name, db) if !db.strict? || db.collection_names.include?(name)
return Collection.new(name, db) if !db.strict? ||
db.collection_names.include?(name.to_s)
raise "Collection #{name} doesn't exist. Currently in strict mode."
end

View File

@ -251,26 +251,28 @@ module Mongo
# new collection. If +strict+ is true, will raise an error if
# collection +name+ already exists.
#
# @param [String] name the name of the new collection.
# @param [String, Symbol] name the name of the new collection.
#
# @option opts [Boolean] :capped (False) created a capped collection.
#
# @option opts [Integer] :size (Nil) If +capped+ is +true+, specifies the maximum number of
# bytes for the capped collection. If +false+, specifies the number of bytes allocated
# @option opts [Integer] :size (Nil) If +capped+ is +true+,
# specifies the maximum number of bytes for the capped collection.
# If +false+, specifies the number of bytes allocated
# for the initial extent of the collection.
#
# @option opts [Integer] :max (Nil) If +capped+ is +true+, indicates the maximum number of records
# in a capped collection.
# @option opts [Integer] :max (Nil) If +capped+ is +true+, indicates
# the maximum number of records in a capped collection.
#
# @raise [MongoDBError] raised under two conditions: either we're in +strict+ mode and the collection
# @raise [MongoDBError] raised under two conditions:
# either we're in +strict+ mode and the collection
# already exists or collection creation fails on the server.
#
# @return [Mongo::Collection]
def create_collection(name, opts={})
# Does the collection already exist?
if collection_names.include?(name)
if collection_names.include?(name.to_s)
if strict?
raise MongoDBError, "Collection #{name} already exists. Currently in strict mode."
raise MongoDBError, "Collection #{name} already exists. " +
"Currently in strict mode."
else
return Collection.new(name, self, opts)
end
@ -286,15 +288,17 @@ module Mongo
# Get a collection by name.
#
# @param [String] name the collection name.
# @param [String, Symbol] name the collection name.
# @param [Hash] opts any valid options that can be passed to Collection#new.
#
# @raise [MongoDBError] if collection does not already exist and we're in +strict+ mode.
# @raise [MongoDBError] if collection does not already exist and we're in
# +strict+ mode.
#
# @return [Mongo::Collection]
def collection(name, opts={})
if strict? && !collection_names.include?(name)
raise Mongo::MongoDBError, "Collection #{name} doesn't exist. Currently in strict mode."
if strict? && !collection_names.include?(name.to_s)
raise Mongo::MongoDBError, "Collection #{name} doesn't exist. " +
"Currently in strict mode."
else
opts = opts.dup
opts[:safe] = opts.fetch(:safe, @safe)
@ -306,11 +310,11 @@ module Mongo
# Drop a collection by +name+.
#
# @param [String] name
# @param [String, Symbol] name
#
# @return [Boolean] +true+ on success or +false+ if the collection name doesn't exist.
def drop_collection(name)
return true unless collection_names.include?(name)
return true unless collection_names.include?(name.to_s)
ok?(command(:drop => name))
end

View File

@ -32,14 +32,35 @@ class DBTest < Test::Unit::TestCase
@@users = @@db.collection('system.users')
end
end
def test_create_collection
col = @@db.create_collection('foo')
assert_equal @@db['foo'].name, col.name
col = @@db.create_collection(:foo)
assert_equal @@db['foo'].name, col.name
@@db.drop_collection('foo')
end
def test_get_and_drop_collection
db = @@conn.db(MONGO_TEST_DB, :strict => true)
db.create_collection('foo')
assert db.collection('foo')
assert db.drop_collection('foo')
db.create_collection(:foo)
assert db.collection(:foo)
assert db.drop_collection(:foo)
end
def test_logger
output = StringIO.new
logger = Logger.new(output)
logger.level = Logger::DEBUG
conn = standard_connection(:logger => logger)
assert_equal logger, conn.logger
conn.logger.debug 'testing'
assert output.string.include?('testing')
end