Class: BSON::DbPointer

Inherits:
Object
  • Object
show all
Includes:
JSON
Defined in:
lib/bson/db_pointer.rb

Overview

Injects behaviour for encoding and decoding DBPointer values to and from raw bytes as specified by the BSON spec.

Constant Summary collapse

BSON_TYPE =

A DBPointer is type 0x0C in the BSON spec.

::String.new(0x0C.chr, encoding: BINARY).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JSON

#to_json

Constructor Details

#initialize(ref, id) ⇒ DbPointer

Create a new DBPointer object.

Parameters:



33
34
35
36
# File 'lib/bson/db_pointer.rb', line 33

def initialize(ref, id)
  @ref = ref
  @id = id
end

Instance Attribute Details

#idBSON::ObjectId (readonly)

Return the DbPointer’s id.

Returns:



46
47
48
# File 'lib/bson/db_pointer.rb', line 46

def id
  @id
end

#refString (readonly)

Return the collection name.

Returns:

  • (String)

    The database collection name.



41
42
43
# File 'lib/bson/db_pointer.rb', line 41

def ref
  @ref
end

Class Method Details

.from_bson(buffer, **options) ⇒ BSON::DbPointer

Deserialize a DBPointer from BSON.

Parameters:

Options Hash (**options):

  • :mode (nil | :bson)

    Decoding mode to use.

Returns:

See Also:



97
98
99
100
101
102
103
104
105
# File 'lib/bson/db_pointer.rb', line 97

def self.from_bson(buffer, **options)
  ref = buffer.get_string
  id = if options.empty?
    ObjectId.from_bson(buffer)
  else
    ObjectId.from_bson(buffer, **options)
  end
  new(ref, id)
end

Instance Method Details

#==(other) ⇒ true | false

Determine if this DBPointer object is equal to another object.

Parameters:

  • other (Object)

    The object to compare against.

Returns:

  • (true | false)

    If the objects are equal



53
54
55
56
# File 'lib/bson/db_pointer.rb', line 53

def ==(other)
  return false unless other.is_a?(DbPointer)
  ref == other.ref && id == other.id
end

#as_extended_json(**_options) ⇒ Hash

Converts this object to a representation directly serializable to Extended JSON (github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).

Returns:

  • (Hash)

    The extended json representation.



72
73
74
# File 'lib/bson/db_pointer.rb', line 72

def as_extended_json(**_options)
  { '$dbPointer' => { "$ref" => ref, '$id' => id.as_extended_json } }
end

#as_json(*_args) ⇒ Hash

Return a representation of the object for use in application-level JSON serialization. Since BSON::DbPointer is used exclusively in BSON-related contexts, this method returns the canonical Extended JSON representation.

Returns:

  • (Hash)

    The extended json representation.



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

def as_json(*_args)
  as_extended_json
end

#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer

Encode the DBPointer.

Returns:

See Also:



81
82
83
84
85
# File 'lib/bson/db_pointer.rb', line 81

def to_bson(buffer = ByteBuffer.new)
  buffer.put_string(ref)
  id.to_bson(buffer)
  buffer
end