Skip to content

Commit af0ecde

Browse files
committed
RUBY-204 Collection construct now has analogous API
to DB constructor (i.e., name comes first)
1 parent fa58376 commit af0ecde

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

lib/mongo/collection.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class Collection
2424

2525
# Initialize a collection object.
2626
#
27-
# @param [DB] db a MongoDB database instance.
2827
# @param [String, Symbol] name the name of the collection.
28+
# @param [DB] db a MongoDB database instance.
2929
#
3030
# @option options [:create_pk] :pk (BSON::ObjectId) A primary key factory to use
3131
# other than the default BSON::ObjectId.
@@ -44,7 +44,13 @@ class Collection
4444
# @return [Collection]
4545
#
4646
# @core collections constructor_details
47-
def initialize(db, name, options={})
47+
def initialize(name, db, options={})
48+
if db.is_a?(String) && name.is_a?(Mongo::DB)
49+
warn "Warning: the order of parameters to initialize a collection have changed. " +
50+
"Please specify the collection name first, followed by the db."
51+
db, name = name, db
52+
end
53+
4854
case name
4955
when Symbol, String
5056
else
@@ -96,7 +102,7 @@ def initialize(db, name, options={})
96102
# the specified sub-collection
97103
def [](name)
98104
name = "#{self.name}.#{name}"
99-
return Collection.new(db, name) if !db.strict? || db.collection_names.include?(name)
105+
return Collection.new(name, db) if !db.strict? || db.collection_names.include?(name)
100106
raise "Collection #{name} doesn't exist. Currently in strict mode."
101107
end
102108

lib/mongo/db.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def strict?; @strict; end
5656

5757
# Instances of DB are normally obtained by calling Mongo#db.
5858
#
59-
# @param [String] db_name the database name.
59+
# @param [String] name the database name.
6060
# @param [Mongo::Connection] connection a connection object pointing to MongoDB. Note
6161
# that databases are usually instantiated via the Connection class. See the examples below.
6262
#
@@ -76,8 +76,8 @@ def strict?; @strict; end
7676
# @option options [Integer] :cache_time (300) Set the time that all ensure_index calls should cache the command.
7777
#
7878
# @core databases constructor_details
79-
def initialize(db_name, connection, options={})
80-
@name = Mongo::Support.validate_db_name(db_name)
79+
def initialize(name, connection, options={})
80+
@name = Mongo::Support.validate_db_name(name)
8181
@connection = connection
8282
@strict = options[:strict]
8383
@pk_factory = options[:pk]
@@ -208,8 +208,8 @@ def collection_names
208208
#
209209
# @return [Array<Mongo::Collection>]
210210
def collections
211-
collection_names.map do |collection_name|
212-
Collection.new(self, collection_name)
211+
collection_names.map do |name|
212+
Collection.new(name, self)
213213
end
214214
end
215215

@@ -223,7 +223,7 @@ def collections
223223
def collections_info(coll_name=nil)
224224
selector = {}
225225
selector[:name] = full_collection_name(coll_name) if coll_name
226-
Cursor.new(Collection.new(self, SYSTEM_NAMESPACE_COLLECTION), :selector => selector)
226+
Cursor.new(Collection.new(SYSTEM_NAMESPACE_COLLECTION, self), :selector => selector)
227227
end
228228

229229
# Create a collection.
@@ -252,15 +252,15 @@ def create_collection(name, options={})
252252
if strict?
253253
raise MongoDBError, "Collection #{name} already exists. Currently in strict mode."
254254
else
255-
return Collection.new(self, name)
255+
return Collection.new(name, self)
256256
end
257257
end
258258

259259
# Create a new collection.
260260
oh = BSON::OrderedHash.new
261261
oh[:create] = name
262262
doc = command(oh.merge(options || {}))
263-
return Collection.new(self, name, :pk => @pk_factory) if ok?(doc)
263+
return Collection.new(name, self, :pk => @pk_factory) if ok?(doc)
264264
raise MongoDBError, "Error creating collection: #{doc.inspect}"
265265
end
266266

@@ -278,7 +278,7 @@ def collection(name, options={})
278278
else
279279
options[:safe] = options.fetch(:safe, @safe)
280280
options.merge!(:pk => @pk_factory) unless options[:pk]
281-
Collection.new(self, name, options)
281+
Collection.new(name, self, options)
282282
end
283283
end
284284
alias_method :[], :collection
@@ -418,7 +418,7 @@ def drop_index(collection_name, index_name)
418418
def index_information(collection_name)
419419
sel = {:ns => full_collection_name(collection_name)}
420420
info = {}
421-
Cursor.new(Collection.new(self, SYSTEM_INDEX_COLLECTION), :selector => sel).each do |index|
421+
Cursor.new(Collection.new(SYSTEM_INDEX_COLLECTION, self), :selector => sel).each do |index|
422422
info[index['name']] = index
423423
end
424424
info
@@ -558,7 +558,7 @@ def profiling_level=(level)
558558
#
559559
# @return [Array] a list of documents containing profiling information.
560560
def profiling_info
561-
Cursor.new(Collection.new(self, DB::SYSTEM_PROFILE_COLLECTION), :selector => {}).to_a
561+
Cursor.new(Collection.new(SYSTEM_PROFILE_COLLECTION, self), :selector => {}).to_a
562562
end
563563

564564
# Validate a named collection.
@@ -581,7 +581,7 @@ def validate_collection(name)
581581
private
582582

583583
def system_command_collection
584-
Collection.new(self, SYSTEM_COMMAND_COLLECTION)
584+
Collection.new(SYSTEM_COMMAND_COLLECTION, self)
585585
end
586586
end
587587
end

test/collection_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ def self.create_pk
3232
end
3333

3434
def test_pk_factory_on_collection
35-
@coll = Collection.new(@@db, 'foo', TestPK)
35+
@coll = Collection.new('foo', @@db, TestPK)
3636
assert_equal TestPK, @coll.pk_factory
3737

3838

39-
@coll2 = Collection.new(@@db, 'foo', :pk => TestPK)
39+
@coll2 = Collection.new('foo', @@db, :pk => TestPK)
4040
assert_equal TestPK, @coll2.pk_factory
4141
end
4242

test/unit/safe_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ class SafeTest < Test::Unit::TestCase
4040
col = @db['bar']
4141
assert_equal @safe_value, col.safe
4242

43-
col = Collection.new(@db, 'bar')
43+
col = Collection.new('bar', @db)
4444
assert_equal @safe_value, col.safe
4545
end
4646

4747
should "allow override on collection" do
4848
col = @db.collection('bar', :safe => false)
4949
assert_equal false, col.safe
5050

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

0 commit comments

Comments
 (0)