|
16 | 16 | # limitations under the License.
|
17 | 17 |
|
18 | 18 | module Mongo
|
19 |
| - |
20 |
| - # Represents a DBRef document in the database. |
21 |
| - # |
22 |
| - # @since 2.1.0 |
23 |
| - class DBRef |
24 |
| - include BSON::JSON |
25 |
| - |
26 |
| - # The constant for the collection reference field. |
27 |
| - # |
28 |
| - # @since 2.1.0 |
29 |
| - COLLECTION = '$ref'.freeze |
30 |
| - |
31 |
| - # The constant for the id field. |
32 |
| - # |
33 |
| - # @since 2.1.0 |
34 |
| - ID = '$id'.freeze |
35 |
| - |
36 |
| - # The constant for the database field. |
37 |
| - # |
38 |
| - # @since 2.1.0 |
39 |
| - DATABASE = '$db'.freeze |
40 |
| - |
41 |
| - # @return [ String ] collection The collection name. |
42 |
| - attr_reader :collection |
43 |
| - |
44 |
| - # @return [ BSON::ObjectId ] id The referenced document id. |
45 |
| - attr_reader :id |
46 |
| - |
47 |
| - # @return [ String ] database The database name. |
48 |
| - attr_reader :database |
49 |
| - |
50 |
| - # Get the DBRef as a JSON document |
51 |
| - # |
52 |
| - # @example Get the DBRef as a JSON hash. |
53 |
| - # dbref.as_json |
54 |
| - # |
55 |
| - # @return [ Hash ] The max key as a JSON hash. |
56 |
| - # |
57 |
| - # @since 2.1.0 |
58 |
| - def as_json(*args) |
59 |
| - document = { COLLECTION => collection, ID => id } |
60 |
| - document.merge!(DATABASE => database) if database |
61 |
| - document |
62 |
| - end |
63 |
| - |
64 |
| - # Instantiate a new DBRef. |
65 |
| - # |
66 |
| - # @example Create the DBRef. |
67 |
| - # Mongo::DBRef.new('users', id, 'database') |
68 |
| - # |
69 |
| - # @param [ String ] collection The collection name. |
70 |
| - # @param [ BSON::ObjectId ] id The object id. |
71 |
| - # @param [ String ] database The database name. |
72 |
| - # |
73 |
| - # @since 2.1.0 |
74 |
| - def initialize(collection, id, database = nil) |
75 |
| - @collection = collection |
76 |
| - @id = id |
77 |
| - @database = database |
78 |
| - end |
79 |
| - |
80 |
| - # Converts the DBRef to raw BSON. |
81 |
| - # |
82 |
| - # @example Convert the DBRef to raw BSON. |
83 |
| - # dbref.to_bson |
84 |
| - # |
85 |
| - # @param [ BSON::ByteBuffer ] buffer The encoded BSON buffer to append to. |
86 |
| - # @param [ true, false ] validating_keys Whether keys should be validated when serializing. |
87 |
| - # |
88 |
| - # @return [ String ] The raw BSON. |
89 |
| - # |
90 |
| - # @since 2.1.0 |
91 |
| - def to_bson(buffer = BSON::ByteBuffer.new, validating_keys = BSON::Config.validating_keys?) |
92 |
| - as_json.to_bson(buffer) |
93 |
| - end |
94 |
| - |
95 |
| - module ClassMethods |
96 |
| - |
97 |
| - # Deserialize the hash from BSON, converting to a DBRef if appropriate. |
98 |
| - # |
99 |
| - # @param [ String ] buffer The bson representing a hash. |
100 |
| - # |
101 |
| - # @return [ Hash, DBRef ] The decoded hash or DBRef. |
102 |
| - # |
103 |
| - # @see http://bsonspec.org/#/specification |
104 |
| - # |
105 |
| - # @since 2.0.0 |
106 |
| - def from_bson(buffer, **options) |
107 |
| - # bson-ruby 4.8.0 changes #from_bson API to take **options. |
108 |
| - # However older bsons fail if invoked with a plain super here, |
109 |
| - # even if options are empty. |
110 |
| - decoded = if options.empty? |
111 |
| - super(buffer) |
112 |
| - else |
113 |
| - super |
114 |
| - end |
115 |
| - if ref = decoded[COLLECTION] |
116 |
| - decoded = DBRef.new(ref, decoded[ID], decoded[DATABASE]) |
117 |
| - end |
118 |
| - decoded |
119 |
| - end |
120 |
| - end |
121 |
| - end |
122 |
| - |
123 |
| - ::Hash.send(:extend, DBRef::ClassMethods) |
| 19 | + DBRef = BSON::DBRef |
124 | 20 | end
|
0 commit comments