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(buffer) ⇒ 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
(also: #to_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(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) ⇒ BSON::ByteBuffer
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(buffer) ⇒ BSON::ObjectId
Deserialize the object id from raw BSON bytes.
223 224 225 |
# File 'lib/bson/object_id.rb', line 223 def from_bson(buffer) from_data(buffer.get_bytes(12)) end |
.from_data(data) ⇒ ObjectId
Create a new object id from raw bytes.
237 238 239 240 241 |
# File 'lib/bson/object_id.rb', line 237 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.
255 256 257 258 259 260 |
# File 'lib/bson/object_id.rb', line 255 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.
279 280 281 |
# File 'lib/bson/object_id.rb', line 279 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.
293 294 295 |
# File 'lib/bson/object_id.rb', line 293 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.
310 311 312 313 314 315 316 |
# File 'lib/bson/object_id.rb', line 310 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) generate_data <=> other.to_bson.to_s 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) generate_data == other.send(:generate_data) 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 Also known as: to_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(generate_data.unpack("N")[0]).utc end |
#hash ⇒ Integer
Get the hash value for the object id.
115 116 117 |
# File 'lib/bson/object_id.rb', line 115 def hash generate_data.hash end |
#inspect ⇒ String
Get a nice string for use with object inspection.
127 128 129 |
# File 'lib/bson/object_id.rb', line 127 def inspect "BSON::ObjectId('#{to_s}')" end |
#marshal_dump ⇒ String
Dump the raw bson when calling Marshal.dump.
139 140 141 |
# File 'lib/bson/object_id.rb', line 139 def marshal_dump generate_data end |
#marshal_load(data) ⇒ String
Unmarshal the data into an object id.
153 154 155 |
# File 'lib/bson/object_id.rb', line 153 def marshal_load(data) @raw_data = data end |
#to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) ⇒ BSON::ByteBuffer
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.
172 173 174 |
# File 'lib/bson/object_id.rb', line 172 def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) buffer.put_bytes(generate_data) end |