class BSON::ObjectId

@since 2.0.0
@see bsonspec.org/#/specification<br><br>Represents object_id data.

def self._generator

Other tags:
    Api: - private
def self._generator
  @@generator
end

def <=>(other)

Other tags:
    Since: - 2.0.0

Returns:
  • (Integer) - The result of the comparison.

Parameters:
  • other (Object) -- The object to compare to.

Other tags:
    Example: Compare the object id with the other object. -
def <=>(other)
  generate_data <=> other.to_bson.to_s
end

def ==(other)

Other tags:
    Since: - 2.0.0

Returns:
  • (true, false) - If the objects are equal.

Parameters:
  • other (Object) -- The object to check against.

Other tags:
    Example: Check if the object id is equal to the other. -
def ==(other)
  return false unless other.is_a?(ObjectId)
  generate_data == other.send(:generate_data)
end

def ===(other)

Other tags:
    Since: - 2.0.0

Returns:
  • (true, false) - If the objects are equal in a case.

Parameters:
  • other (Object) -- The object to check against.

Other tags:
    Example: Check case equality. -
def ===(other)
  return to_str == other.to_str if other.respond_to?(:to_str)
  super
end

def _counter_part

Other tags:
    Api: - private

Returns:
  • (String) - The counter portion of the id.
def _counter_part
  to_s[18, 6]
end

def _process_part

Other tags:
    Api: - private

Returns:
  • (String) - The process portion of the id.
def _process_part
  to_s[8, 10]
end

def as_extended_json(**_)

Returns:
  • (Hash) - The extended json representation.
def as_extended_json(**_)
  { '$oid' => to_s }
end

def as_json(*_)

Returns:
  • (String) - The object id as a string.

Other tags:
    Example: Get the object id as a JSON-serializable object. -
def as_json(*_)
  to_s
end

def from_bson(buffer, **_)

Other tags:
    Since: - 2.0.0

Returns:
  • (BSON::ObjectId) - The object id.

Parameters:
  • _ (Hash) -- An optional hash of keyword arguments (unused).
  • buffer (ByteBuffer) -- The byte buffer.

Other tags:
    Example: Get the object id from BSON. -
def from_bson(buffer, **_)
  from_data(buffer.get_bytes(12))
end

def from_data(data)

Other tags:
    Since: - 2.0.0

Returns:
  • (ObjectId) - The new object id.

Parameters:
  • data (String) -- The raw bytes.

Other tags:
    Example: Create an object id from raw bytes. -
def from_data(data)
  object_id = allocate
  object_id.instance_variable_set(:@raw_data, data)
  object_id
end

def from_string(string)

Other tags:
    Since: - 2.0.0

Returns:
  • (BSON::ObjectId) - The new object id.

Raises:
  • (BSON::Error::InvalidObjectId) - If the provided string is invalid.

Parameters:
  • string (String) -- The string to create the id from.

Other tags:
    Example: Create an object id from the string. -
def from_string(string)
  raise Error::InvalidObjectId, "'#{string}' is an invalid ObjectId." unless legal?(string)
  from_data([ string ].pack('H*'))
end

def from_time(time, options = {})

Other tags:
    Since: - 2.0.0

Returns:
  • (ObjectId) - The new object id.

Options Hash: (**options)
  • :unique (true, false) -- Whether the id should be

Parameters:
  • options (Hash) -- The options.
  • time (Time) -- The time to generate from.

Other tags:
    Example: Create an object id from a time, ensuring uniqueness. -
    Example: Create an object id from a time. -
def from_time(time, options = {})
  from_data(options[:unique] ? @@generator.next_object_id(time.to_i) : [ time.to_i ].pack('Nx8'))
end

def generate_data

def generate_data
  repair if defined?(@data)
  # rubocop:disable Naming/MemoizedInstanceVariableName
  @raw_data ||= @@generator.next_object_id
  # rubocop:enable Naming/MemoizedInstanceVariableName
end

def generation_time

Other tags:
    Since: - 2.0.0

Returns:
  • (Time) - The time the id was generated.

Other tags:
    Example: Get the generation time. -
def generation_time
  ::Time.at(generate_data.unpack1('N')).utc
end

def hash

Other tags:
    Since: - 2.0.0

Returns:
  • (Integer) - The hash value.

Other tags:
    Example: Get the hash value. -
def hash
  generate_data.hash
end

def initialize_copy(other)

def initialize_copy(other)
  generate_data
  other.instance_variable_set(:@raw_data, @raw_data)
end

def inspect

Other tags:
    Since: - 2.0.0

Returns:
  • (String) - The object id in form BSON::ObjectId('id')

Other tags:
    Example: Inspect the object id. -
def inspect
  "BSON::ObjectId('#{self}')"
end

def legal?(string)

Other tags:
    Since: - 2.0.0

Returns:
  • (true, false) - If the string is legal.

Parameters:
  • string (String) -- The string to check.

Other tags:
    Example: Is the string a legal object id? -
def legal?(string)
  (string.to_s =~ /\A[0-9a-f]{24}\z/i) ? true : false
end

def marshal_dump

Other tags:
    Since: - 2.0.0

Returns:
  • (String) - The raw bson bytes.

Other tags:
    Example: Dump the raw bson. -
def marshal_dump
  generate_data
end

def marshal_load(data)

Other tags:
    Since: - 2.0.0

Returns:
  • (String) - The raw bson bytes.

Parameters:
  • data (String) -- The raw bson bytes.

Other tags:
    Example: Unmarshal the data. -
def marshal_load(data)
  @raw_data = data
end

def repair

def repair
  @raw_data = @data.to_bson_object_id
  remove_instance_variable(:@data)
end

def repair(object)

Other tags:
    Since: - 2.0.0

Returns:
  • (String) - The result of the block.

Raises:
  • (BSON::Error::InvalidObjectId) - If the array is not 12 elements.

Parameters:
  • object (String, Array) -- The object to repair.

Other tags:
    Example: Execute in a repairing block. -
def repair(object)
  raise Error::InvalidObjectId, "#{object.inspect} is not a valid object id." if object.size != 12
  block_given? ? yield(object) : object
end

def timestamp

Returns:
  • (Integer) - the number of seconds since the Epoch.

Other tags:
    Note: - This value is guaranteed to be no more than 4 bytes in length. A
def timestamp
  ::Time.now.to_i % MAX_INTEGER
end

def to_bson(buffer = ByteBuffer.new)

Other tags:
    Since: - 2.0.0

Other tags:
    See: http://bsonspec.org/#/specification -

Returns:
  • (BSON::ByteBuffer) - The buffer with the encoded object.

Other tags:
    Note: - Since Moped's BSON and MongoDB BSON before 2.0.0 have different

Other tags:
    Example: Get the raw bson bytes. -
def to_bson(buffer = ByteBuffer.new)
  buffer.put_bytes(generate_data)
end

def to_s

Other tags:
    Since: - 2.0.0

Returns:
  • (String) - The object id as a string.

Other tags:
    Example: Get the object id as a string. -
def to_s
  generate_data.to_hex_string.force_encoding(UTF8)
end