New ObjectID.from_string method
This commit is contained in:
parent
5d71afa737
commit
5b83544b9c
|
@ -53,6 +53,18 @@ module XGen
|
||||||
@@index_time = Time.new.to_i
|
@@index_time = Time.new.to_i
|
||||||
@@index = 0
|
@@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.
|
# +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.
|
# The time +t+ is only used for testing; leave it nil.
|
||||||
def initialize(data=nil, t=nil)
|
def initialize(data=nil, t=nil)
|
||||||
|
|
|
@ -65,4 +65,12 @@ class ObjectIDTest < Test::Unit::TestCase
|
||||||
assert_equal @o, row['_id']
|
assert_equal @o, row['_id']
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue