dereference helper method

This commit is contained in:
Mike Dirolf 2009-04-28 14:55:36 -04:00
parent a153eae543
commit cea73c2712
2 changed files with 19 additions and 0 deletions

View File

@ -372,6 +372,11 @@ module XGen
raise "Error with count command: #{doc.inspect}"
end
# Dereference a DBRef, getting the document it points to.
def dereference(dbref)
collection(dbref.namespace).find_first("_id" => dbref.object_id)
end
# Evaluate a JavaScript expression on MongoDB.
# +code+ should be a string or Code instance containing a JavaScript
# expression. Additional arguments will be passed to that expression

View File

@ -544,6 +544,20 @@ class DBAPITest < Test::Unit::TestCase
assert_equal 1, test.group([], {"a" => {"$gt" => 1}}, {"count" => 0}, "function (obj, prev) { prev.count++; }")[0]["count"]
end
def test_deref
@@coll.clear
assert_equal nil, @@db.dereference(DBRef.new("test", ObjectID.new))
obj = {"x" => true}
key = @@coll.insert(obj)["_id"]
assert_equal true, @@db.dereference(DBRef.new("test", key))["x"]
assert_equal nil, @@db.dereference(DBRef.new("test", 4))
obj = {"_id" => 4}
@@coll.insert(obj)
assert_equal obj, @@db.dereference(DBRef.new("test", 4))
end
# TODO this test fails with error message "Undefed Before end of object"
# That is a database error. The undefined type may go away.