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 (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.
33 34 35 36 |
# File 'lib/bson/db_pointer.rb', line 33 def initialize(ref, id) @ref = ref @id = id end |
Instance Attribute Details
#id ⇒ BSON::ObjectId (readonly)
Return the DbPointer’s id.
46 47 48 |
# File 'lib/bson/db_pointer.rb', line 46 def id @id end |
#ref ⇒ String (readonly)
Return the 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.
97 98 99 100 101 102 103 104 105 |
# File 'lib/bson/db_pointer.rb', line 97 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.
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).
72 73 74 |
# File 'lib/bson/db_pointer.rb', line 72 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.
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.
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 |