Skip to content

RUBY-2090 RUBY-2638 Move driver DBRef class to bson-ruby #2391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/release-notes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ This release includes the following new features:
- Added support for Azure Key Vault, Google Cloud Key Management, and any
KMIP compliant Key Management System to be used as master key storage for
client side encryption.
- The DBRef class has been moved to bson-ruby and requires bson-ruby 4.13.0
or greater. For backwards compatibility, BSON::DBRef is aliased as Mongo::DBRef.
The BSON::DBRef class derives from BSON::Document, unlike Mongo::DBRef which
derived from Object. BSON::DBRef retains all fields, unlike Mongo::DBRef which
only allowed $ref, $id, and $db. BSON::DBRef also reorders the fields if
necessary to place $ref, $id, and $db first, in that order.

2.17
====
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/bson_master.gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source "https://rubygems.org"
gemspec path: '..'

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

require_relative './standard'

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/bson_min.gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source "https://rubygems.org"
gemspec path: '..'

gem 'bson', '4.8.2'
gem 'bson', '4.13.0'

require_relative './standard'

Expand Down
106 changes: 1 addition & 105 deletions lib/mongo/dbref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,109 +16,5 @@
# limitations under the License.

module Mongo

# Represents a DBRef document in the database.
#
# @since 2.1.0
class DBRef
include BSON::JSON

# The constant for the collection reference field.
#
# @since 2.1.0
COLLECTION = '$ref'.freeze

# The constant for the id field.
#
# @since 2.1.0
ID = '$id'.freeze

# The constant for the database field.
#
# @since 2.1.0
DATABASE = '$db'.freeze

# @return [ String ] collection The collection name.
attr_reader :collection

# @return [ BSON::ObjectId ] id The referenced document id.
attr_reader :id

# @return [ String ] database The database name.
attr_reader :database

# Get the DBRef as a JSON document
#
# @example Get the DBRef as a JSON hash.
# dbref.as_json
#
# @return [ Hash ] The max key as a JSON hash.
#
# @since 2.1.0
def as_json(*args)
document = { COLLECTION => collection, ID => id }
document.merge!(DATABASE => database) if database
document
end

# Instantiate a new DBRef.
#
# @example Create the DBRef.
# Mongo::DBRef.new('users', id, 'database')
#
# @param [ String ] collection The collection name.
# @param [ BSON::ObjectId ] id The object id.
# @param [ String ] database The database name.
#
# @since 2.1.0
def initialize(collection, id, database = nil)
@collection = collection
@id = id
@database = database
end

# Converts the DBRef to raw BSON.
#
# @example Convert the DBRef to raw BSON.
# dbref.to_bson
#
# @param [ BSON::ByteBuffer ] buffer The encoded BSON buffer to append to.
# @param [ true, false ] validating_keys Whether keys should be validated when serializing.
#
# @return [ String ] The raw BSON.
#
# @since 2.1.0
def to_bson(buffer = BSON::ByteBuffer.new, validating_keys = BSON::Config.validating_keys?)
as_json.to_bson(buffer)
end

module ClassMethods

# Deserialize the hash from BSON, converting to a DBRef if appropriate.
#
# @param [ String ] buffer The bson representing a hash.
#
# @return [ Hash, DBRef ] The decoded hash or DBRef.
#
# @see http://bsonspec.org/#/specification
#
# @since 2.0.0
def from_bson(buffer, **options)
# bson-ruby 4.8.0 changes #from_bson API to take **options.
# However older bsons fail if invoked with a plain super here,
# even if options are empty.
decoded = if options.empty?
super(buffer)
else
super
end
if ref = decoded[COLLECTION]
decoded = DBRef.new(ref, decoded[ID], decoded[DATABASE])
end
decoded
end
end
end

::Hash.send(:extend, DBRef::ClassMethods)
DBRef = BSON::DBRef
end
2 changes: 1 addition & 1 deletion mongo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ Gem::Specification.new do |s|

s.required_ruby_version = ">= 2.5"

s.add_dependency 'bson', '>=4.8.2', '<5.0.0'
s.add_dependency 'bson', '>=4.13.0', '<5.0.0'
end
152 changes: 0 additions & 152 deletions spec/mongo/dbref_spec.rb

This file was deleted.