Class: BSON::ObjectID
- Inherits:
-
Object
- Object
- BSON::ObjectID
- Defined in:
- lib/bson/types/objectid.rb
Overview
Generates MongoDB object ids.
Constant Summary collapse
- @@lock =
Mutex.new
- @@index =
0
Class Method Summary collapse
-
.create_pk(doc) ⇒ Mongo::ObjectID, Object
Adds a primary key to the given document if needed.
-
.from_string(str) ⇒ Mongo::ObjectID
Given a string representation of an ObjectID, return a new ObjectID with that value.
-
.from_time(time) ⇒ Mongo::ObjectID
Create an object id from the given time.
-
.legal?(str) ⇒ Boolean
Determine if the supplied string is legal.
Instance Method Summary collapse
-
#eql?(object_id) ⇒ Boolean
(also: #==)
Check equality of this object id with another.
-
#generation_time ⇒ Time
Return the UTC time at which this ObjectID was generated.
-
#hash ⇒ Integer
Get a unique hashcode for this object.
- #initialize(data = nil) ⇒ ObjectID constructor deprecated Deprecated.
- #inspect ⇒ Object
-
#to_a ⇒ Array
Get an array representation of the object id.
-
#to_json(*a) ⇒ String
Convert to MongoDB extended JSON format.
-
#to_s ⇒ String
Get a string representation of this object id.
Constructor Details
#initialize(data = nil) ⇒ ObjectID
Create a new object id. If no parameter is given, an id corresponding to the ObjectID BSON data type will be created. This is a 12-byte value consisting of a 4-byte timestamp, a 3-byte machine id, a 2-byte process id, and a 3-byte counter.
45 46 47 48 |
# File 'lib/bson/types/objectid.rb', line 45 def initialize(data=nil) warn "BSON::ObjectID is deprecated. Please use BSON::ObjectId instead." @data = data || generate end |
Class Method Details
.create_pk(doc) ⇒ Mongo::ObjectID, Object
Adds a primary key to the given document if needed.
85 86 87 |
# File 'lib/bson/types/objectid.rb', line 85 def self.create_pk(doc) doc.has_key?(:_id) || doc.has_key?('_id') ? doc : doc.merge!(:_id => self.new) end |
.from_string(str) ⇒ Mongo::ObjectID
Given a string representation of an ObjectID, return a new ObjectID with that value.
118 119 120 121 122 123 124 125 |
# File 'lib/bson/types/objectid.rb', line 118 def self.from_string(str) raise InvalidObjectID, "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_time(time) ⇒ Mongo::ObjectID
Create an object id from the given time. This is useful for doing range queries; it works because MongoDB’s object ids begin with a timestamp.
75 76 77 |
# File 'lib/bson/types/objectid.rb', line 75 def self.from_time(time) self.new([time.to_i,0,0].pack("NNN").unpack("C12")) end |
.legal?(str) ⇒ Boolean
Determine if the supplied string is legal. Legal strings will consist of 24 hexadecimal characters.
56 57 58 59 60 61 |
# File 'lib/bson/types/objectid.rb', line 56 def self.legal?(str) len = 24 str =~ /([0-9a-f]+)/i match = $1 str && str.length == len && match == str end |
Instance Method Details
#eql?(object_id) ⇒ Boolean Also known as: ==
Check equality of this object id with another.
92 93 94 |
# File 'lib/bson/types/objectid.rb', line 92 def eql?(object_id) @data == object_id.instance_variable_get("@data") end |
#generation_time ⇒ Time
Return the UTC time at which this ObjectID was generated. This may be used in lieu of a created_at timestamp since this information is always encoded in the object id.
155 156 157 |
# File 'lib/bson/types/objectid.rb', line 155 def generation_time Time.at(@data.pack("C4").unpack("N")[0]).utc end |
#hash ⇒ Integer
Get a unique hashcode for this object. This is required since we’ve defined an #eql? method.
101 102 103 |
# File 'lib/bson/types/objectid.rb', line 101 def hash @data.hash end |
#inspect ⇒ Object
138 139 140 |
# File 'lib/bson/types/objectid.rb', line 138 def inspect "BSON::ObjectID('#{to_s}')" end |
#to_a ⇒ Array
Get an array representation of the object id.
108 109 110 |
# File 'lib/bson/types/objectid.rb', line 108 def to_a @data.dup end |
#to_json(*a) ⇒ String
Convert to MongoDB extended JSON format. Since JSON includes type information, but lacks an ObjectID type, this JSON format encodes the type using an $oid key.
146 147 148 |
# File 'lib/bson/types/objectid.rb', line 146 def to_json(*a) "{\"$oid\": \"#{to_s}\"}" end |
#to_s ⇒ String
Get a string representation of this object id.
130 131 132 133 134 135 136 |
# File 'lib/bson/types/objectid.rb', line 130 def to_s str = ' ' * 24 12.times do |i| str[i * 2, 2] = '%02x' % @data[i] end str end |