class Moped::BSON::ObjectId

Private Class Methods

__bson_load__(io) click to toggle source
# File lib/moped/bson/object_id.rb, line 195
def __bson_load__(io)
  from_data(io.read(12))
end
from_data(data) click to toggle source

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
from_string(string) click to toggle source

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
from_time(time, options = nil) click to toggle source

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

Public Instance Methods

<=>(other) click to toggle source

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
==(other) click to toggle source

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
Also aliased as: eql?
===(other) click to toggle source

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

Calls superclass method
# 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
__bson_dump__(io, key) click to toggle source

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
data() click to toggle source

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
eql?(other)
Alias for: ==
generation_time() click to toggle source

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
hash() click to toggle source

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
inspect() click to toggle source

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
marshal_dump() click to toggle source

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
marshal_load(data) click to toggle source

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
to_json(*args) click to toggle source

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
to_s() click to toggle source

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
Also aliased as: to_str
to_str()
Alias for: to_s

Private Instance Methods

data=(data) click to toggle source

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
repair!(data) click to toggle source

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