Parent

Included Modules

BSON::ObjectId

Represents object_id data.

@see bsonspec.org/#/specification

@since 2.0.0

Constants

BSON_TYPE

A object_id is type 0x07 in the BSON spec.

@since 2.0.0

Public Instance Methods

<=>(other) click to toggle source

Compare this object id with another object for use in sorting.

@example Compare the object id with the other object.

object <=> other

@param [ Object ] other The object to compare to.

@return [ Integer ] The result of the comparison.

@since 2.0.0

# File lib/bson/object_id.rb, line 88
def <=>(other)
  to_bson <=> other.to_bson
end
==(other) click to toggle source

Check equality of the object id with another object.

@example Check if the object id is equal to the other.

object_id == other

@param [ Object ] other The object to check against.

@return [ true, false ] If the objects are equal.

@since 2.0.0

# File lib/bson/object_id.rb, line 45
def ==(other)
  return false unless other.is_a?(ObjectId)
  to_bson == other.to_bson
end
Also aliased as: eql?
===(other) click to toggle source

Check case equality on the object id.

@example Check case equality.

object_id === other

@param [ Object ] other The object to check against.

@return [ true, false ] If the objects are equal in a case.

@since 2.0.0

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

Return the object id as a JSON hash representation.

@example Get the object id as JSON.

object_id.as_json

@return [ Hash ] The object id as a JSON hash.

@since 2.0.0

# File lib/bson/object_id.rb, line 74
def as_json(*args)
  { "$oid" => to_s }
end
eql?(other) click to toggle source
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 2.0.0

# File lib/bson/object_id.rb, line 102
def generation_time
  ::Time.at(to_bson.unpack("N")[0]).utc
end
hash() click to toggle source

Get the hash value for the object id.

@example Get the hash value.

object_id.hash

@return [ Integer ] The hash value.

@since 2.0.0

# File lib/bson/object_id.rb, line 114
def hash
  to_bson.hash
end
inspect() click to toggle source

Get a nice string for use with object inspection.

@example Inspect the object id.

obhect_id.inspect

@return [ String ] The object id in form BSON::ObjectId(‘id’)

@since 2.0.0

# File lib/bson/object_id.rb, line 126
def inspect
  "BSON::ObjectId('#{to_s}')"
end
marshal_dump() click to toggle source

Dump the raw bson when calling Marshal.dump.

@example Dump the raw bson.

Marshal.dump(object_id)

@return [ String ] The raw bson bytes.

@since 2.0.0

# File lib/bson/object_id.rb, line 138
def marshal_dump
  to_bson
end
marshal_load(data) click to toggle source

Unmarshal the data into an object id.

@example Unmarshal the data.

Marshal.load(data)

@param [ String ] data The raw bson bytes.

@return [ String ] The raw bson bytes.

@since 2.0.0

# File lib/bson/object_id.rb, line 152
def marshal_load(data)
  @raw_data = data
end
to_bson(encoded = ''.force_encoding(BINARY)) click to toggle source

Get the object id as it’s raw BSON data.

@example Get the raw bson bytes.

object_id.to_bson

@note 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)

@return [ String ] The raw bytes.

@see bsonspec.org/#/specification

@since 2.0.0

# File lib/bson/object_id.rb, line 171
def to_bson(encoded = ''.force_encoding(BINARY))
  repair if defined?(@data)
  @raw_data ||= @@generator.next
  encoded << @raw_data
end
to_s() click to toggle source

Get the string representation of the object id.

@example Get the object id as a string.

object_id.to_s

@return [ String ] The object id as a string.

@since 2.0.0

# File lib/bson/object_id.rb, line 185
def to_s
  to_bson.to_hex_string.force_encoding(UTF8)
end
Also aliased as: to_str
to_str() click to toggle source
Alias for: to_s

[Validate]

Generated with the Darkfish Rdoc Generator 2.