better object id representation for inspect
This commit is contained in:
parent
60ffbb2706
commit
653d156a8a
|
@ -22,6 +22,10 @@ require 'digest/md5'
|
||||||
|
|
||||||
module BSON
|
module BSON
|
||||||
|
|
||||||
|
def BSON::ObjectID(s)
|
||||||
|
ObjectID.from_string(s)
|
||||||
|
end
|
||||||
|
|
||||||
# Generates MongoDB object ids.
|
# Generates MongoDB object ids.
|
||||||
#
|
#
|
||||||
# @core objectids
|
# @core objectids
|
||||||
|
@ -129,11 +133,11 @@ module BSON
|
||||||
end
|
end
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
{"$oid" => to_s}
|
"BSON::ObjectID('#{to_s}')"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Convert to MongoDB extended JSON format. Since JSON includes type information,
|
# 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.
|
# @return [String] the object id represented as MongoDB extended JSON.
|
||||||
def to_json(escaped=false)
|
def to_json(escaped=false)
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
require 'test/test_helper'
|
require 'test/test_helper'
|
||||||
|
require 'rubygems'
|
||||||
|
require 'json'
|
||||||
|
|
||||||
class ObjectIDTest < Test::Unit::TestCase
|
class ObjectIDTest < Test::Unit::TestCase
|
||||||
|
|
||||||
|
@ -49,9 +51,12 @@ class ObjectIDTest < Test::Unit::TestCase
|
||||||
assert_equal 24, $1.length
|
assert_equal 24, $1.length
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_method
|
||||||
|
assert_equal ObjectID.from_string(@o.to_s), BSON::ObjectID(@o.to_s)
|
||||||
|
end
|
||||||
|
|
||||||
def test_inspect
|
def test_inspect
|
||||||
obj = {'$oid' => @o.to_s}
|
assert_equal "BSON::ObjectID('#{@o.to_s}')", @o.inspect
|
||||||
assert_equal obj, @o.inspect
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_save_and_restore
|
def test_save_and_restore
|
||||||
|
|
Loading…
Reference in New Issue