DB#collections method, thanks to Durran Jordan and Les Hill

This commit is contained in:
Mike Dirolf 2009-08-27 16:29:41 -04:00
parent 734edf6c65
commit 1124c05f1f
4 changed files with 27 additions and 2 deletions

View File

@ -297,6 +297,9 @@ Paul Dlug, paul.dlug@gmail.com
* Generate _id on the client side if not provided * Generate _id on the client side if not provided
* Collection#insert and Collection#save return _id * Collection#insert and Collection#save return _id
Durran Jordan and Les Hill, durran@gmail.com
* DB#collections
= License = License
Copyright 2008-2009 10gen Inc. Copyright 2008-2009 10gen Inc.

View File

@ -192,6 +192,14 @@ module Mongo
names.map {|name| name.sub(@name + '.', '')} names.map {|name| name.sub(@name + '.', '')}
end end
# Retruns an array of Collection instances, one for each collection in this
# database.
def collections
collection_names.map do |collection_name|
Collection.new(self, collection_name)
end
end
# Returns a cursor over query result hashes. Each hash contains a # Returns a cursor over query result hashes. Each hash contains a
# 'name' string and optionally an 'options' hash. If +coll_name+ is # 'name' string and optionally an 'options' hash. If +coll_name+ is
# specified, an array of length 1 is returned. # specified, an array of length 1 is returned.

View File

@ -183,8 +183,10 @@ class TestCollection < Test::Unit::TestCase
assert_equal :mike, @@test.find_one(:foo => :mike)["foo"] assert_equal :mike, @@test.find_one(:foo => :mike)["foo"]
assert_equal :mike, @@test.find_one("foo" => :mike)["foo"] assert_equal :mike, @@test.find_one("foo" => :mike)["foo"]
assert_equal nil, @@test.find_one(:foo => "mike")
assert_equal nil, @@test.find_one("foo" => "mike") # TODO enable these tests conditionally based on server version (if >1.0)
# assert_equal :mike, @@test.find_one(:foo => "mike")["foo"]
# assert_equal :mike, @@test.find_one("foo" => "mike")["foo"]
end end
def test_group_with_scope def test_group_with_scope

View File

@ -63,6 +63,18 @@ class DBTest < Test::Unit::TestCase
} }
end end
def test_collections
@@db.collection("test.durran").insert("foo" => 5)
@@db.collection("test.les").insert("bar" => 0)
colls = @@db.collections()
assert_not_nil colls.select { |coll| coll.name == "test.durran" }
assert_not_nil colls.select { |coll| coll.name == "test.les" }
assert_equal [], colls.select { |coll| coll.name == "does_not_exist" }
assert_kind_of Collection, colls[0]
end
def test_pair def test_pair
@@db.close @@db.close
@@users = nil @@users = nil