Compare this object with another object, used in sorting.
@example Compare the two objects.
object <=> other
@param [ Object ] other The object to compare to.
@return [ Integer ] The result of the comparison.
@since 1.0.0
# File lib/moped/bson/object_id.rb, line 64 def <=>(other) data <=> other.data end
Check equality on the object.
@example Check equality.
object == other
@param [ Object ] other The object to check against.
@return [ true, false ] If the objects are equal.
@since 1.0.0
# File lib/moped/bson/object_id.rb, line 49 def ==(other) BSON::ObjectId === other && data == other.data end
Check equality on the object.
@example Check equality.
object === other
@param [ Object ] other The object to check against.
@return [ true, false ] If the objects are equal.
@since 1.0.0
# File lib/moped/bson/object_id.rb, line 34 def ===(other) return to_str === other.to_str if other.respond_to?(:to_str) super end
Serialize the object id to its raw bytes.
@example Serialize the object id.
object_id.__bson_dump__("", "_id")
@param [ String ] io The raw bytes to write to. @param [ String ] key The field name.
@since 1.0.0
# File lib/moped/bson/object_id.rb, line 18 def __bson_dump__(io, key) io << Types::OBJECT_ID io << key.to_bson_cstring io << data end
Get the raw data (bytes) for the object id.
@example Get the raw data.
object_id.data
@return [ String ] The raw bytes.
@since 1.0.0
# File lib/moped/bson/object_id.rb, line 76 def data # If @data is defined, then we know we've been loaded in some # non-standard way, so we attempt to repair the data. repair! @data if defined? @data @raw_data ||= @@generator.next end
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.
@example Get the generation time.
object_id.generation_time
@return [ Time ] The time the id was generated.
@since 1.0.0
# File lib/moped/bson/object_id.rb, line 93 def generation_time Time.at(data.unpack("N")[0]).utc end
Gets the hash code for the object.
@example Get the hash code.
object.hash
@return [ Fixnum ] The hash code.
@since 1.0.0
# File lib/moped/bson/object_id.rb, line 105 def hash data.hash end
Gets the string inspection for the object.
@example Get the string inspection.
object.inspect
@return [ String ] The inspection.
@since 1.0.0
# File lib/moped/bson/object_id.rb, line 117 def inspect to_s.inspect end
Dump the object for use in a marshal dump.
@example Dump the object.
object.marshal_dump
@return [ String ] The dumped object.
@since 1.0.0
# File lib/moped/bson/object_id.rb, line 129 def marshal_dump data end
Load the object from the marshal dump.
@example Load the object.
object.marshal_load("")
@param [ String ] data The raw data.
@since 1.0.0
# File lib/moped/bson/object_id.rb, line 141 def marshal_load(data) self.data = data end
Convert the object to a JSON string.
@example Convert to a JSON string.
obejct.to_json
@return [ String ] The object as JSON.
@since 1.0.0
# File lib/moped/bson/object_id.rb, line 153 def to_json(*args) "{\"$oid\": \"#{to_s}\"}" end
Get the string representation of the object.
@example Get the string representation.
object.to_s
@return [ String ] The string representation.
@since 1.0.0
# File lib/moped/bson/object_id.rb, line 165 def to_s data.unpack("H*")[0].force_encoding(Moped::BSON::UTF8_ENCODING) end