From 653d156a8a03e292a649ee05f3e27c2a7e9990d8 Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Tue, 18 May 2010 12:22:19 -0400 Subject: [PATCH] better object id representation for inspect --- lib/bson/types/objectid.rb | 8 ++++++-- test/mongo_bson/objectid_test.rb | 9 +++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/bson/types/objectid.rb b/lib/bson/types/objectid.rb index 10cbb11..edb7ff3 100644 --- a/lib/bson/types/objectid.rb +++ b/lib/bson/types/objectid.rb @@ -22,6 +22,10 @@ require 'digest/md5' module BSON + def BSON::ObjectID(s) + ObjectID.from_string(s) + end + # Generates MongoDB object ids. # # @core objectids @@ -129,11 +133,11 @@ module BSON end def inspect - {"$oid" => to_s} + "BSON::ObjectID('#{to_s}')" end # Convert to MongoDB extended JSON format. Since JSON includes type information, - # but lacks an ObjectID type, this JSON format encodes the type using an $id key. + # but lacks an ObjectID type, this JSON format encodes the type using an $oid key. # # @return [String] the object id represented as MongoDB extended JSON. def to_json(escaped=false) diff --git a/test/mongo_bson/objectid_test.rb b/test/mongo_bson/objectid_test.rb index 4b0d562..307c3ab 100644 --- a/test/mongo_bson/objectid_test.rb +++ b/test/mongo_bson/objectid_test.rb @@ -1,4 +1,6 @@ require 'test/test_helper' +require 'rubygems' +require 'json' class ObjectIDTest < Test::Unit::TestCase @@ -49,9 +51,12 @@ class ObjectIDTest < Test::Unit::TestCase assert_equal 24, $1.length end + def test_method + assert_equal ObjectID.from_string(@o.to_s), BSON::ObjectID(@o.to_s) + end + def test_inspect - obj = {'$oid' => @o.to_s} - assert_equal obj, @o.inspect + assert_equal "BSON::ObjectID('#{@o.to_s}')", @o.inspect end def test_save_and_restore