New ObjectID.from_string method

This commit is contained in:
Jim Menard 2009-01-09 13:34:30 -05:00
parent 5d71afa737
commit 5b83544b9c
2 changed files with 20 additions and 0 deletions

View File

@ -53,6 +53,18 @@ module XGen
@@index_time = Time.new.to_i
@@index = 0
# Given a string representation of an ObjectID, return a new ObjectID
# with that value.
def self.from_string(str)
data = []
i = str.to_i(16)
while i > 0
data << (i & 0xff)
i >>= 8
end
self.new(data.reverse)
end
# +data+ is an array of bytes. If nil, a new id will be generated.
# The time +t+ is only used for testing; leave it nil.
def initialize(data=nil, t=nil)

View File

@ -65,4 +65,12 @@ class ObjectIDTest < Test::Unit::TestCase
assert_equal @o, row['_id']
end
def test_from_string
hex_str = @o.to_s
o2 = ObjectID.from_string(hex_str)
assert_equal hex_str, o2.to_s
assert_equal @o, o2
assert_equal @o.to_s, o2.to_s
end
end