class Moped::BSON::ObjectId
Public Instance Methods
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
Private Instance Methods
# File lib/moped/bson/object_id.rb, line 195 def __bson_load__(io) from_data(io.read(12)) end
Private interface for setting the internal data for an object id.
# File lib/moped/bson/object_id.rb, line 173 def data=(data) @raw_data = data end
Create a new object id from some raw data.
@example Create an object id from raw data.
Moped::BSON::ObjectId.from_data(data)
@param [ String ] data The raw bytes.
@return [ ObjectId ] The new object id.
@since 1.0.0
# File lib/moped/bson/object_id.rb, line 260 def from_data(data) id = allocate id.send(:data=, data) id end
Create a new object id from a string.
@example Create an object id from the string.
Moped::BSON::ObjectId.from_string(id)
@param [ String ] string The string to create from.
@return [ ObjectId ] The new object id.
@since 1.0.0
# File lib/moped/bson/object_id.rb, line 209 def from_string(string) raise Errors::InvalidObjectId.new(string) unless legal?(string) from_data [string].pack("H*") end
Create a new object id from a time.
@example Create an object id from a time.
Moped::BSON::ObjectId.from_id(time)
@example Create an object id from a time, ensuring uniqueness.
Moped::BSON::ObjectId.from_id(time, unique: true)
@param [ Time ] time The time to generate from. @param [ Hash ] options The options.
@option options [ true, false ] :unique Whether the id should be
unique.
@return [ ObjectId ] The new object id.
@since 1.0.0
# File lib/moped/bson/object_id.rb, line 231 def from_time(time, options = nil) unique = (options || {})[:unique] from_data(unique ? @@generator.next(time.to_i) : [ time.to_i ].pack("Nx8")) end
Determine if the string is a legal object id.
@example Is the string a legal object id?
Moped::BSON::ObjectId.legal?(string)
@param [ String ] The string to test.
@return [ true, false ] If the string is legal.
@since 1.0.0
# File lib/moped/bson/object_id.rb, line 246 def legal?(string) string.to_s =~ /\A\h{24}\z/ ? true : false end
Attempts to repair ObjectId data marshalled in previous formats.
The first check covers an ObjectId generated by the mongo-ruby-driver.
The second check covers an ObjectId generated by moped before a custom marshal strategy was added.
# File lib/moped/bson/object_id.rb, line 183 def repair!(data) if data.is_a?(Array) && data.size == 12 self.data = data.pack("C*") elsif data.is_a?(String) && data.size == 12 self.data = data else raise TypeError, "Could not convert #{data.inspect} into an ObjectId" end end