Class: Mongo::ObjectID
- Inherits:
-
Object
- Object
- Mongo::ObjectID
- Defined in:
- lib/mongo/types/objectid.rb
Overview
Representation of an ObjectId for Mongo.
Constant Summary collapse
- BYTE_ORDER =
This is the legacy byte ordering for Babble. Versions of the Ruby driver prior to 0.14 used this byte ordering when converting ObjectID instances to and from strings. If you have string representations of ObjectIDs using the legacy byte ordering make sure to use the to_s_legacy and from_string_legacy methods, or convert your strings with ObjectID#legacy_string_convert
[7, 6, 5, 4, 3, 2, 1, 0, 11, 10, 9, 8]
- LOCK =
Object.new
- @@index =
0
Class Method Summary collapse
-
.from_string(str) ⇒ Object
Given a string representation of an ObjectID, return a new ObjectID with that value.
-
.from_string_legacy(str) ⇒ Object
Create a new ObjectID given a string representation of an ObjectID using the legacy byte ordering.
-
.legacy_string_convert(str) ⇒ Object
Convert a string representation of an ObjectID using the legacy byte ordering to the proper byte ordering.
- .legal?(str) ⇒ Boolean
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(data = nil) ⇒ ObjectID
constructor
data
is an array of bytes. - #to_a ⇒ Object
- #to_s ⇒ Object
-
#to_s_legacy ⇒ Object
Get a string representation of this ObjectID using the legacy byte ordering.
Constructor Details
#initialize(data = nil) ⇒ ObjectID
data
is an array of bytes. If nil, a new id will be generated.
46 47 48 |
# File 'lib/mongo/types/objectid.rb', line 46 def initialize(data=nil) @data = data || generate end |
Class Method Details
.from_string(str) ⇒ Object
Given a string representation of an ObjectID, return a new ObjectID with that value.
61 62 63 64 65 66 67 68 |
# File 'lib/mongo/types/objectid.rb', line 61 def self.from_string(str) raise "illegal ObjectID format" unless legal?(str) data = [] 12.times do |i| data[i] = str[i * 2, 2].to_i(16) end self.new(data) end |
.from_string_legacy(str) ⇒ Object
Create a new ObjectID given a string representation of an ObjectID using the legacy byte ordering. This method may eventually be removed. If you are not sure that you need this method you should be using the regular from_string.
74 75 76 77 78 79 80 81 |
# File 'lib/mongo/types/objectid.rb', line 74 def self.from_string_legacy(str) raise "illegal ObjectID format" unless legal?(str) data = [] BYTE_ORDER.each_with_index { |string_position, data_index| data[data_index] = str[string_position * 2, 2].to_i(16) } self.new(data) end |
.legacy_string_convert(str) ⇒ Object
Convert a string representation of an ObjectID using the legacy byte ordering to the proper byte ordering. This method may eventually be removed. If you are not sure that you need this method it is probably unnecessary.
106 107 108 109 110 111 112 |
# File 'lib/mongo/types/objectid.rb', line 106 def self.legacy_string_convert(str) legacy = ' ' * 24 BYTE_ORDER.each_with_index do |legacy_pos, pos| legacy[legacy_pos * 2, 2] = str[pos * 2, 2] end legacy end |
.legal?(str) ⇒ Boolean
38 39 40 41 42 43 |
# File 'lib/mongo/types/objectid.rb', line 38 def self.legal?(str) len = BYTE_ORDER.length * 2 str =~ /([0-9a-f]+)/i match = $1 str && str.length == len && match == str end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
50 51 52 |
# File 'lib/mongo/types/objectid.rb', line 50 def eql?(other) @data == other.instance_variable_get("@data") end |
#to_a ⇒ Object
55 56 57 |
# File 'lib/mongo/types/objectid.rb', line 55 def to_a @data.dup end |
#to_s ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/mongo/types/objectid.rb', line 83 def to_s str = ' ' * 24 12.times do |i| str[i * 2, 2] = '%02x' % @data[i] end str end |
#to_s_legacy ⇒ Object
Get a string representation of this ObjectID using the legacy byte ordering. This method may eventually be removed. If you are not sure that you need this method you should be using the regular to_s.
94 95 96 97 98 99 100 |
# File 'lib/mongo/types/objectid.rb', line 94 def to_s_legacy str = ' ' * 24 BYTE_ORDER.each_with_index { |string_position, data_index| str[string_position * 2, 2] = '%02x' % @data[data_index] } str end |