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 # Return a sub-collection of this collection by name. If 'users' is a collection, then
# 'users.comments' is a sub-collection of users. # 'users.comments' is a sub-collection of users.
# #
# @param [String] name # @param [String, Symbol] name
# the collection to return # the collection to return
# #
# @raise [Mongo::InvalidNSName] # @raise [Mongo::InvalidNSName]
@ -101,7 +101,8 @@ module Mongo
# the specified sub-collection # the specified sub-collection
def [](name) def [](name)
name = "#{self.name}.#{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." raise "Collection #{name} doesn't exist. Currently in strict mode."
end end

View File

@ -251,26 +251,28 @@ module Mongo
# new collection. If +strict+ is true, will raise an error if # new collection. If +strict+ is true, will raise an error if
# collection +name+ already exists. # 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 [Boolean] :capped (False) created a capped collection.
# #
# @option opts [Integer] :size (Nil) If +capped+ is +true+, specifies the maximum number of # @option opts [Integer] :size (Nil) If +capped+ is +true+,
# bytes for the capped collection. If +false+, specifies the number of bytes allocated # 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. # for the initial extent of the collection.
# #
# @option opts [Integer] :max (Nil) If +capped+ is +true+, indicates the maximum number of records # @option opts [Integer] :max (Nil) If +capped+ is +true+, indicates
# in a capped collection. # 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. # already exists or collection creation fails on the server.
# #
# @return [Mongo::Collection] # @return [Mongo::Collection]
def create_collection(name, opts={}) def create_collection(name, opts={})
# Does the collection already exist? if collection_names.include?(name.to_s)
if collection_names.include?(name)
if strict? if strict?
raise MongoDBError, "Collection #{name} already exists. Currently in strict mode." raise MongoDBError, "Collection #{name} already exists. " +
"Currently in strict mode."
else else
return Collection.new(name, self, opts) return Collection.new(name, self, opts)
end end
@ -286,15 +288,17 @@ module Mongo
# Get a collection by name. # 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. # @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] # @return [Mongo::Collection]
def collection(name, opts={}) def collection(name, opts={})
if strict? && !collection_names.include?(name) if strict? && !collection_names.include?(name.to_s)
raise Mongo::MongoDBError, "Collection #{name} doesn't exist. Currently in strict mode." raise Mongo::MongoDBError, "Collection #{name} doesn't exist. " +
"Currently in strict mode."
else else
opts = opts.dup opts = opts.dup
opts[:safe] = opts.fetch(:safe, @safe) opts[:safe] = opts.fetch(:safe, @safe)
@ -306,11 +310,11 @@ module Mongo
# Drop a collection by +name+. # 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. # @return [Boolean] +true+ on success or +false+ if the collection name doesn't exist.
def drop_collection(name) def drop_collection(name)
return true unless collection_names.include?(name) return true unless collection_names.include?(name.to_s)
ok?(command(:drop => name)) ok?(command(:drop => name))
end end

View File

@ -33,6 +33,27 @@ class DBTest < Test::Unit::TestCase
end end
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 def test_logger
output = StringIO.new output = StringIO.new
logger = Logger.new(output) logger = Logger.new(output)