find_one takes same options as find

This commit is contained in:
Mike Dirolf 2009-08-14 16:43:12 -04:00
parent 28a80f1b5e
commit 402b895385
2 changed files with 8 additions and 7 deletions

View File

@ -98,7 +98,8 @@ module XGen
# present for a document to be included in the result set OR an
# instance of ObjectID to be used as the value for an _id query.
# if nil an empty spec, {}, will be used.
def find_one(spec_or_object_id=nil)
# :options :: options, as passed to Collection#find
def find_one(spec_or_object_id=nil, options={})
spec = case spec_or_object_id
when nil
{}
@ -109,7 +110,7 @@ module XGen
else
raise TypeError, "spec_or_object_id must be an instance of ObjectID or Hash, or nil"
end
find(spec, :limit => -1).next_object
find(spec, options.merge(:limit => -1)).next_object
end
# DEPRECATED - use find_one instead
@ -117,10 +118,7 @@ module XGen
# Find the first record that matches +selector+. See #find.
def find_first(selector={}, options={})
warn "Collection#find_first is deprecated and will be removed. Please use Collection#find_one instead."
h = options.dup
h[:limit] = 1
cursor = find(selector, h)
cursor.next_object # don't need to explicitly close b/c of limit
find_one(selector, options)
end
# Save a document in this collection.

View File

@ -88,7 +88,7 @@ class TestCollection < Test::Unit::TestCase
end
def test_find_one
id = @@test.save("hello" => "world")
id = @@test.save("hello" => "world", "foo" => "bar")
assert_equal "world", @@test.find_one()["hello"]
assert_equal @@test.find_one(id), @@test.find_one()
@ -97,6 +97,9 @@ class TestCollection < Test::Unit::TestCase
assert_equal @@test.find_one("hello" => "world"), @@test.find_one()
assert_equal @@test.find_one(OrderedHash["hello", "world"]), @@test.find_one()
assert @@test.find_one(nil, :fields => ["hello"]).include?("hello")
assert !@@test.find_one(nil, :fields => ["foo"]).include?("hello")
assert_equal nil, @@test.find_one("hello" => "foo")
assert_equal nil, @@test.find_one(OrderedHash["hello", "foo"])
assert_equal nil, @@test.find_one(ObjectID.new)