Class: BSON::DbPointer
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
-
#id ⇒ BSON::ObjectId
readonly
Return the DbPointer's id.
-
#ref ⇒ String
readonly
Return the collection name.
Class Method Summary collapse
-
.from_bson(buffer, **options) ⇒ BSON::DbPointer
Deserialize a DBPointer from BSON.
Instance Method Summary collapse
-
#==(other) ⇒ true | false
Determine if this DBPointer object is equal to another object.
-
#as_extended_json(**_options) ⇒ Hash
Converts this object to a representation directly serializable to Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).
-
#as_json(*_args) ⇒ Hash
Return a representation of the object for use in application-level JSON serialization.
-
#initialize(ref, id) ⇒ DbPointer
constructor
Create a new DBPointer object.
-
#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer
Encode the DBPointer.
Methods included from JSON
Constructor Details
#initialize(ref, id) ⇒ DbPointer
Create a new DBPointer object.
The types are enforced because the DBPointer wire format is a string followed by exactly 12 raw bytes. #to_bson writes the id duck-typed, so an id of any other type emits the wrong number of bytes and reframes the enclosing document for every conforming reader.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/bson/db_pointer.rb', line 41 def initialize(ref, id) unless ref.is_a?(::String) raise ArgumentError, "DBPointer ref must be a String: #{ref.inspect}" end unless id.is_a?(ObjectId) raise ArgumentError, "DBPointer id must be a BSON::ObjectId: #{id.inspect}" end @ref = ref @id = id end |
Instance Attribute Details
#id ⇒ BSON::ObjectId (readonly)
Return the DbPointer's id.
62 63 64 |
# File 'lib/bson/db_pointer.rb', line 62 def id @id end |
#ref ⇒ String (readonly)
Return the collection name.
57 58 59 |
# File 'lib/bson/db_pointer.rb', line 57 def ref @ref end |
Class Method Details
.from_bson(buffer, **options) ⇒ BSON::DbPointer
Deserialize a DBPointer from BSON.
113 114 115 116 117 118 119 120 121 |
# File 'lib/bson/db_pointer.rb', line 113 def self.from_bson(buffer, **) ref = buffer.get_string id = if .empty? ObjectId.from_bson(buffer) else ObjectId.from_bson(buffer, **) end new(ref, id) end |
Instance Method Details
#==(other) ⇒ true | false
Determine if this DBPointer object is equal to another object.
69 70 71 72 |
# File 'lib/bson/db_pointer.rb', line 69 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 (https://github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).
88 89 90 |
# File 'lib/bson/db_pointer.rb', line 88 def as_extended_json(**) { '$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.
80 81 82 |
# File 'lib/bson/db_pointer.rb', line 80 def as_json(*_args) as_extended_json end |
#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer
Encode the DBPointer.
97 98 99 100 101 |
# File 'lib/bson/db_pointer.rb', line 97 def to_bson(buffer = ByteBuffer.new) buffer.put_string(ref) id.to_bson(buffer) buffer end |