Skip to content

Commit a2f5019

Browse files
committed
Specify pk factory on Collection.new as a :pk option
1 parent 888c318 commit a2f5019

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

lib/mongo/collection.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class Collection
2828
# @param [DB] db a MongoDB database instance.
2929
# @param [String, Symbol] name the name of the collection.
3030
#
31+
# @option options [:create_pk] :pk (BSON::ObjectId) A primary key factory to use
32+
# other than the default BSON::ObjectId.
33+
#
3134
# @option options [Boolean, Hash] :safe (false) Set the default safe-mode options
3235
# for insert, update, and remove method called on this Collection instance. If no
3336
# value is provided, the default value set on this instance's DB will be used. This
@@ -42,7 +45,7 @@ class Collection
4245
# @return [Collection]
4346
#
4447
# @core collections constructor_details
45-
def initialize(db, name, pk_factory=nil, options={})
48+
def initialize(db, name, options={})
4649
case name
4750
when Symbol, String
4851
else
@@ -61,11 +64,21 @@ def initialize(db, name, pk_factory=nil, options={})
6164
raise Mongo::InvalidNSName, "collection names must not start or end with '.'"
6265
end
6366

67+
if options.respond_to?(:create_pk) || !options.is_a?(Hash)
68+
warn "The method for specifying a primary key factory on a Collection has changed.\n" +
69+
"Please specify it as an option (e.g., :pk => PkFactory)."
70+
pk_factory = options
71+
else
72+
pk_factory = nil
73+
end
74+
6475
@db, @name = db, name
6576
@connection = @db.connection
6677
@logger = @connection.logger
67-
@pk_factory = pk_factory || BSON::ObjectId
68-
@safe = options.has_key?(:safe) ? options[:safe] : @db.safe
78+
unless pk_factory
79+
@safe = options.has_key?(:safe) ? options[:safe] : @db.safe
80+
end
81+
@pk_factory = pk_factory || options[:pk] || BSON::ObjectId
6982
@hint = nil
7083
end
7184

lib/mongo/db.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,14 @@ def create_collection(name, options={})
255255
oh = BSON::OrderedHash.new
256256
oh[:create] = name
257257
doc = command(oh.merge(options || {}))
258-
return Collection.new(self, name, @pk_factory) if ok?(doc)
258+
return Collection.new(self, name, :pk => @pk_factory) if ok?(doc)
259259
raise MongoDBError, "Error creating collection: #{doc.inspect}"
260260
end
261261

262262
# Get a collection by name.
263263
#
264264
# @param [String] name the collection name.
265+
# @param [Hash] options any valid options that can me passed to Collection#new.
265266
#
266267
# @raise [MongoDBError] if collection does not already exist and we're in +strict+ mode.
267268
#
@@ -271,7 +272,8 @@ def collection(name, options={})
271272
raise Mongo::MongoDBError, "Collection #{name} doesn't exist. Currently in strict mode."
272273
else
273274
options[:safe] = options.has_key?(:safe) ? options[:safe] : @safe
274-
Collection.new(self, name, @pk_factory, options)
275+
options.merge!(:pk => @pk_factory) unless options[:pk]
276+
Collection.new(self, name, options)
275277
end
276278
end
277279
alias_method :[], :collection

test/collection_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ def test_optional_pk_factory
2626
assert @coll.pk_factory.is_a?(Object)
2727
end
2828

29+
class TestPK
30+
def self.create_pk
31+
end
32+
end
33+
34+
def test_pk_factory_on_collection
35+
@coll = Collection.new(@@db, 'foo', TestPK)
36+
assert_equal TestPK, @coll.pk_factory
37+
38+
39+
@coll2 = Collection.new(@@db, 'foo', :pk => TestPK)
40+
assert_equal TestPK, @coll2.pk_factory
41+
end
42+
2943
def test_valid_names
3044
assert_raise Mongo::InvalidNSName do
3145
@@db["te$t"]

test/unit/safe_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class SafeTest < Test::Unit::TestCase
4848
col = @db.collection('bar', :safe => false)
4949
assert_equal false, col.safe
5050

51-
col = Collection.new(@db, 'bar', nil, :safe => false)
51+
col = Collection.new(@db, 'bar', :safe => false)
5252
assert_equal false, col.safe
5353
end
5454
end

0 commit comments

Comments
 (0)