From 1124c05f1fa113a46fbe0aa97a11ab80bc68e7fb Mon Sep 17 00:00:00 2001 From: Mike Dirolf Date: Thu, 27 Aug 2009 16:29:41 -0400 Subject: [PATCH] DB#collections method, thanks to Durran Jordan and Les Hill --- README.rdoc | 3 +++ lib/mongo/db.rb | 8 ++++++++ test/test_collection.rb | 6 ++++-- test/test_db.rb | 12 ++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/README.rdoc b/README.rdoc index c8bafc5..c4756cb 100644 --- a/README.rdoc +++ b/README.rdoc @@ -297,6 +297,9 @@ Paul Dlug, paul.dlug@gmail.com * Generate _id on the client side if not provided * Collection#insert and Collection#save return _id +Durran Jordan and Les Hill, durran@gmail.com +* DB#collections + = License Copyright 2008-2009 10gen Inc. diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 0990819..31ebc22 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -192,6 +192,14 @@ module Mongo names.map {|name| name.sub(@name + '.', '')} 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 # 'name' string and optionally an 'options' hash. If +coll_name+ is # specified, an array of length 1 is returned. diff --git a/test/test_collection.rb b/test/test_collection.rb index dc31149..f9481c5 100644 --- a/test/test_collection.rb +++ b/test/test_collection.rb @@ -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 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 def test_group_with_scope diff --git a/test/test_db.rb b/test/test_db.rb index ac058fb..d5dfe28 100644 --- a/test/test_db.rb +++ b/test/test_db.rb @@ -63,6 +63,18 @@ class DBTest < Test::Unit::TestCase } 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 @@db.close @@users = nil