Class: BSON::ObjectId
- Inherits:
-
Object
- Object
- BSON::ObjectId
- Includes:
- JSON, Comparable
- Defined in:
- lib/bson/object_id.rb
Overview
Represents object_id data.
Defined Under Namespace
Constant Summary collapse
- BSON_TYPE =
A object_id is type 0x07 in the BSON spec.
7.chr.force_encoding(BINARY).freeze
Class Method Summary collapse
-
.from_bson(bson) ⇒ BSON::ObjectId
Deserialize the object id from raw BSON bytes.
-
.from_data(data) ⇒ ObjectId
Create a new object id from raw bytes.
-
.from_string(string) ⇒ BSON::ObjectId
Create a new object id from a string.
-
.from_time(time, options = {}) ⇒ ObjectId
Create a new object id from a time.
-
.legal?(string) ⇒ true, false
Determine if the provided string is a legal object id.
-
.repair(object) ⇒ String
Executes the provided block only if the size of the provided object is 12.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compare this object id with another object for use in sorting.
-
#==(other) ⇒ true, false
(also: #eql?)
Check equality of the object id with another object.
-
#===(other) ⇒ true, false
Check case equality on the object id.
-
#as_json(*args) ⇒ Hash
Return the object id as a JSON hash representation.
-
#generation_time ⇒ Time
Return the UTC time at which this ObjectId was generated.
-
#hash ⇒ Integer
Get the hash value for the object id.
-
#inspect ⇒ String
Get a nice string for use with object inspection.
-
#marshal_dump ⇒ String
Dump the raw bson when calling Marshal.dump.
-
#marshal_load(data) ⇒ String
Unmarshal the data into an object id.
-
#to_bson(encoded = ''.force_encoding(BINARY)) ⇒ String
Get the object id as it’s raw BSON data.
-
#to_s ⇒ String
(also: #to_str)
Get the string representation of the object id.
Methods included from JSON
Class Method Details
.from_bson(bson) ⇒ BSON::ObjectId
Deserialize the object id from raw BSON bytes.
214 215 216 |
# File 'lib/bson/object_id.rb', line 214 def from_bson(bson) from_data(bson.read(12)) end |
.from_data(data) ⇒ ObjectId
Create a new object id from raw bytes.
228 229 230 231 232 |
# File 'lib/bson/object_id.rb', line 228 def from_data(data) object_id = allocate object_id.instance_variable_set(:@raw_data, data) object_id end |
.from_string(string) ⇒ BSON::ObjectId
Create a new object id from a string.
246 247 248 249 250 251 |
# File 'lib/bson/object_id.rb', line 246 def from_string(string) unless legal?(string) raise Invalid.new("'#{string}' is an invalid ObjectId.") end from_data([ string ].pack("H*")) end |
.from_time(time, options = {}) ⇒ ObjectId
Create a new object id from a time.
270 271 272 |
# File 'lib/bson/object_id.rb', line 270 def from_time(time, = {}) from_data([:unique] ? @@generator.next_object_id(time.to_i) : [ time.to_i ].pack("Nx8")) end |
.legal?(string) ⇒ true, false
Determine if the provided string is a legal object id.
284 285 286 |
# File 'lib/bson/object_id.rb', line 284 def legal?(string) string.to_s =~ /\A[0-9a-f]{24}\z/i ? true : false end |
.repair(object) ⇒ String
Executes the provided block only if the size of the provided object is
-
Used in legacy id repairs.
301 302 303 304 305 306 307 |
# File 'lib/bson/object_id.rb', line 301 def repair(object) if object.size == 12 block_given? ? yield(object) : object else raise Invalid.new("#{object.inspect} is not a valid object id.") end end |
Instance Method Details
#<=>(other) ⇒ Integer
Compare this object id with another object for use in sorting.
88 89 90 |
# File 'lib/bson/object_id.rb', line 88 def <=>(other) to_bson <=> other.to_bson end |
#==(other) ⇒ true, false Also known as: eql?
Check equality of the object id with another object.
45 46 47 48 |
# File 'lib/bson/object_id.rb', line 45 def ==(other) return false unless other.is_a?(ObjectId) to_bson == other.to_bson end |
#===(other) ⇒ true, false
Check case equality on the object id.
61 62 63 64 |
# File 'lib/bson/object_id.rb', line 61 def ===(other) return to_str === other.to_str if other.respond_to?(:to_str) super end |
#as_json(*args) ⇒ Hash
Return the object id as a JSON hash representation.
74 75 76 |
# File 'lib/bson/object_id.rb', line 74 def as_json(*args) { "$oid" => to_s } end |
#generation_time ⇒ Time
Return the UTC time at which this ObjectId was generated. This may be used instread of a created_at timestamp since this information is always encoded in the object id.
102 103 104 |
# File 'lib/bson/object_id.rb', line 102 def generation_time ::Time.at(to_bson.unpack("N")[0]).utc end |
#hash ⇒ Integer
Get the hash value for the object id.
114 115 116 |
# File 'lib/bson/object_id.rb', line 114 def hash to_bson.hash end |
#inspect ⇒ String
Get a nice string for use with object inspection.
126 127 128 |
# File 'lib/bson/object_id.rb', line 126 def inspect "BSON::ObjectId('#{to_s}')" end |
#marshal_dump ⇒ String
Dump the raw bson when calling Marshal.dump.
138 139 140 |
# File 'lib/bson/object_id.rb', line 138 def marshal_dump to_bson end |
#marshal_load(data) ⇒ String
Unmarshal the data into an object id.
152 153 154 |
# File 'lib/bson/object_id.rb', line 152 def marshal_load(data) @raw_data = data end |
#to_bson(encoded = ''.force_encoding(BINARY)) ⇒ String
Since Moped’s BSON and MongoDB BSON before 2.0.0 have different internal representations, we will attempt to repair the data for cases where the object was instantiated in a non-standard way. (Like a Marshal.load)
Get the object id as it’s raw BSON data.
171 172 173 174 175 |
# File 'lib/bson/object_id.rb', line 171 def to_bson(encoded = ''.force_encoding(BINARY)) repair if defined?(@data) @raw_data ||= @@generator.next_object_id encoded << @raw_data end |