Class: Moped::BSON::ObjectId

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/moped/bson/object_id.rb

Defined Under Namespace

Classes: Generator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.__bson_load__(io) ⇒ Object



195
196
197
# File 'lib/moped/bson/object_id.rb', line 195

def __bson_load__(io)
  from_data(io.read(12))
end

.from_data(data) ⇒ ObjectId

Create a new object id from some raw data.

Examples:

Create an object id from raw data.

Moped::BSON::ObjectId.from_data(data)

Parameters:

  • data (String)

    The raw bytes.

Returns:

Since:

  • 1.0.0



260
261
262
263
264
# File 'lib/moped/bson/object_id.rb', line 260

def from_data(data)
  id = allocate
  id.send(:data=, data)
  id
end

.from_string(string) ⇒ ObjectId

Create a new object id from a string.

Examples:

Create an object id from the string.

Moped::BSON::ObjectId.from_string(id)

Parameters:

  • string (String)

    The string to create from.

Returns:

Raises:

Since:

  • 1.0.0



209
210
211
212
# 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) ⇒ ObjectId

Create a new object id from a time.

Examples:

Create an object id from a time.

Moped::BSON::ObjectId.from_id(time)

Create an object id from a time, ensuring uniqueness.

Moped::BSON::ObjectId.from_id(time, unique: true)

Parameters:

  • time (Time)

    The time to generate from.

  • options (Hash) (defaults to: nil)

    The options.

Options Hash (options):

  • :unique (true, false)

    Whether the id should be unique.

Returns:

Since:

  • 1.0.0



231
232
233
234
# 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

.legal?(string) ⇒ true, false

Determine if the string is a legal object id.

Examples:

Is the string a legal object id?

Moped::BSON::ObjectId.legal?(string)

Parameters:

  • The (String)

    string to test.

Returns:

  • (true, false)

    If the string is legal.

Since:

  • 1.0.0



246
247
248
# File 'lib/moped/bson/object_id.rb', line 246

def legal?(string)
  /\A\h{24}\Z/ === string.to_s
end

Instance Method Details

#<=>(other) ⇒ Integer

Compare this object with another object, used in sorting.

Examples:

Compare the two objects.

object <=> other

Parameters:

  • other (Object)

    The object to compare to.

Returns:

  • (Integer)

    The result of the comparison.

Since:

  • 1.0.0



64
65
66
# File 'lib/moped/bson/object_id.rb', line 64

def <=>(other)
  data <=> other.data
end

#==(other) ⇒ true, false Also known as: eql?

Check equality on the object.

Examples:

Check equality.

object == other

Parameters:

  • other (Object)

    The object to check against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 1.0.0



49
50
51
# File 'lib/moped/bson/object_id.rb', line 49

def ==(other)
  BSON::ObjectId === other && data == other.data
end

#===(other) ⇒ true, false

Check equality on the object.

Examples:

Check equality.

object === other

Parameters:

  • other (Object)

    The object to check against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 1.0.0



34
35
36
37
# 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) ⇒ Object

Serialize the object id to its raw bytes.

Examples:

Serialize the object id.

object_id.__bson_dump__("", "_id")

Parameters:

  • io (String)

    The raw bytes to write to.

  • key (String)

    The field name.

Since:

  • 1.0.0



18
19
20
21
22
# 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

#dataString

Get the raw data (bytes) for the object id.

Examples:

Get the raw data.

object_id.data

Returns:

Since:

  • 1.0.0



76
77
78
79
80
81
# 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

#generation_timeTime

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.

Examples:

Get the generation time.

object_id.generation_time

Returns:

  • (Time)

    The time the id was generated.

Since:

  • 1.0.0



93
94
95
# File 'lib/moped/bson/object_id.rb', line 93

def generation_time
  Time.at(data.unpack("N")[0]).utc
end

#hashFixnum

Gets the hash code for the object.

Examples:

Get the hash code.

object.hash

Returns:

  • (Fixnum)

    The hash code.

Since:

  • 1.0.0



105
106
107
# File 'lib/moped/bson/object_id.rb', line 105

def hash
  data.hash
end

#inspectString

Gets the string inspection for the object.

Examples:

Get the string inspection.

object.inspect

Returns:

  • (String)

    The inspection.

Since:

  • 1.0.0



117
118
119
# File 'lib/moped/bson/object_id.rb', line 117

def inspect
  to_s.inspect
end

#marshal_dumpString

Dump the object for use in a marshal dump.

Examples:

Dump the object.

object.marshal_dump

Returns:

  • (String)

    The dumped object.

Since:

  • 1.0.0



129
130
131
# File 'lib/moped/bson/object_id.rb', line 129

def marshal_dump
  data
end

#marshal_load(data) ⇒ Object

Load the object from the marshal dump.

Examples:

Load the object.

object.marshal_load("")

Parameters:

  • data (String)

    The raw data.

Since:

  • 1.0.0



141
142
143
# File 'lib/moped/bson/object_id.rb', line 141

def marshal_load(data)
  self.data = data
end

#to_json(*args) ⇒ String

Convert the object to a JSON string.

Examples:

Convert to a JSON string.

obejct.to_json

Returns:

  • (String)

    The object as JSON.

Since:

  • 1.0.0



153
154
155
# File 'lib/moped/bson/object_id.rb', line 153

def to_json(*args)
  "{\"$oid\": \"#{to_s}\"}"
end

#to_sString Also known as: to_str

Get the string representation of the object.

Examples:

Get the string representation.

object.to_s

Returns:

  • (String)

    The string representation.

Since:

  • 1.0.0



165
166
167
# File 'lib/moped/bson/object_id.rb', line 165

def to_s
  data.unpack("H*")[0].force_encoding(Moped::BSON::UTF8_ENCODING)
end