Specify pk factory on Collection.new as a :pk option

This commit is contained in:
Kyle Banker 2010-11-03 18:36:29 -04:00
parent 888c318ad2
commit a2f501924e
4 changed files with 35 additions and 6 deletions

View File

@ -28,6 +28,9 @@ module Mongo
# @param [DB] db a MongoDB database instance.
# @param [String, Symbol] name the name of the collection.
#
# @option options [:create_pk] :pk (BSON::ObjectId) A primary key factory to use
# other than the default BSON::ObjectId.
#
# @option options [Boolean, Hash] :safe (false) Set the default safe-mode options
# for insert, update, and remove method called on this Collection instance. If no
# value is provided, the default value set on this instance's DB will be used. This
@ -42,7 +45,7 @@ module Mongo
# @return [Collection]
#
# @core collections constructor_details
def initialize(db, name, pk_factory=nil, options={})
def initialize(db, name, options={})
case name
when Symbol, String
else
@ -61,11 +64,21 @@ module Mongo
raise Mongo::InvalidNSName, "collection names must not start or end with '.'"
end
if options.respond_to?(:create_pk) || !options.is_a?(Hash)
warn "The method for specifying a primary key factory on a Collection has changed.\n" +
"Please specify it as an option (e.g., :pk => PkFactory)."
pk_factory = options
else
pk_factory = nil
end
@db, @name = db, name
@connection = @db.connection
@logger = @connection.logger
@pk_factory = pk_factory || BSON::ObjectId
@safe = options.has_key?(:safe) ? options[:safe] : @db.safe
unless pk_factory
@safe = options.has_key?(:safe) ? options[:safe] : @db.safe
end
@pk_factory = pk_factory || options[:pk] || BSON::ObjectId
@hint = nil
end

View File

@ -255,13 +255,14 @@ module Mongo
oh = BSON::OrderedHash.new
oh[:create] = name
doc = command(oh.merge(options || {}))
return Collection.new(self, name, @pk_factory) if ok?(doc)
return Collection.new(self, name, :pk => @pk_factory) if ok?(doc)
raise MongoDBError, "Error creating collection: #{doc.inspect}"
end
# Get a collection by name.
#
# @param [String] name the collection name.
# @param [Hash] options any valid options that can me passed to Collection#new.
#
# @raise [MongoDBError] if collection does not already exist and we're in +strict+ mode.
#
@ -271,7 +272,8 @@ module Mongo
raise Mongo::MongoDBError, "Collection #{name} doesn't exist. Currently in strict mode."
else
options[:safe] = options.has_key?(:safe) ? options[:safe] : @safe
Collection.new(self, name, @pk_factory, options)
options.merge!(:pk => @pk_factory) unless options[:pk]
Collection.new(self, name, options)
end
end
alias_method :[], :collection

View File

@ -26,6 +26,20 @@ class TestCollection < Test::Unit::TestCase
assert @coll.pk_factory.is_a?(Object)
end
class TestPK
def self.create_pk
end
end
def test_pk_factory_on_collection
@coll = Collection.new(@@db, 'foo', TestPK)
assert_equal TestPK, @coll.pk_factory
@coll2 = Collection.new(@@db, 'foo', :pk => TestPK)
assert_equal TestPK, @coll2.pk_factory
end
def test_valid_names
assert_raise Mongo::InvalidNSName do
@@db["te$t"]

View File

@ -48,7 +48,7 @@ class SafeTest < Test::Unit::TestCase
col = @db.collection('bar', :safe => false)
assert_equal false, col.safe
col = Collection.new(@db, 'bar', nil, :safe => false)
col = Collection.new(@db, 'bar', :safe => false)
assert_equal false, col.safe
end
end