Skip to content

Commit 9b83f62

Browse files
neilshwekyp
andauthored
RUBY-2090 RUBY-2638 Move driver DBRef class to bson-ruby (#2391)
* RUBY-2090 change dbref to point to bson ruby * RUBY-2090 update tests for new constructor * RUBY-2090 remove dbref tests * RUBY-2090 up the minimum bson-ruby * RUBY-2090 update release notes for dbref * need to use 4-stable, not master of bson-ruby Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 62e9725 commit 9b83f62

File tree

6 files changed

+10
-260
lines changed

6 files changed

+10
-260
lines changed

docs/release-notes.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ This release includes the following new features:
2525
- Added support for Azure Key Vault, Google Cloud Key Management, and any
2626
KMIP compliant Key Management System to be used as master key storage for
2727
client side encryption.
28+
- The DBRef class has been moved to bson-ruby and requires bson-ruby 4.13.0
29+
or greater. For backwards compatibility, BSON::DBRef is aliased as Mongo::DBRef.
30+
The BSON::DBRef class derives from BSON::Document, unlike Mongo::DBRef which
31+
derived from Object. BSON::DBRef retains all fields, unlike Mongo::DBRef which
32+
only allowed $ref, $id, and $db. BSON::DBRef also reorders the fields if
33+
necessary to place $ref, $id, and $db first, in that order.
2834

2935
2.17
3036
====

gemfiles/bson_master.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
source "https://rubygems.org"
22
gemspec path: '..'
33

4-
gem 'bson', git: 'https://github.com/mongodb/bson-ruby'
4+
gem 'bson', git: 'https://github.com/mongodb/bson-ruby', branch: '4-stable'
55

66
require_relative './standard'
77

gemfiles/bson_min.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
source "https://rubygems.org"
22
gemspec path: '..'
33

4-
gem 'bson', '4.8.2'
4+
gem 'bson', '4.13.0'
55

66
require_relative './standard'
77

lib/mongo/dbref.rb

Lines changed: 1 addition & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -16,109 +16,5 @@
1616
# limitations under the License.
1717

1818
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
12420
end

mongo.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ Gem::Specification.new do |s|
3838

3939
s.required_ruby_version = ">= 2.5"
4040

41-
s.add_dependency 'bson', '>=4.8.2', '<5.0.0'
41+
s.add_dependency 'bson', '>=4.13.0', '<5.0.0'
4242
end

spec/mongo/dbref_spec.rb

Lines changed: 0 additions & 152 deletions
This file was deleted.

0 commit comments

Comments
 (0)