Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Avoid a BSON namespace collision if the 10gen driver is detected

All references to BSON in the core lib and core rdoc were also changed to
reference Moped::BSON instead.
  • Loading branch information...
commit f99211e6fba6f5c87a87cf11440d4f4241c066c8 1 parent 1ec6d42
@rmm5t rmm5t authored
View
10 lib/mongoid.rb
@@ -32,7 +32,6 @@
require "origin"
require "moped"
-BSON = Moped::BSON
require "mongoid/extensions"
require "mongoid/errors"
@@ -133,3 +132,12 @@ def session(name)
ActiveModel::Observing::ClassMethods.public_instance_methods(false) <<
{ to: Config }))
end
+
+# Assign a BSON namespace shortcut, but avoid a BSON namespace collision if the
+# 10gen driver is detected. This is placed after all other requires to partially
+# help ensure that the mongoid library itself doesn't use the shortcut.
+begin
+ require "bson"
+rescue LoadError
+ BSON = Moped::BSON
+end
View
2  lib/mongoid/contextual/mongo.rb
@@ -549,7 +549,7 @@ def reset_length
#
# @param [ String ] key The id or foriegn key string.
#
- # @return [ Array<String, BSON::ObjectId> ] The ids to load.
+ # @return [ Array<String, Moped::BSON::ObjectId> ] The ids to load.
#
# @since 3.0.0
def load_ids(key)
View
6 lib/mongoid/criteria.rb
@@ -231,12 +231,12 @@ def field_list
# Find the matchind document(s) in the criteria for the provided ids.
#
# @example Find by an id.
- # criteria.find(BSON::ObjectId.new)
+ # criteria.find(Moped::BSON::ObjectId.new)
#
# @example Find by multiple ids.
- # criteria.find([ BSON::ObjectId.new, BSON::ObjectId.new ])
+ # criteria.find([ Moped::BSON::ObjectId.new, Moped::BSON::ObjectId.new ])
#
- # @param [ Array<BSON::ObjectId> ] args The ids to search for.
+ # @param [ Array<Moped::BSON::ObjectId> ] args The ids to search for.
#
# @return [ Array<Document>, Document ] The matching document(s).
#
View
4 lib/mongoid/document.rb
@@ -109,7 +109,7 @@ def hash
# A Document's is identified absolutely by it's class and database id:
#
- # Person.first.identity #=> [Person, BSON::ObjectId('4f775130a04745933a000003')]
+ # Person.first.identity #=> [Person, Moped::BSON::ObjectId('4f775130a04745933a000003')]
#
# @example Get the identity
# document.identity
@@ -126,7 +126,7 @@ def identity
# an empty +Hash+.
#
# If a primary key is defined, the document's id will be set to that key,
- # otherwise it will be set to a fresh +BSON::ObjectId+ string.
+ # otherwise it will be set to a fresh +Moped::BSON::ObjectId+ string.
#
# @example Create a new document.
# Person.new(:title => "Sir")
View
2  lib/mongoid/extensions.rb
@@ -3,7 +3,7 @@
class Boolean; end
end
-class BSON::ObjectId
+class Moped::BSON::ObjectId
undef :as_json
def as_json(options = nil)
to_s
View
4 lib/mongoid/extensions/string.rb
@@ -135,11 +135,11 @@ def unconvertable_to_bson?
# @example Convert to the object id.
# string.convert_to_object_id
#
- # @return [ String, BSON::ObjectId ] The string or the id.
+ # @return [ String, Moped::BSON::ObjectId ] The string or the id.
#
# @since 3.0.0
def convert_to_object_id
- BSON::ObjectId.legal?(self) ? BSON::ObjectId.from_string(self) : self
+ Moped::BSON::ObjectId.legal?(self) ? Moped::BSON::ObjectId.from_string(self) : self
end
module ClassMethods
View
8 lib/mongoid/fields.rb
@@ -24,9 +24,9 @@ module Fields
field(:_type, default: ->{ self.class.name if hereditary? }, type: String)
field(
:_id,
- default: ->{ BSON::ObjectId.new },
+ default: ->{ Moped::BSON::ObjectId.new },
pre_processed: true,
- type: BSON::ObjectId
+ type: Moped::BSON::ObjectId
)
alias :id :_id
@@ -201,13 +201,13 @@ def replace_field(name, type)
add_field(name, fields[name].options.merge(type: type))
end
- # Convenience method for determining if we are using +BSON::ObjectIds+ as
+ # Convenience method for determining if we are using +Moped::BSON::ObjectIds+ as
# our id.
#
# @example Does this class use object ids?
# person.using_object_ids?
#
- # @return [ true, false ] If the class uses BSON::ObjectIds for the id.
+ # @return [ true, false ] If the class uses Moped::BSON::ObjectIds for the id.
#
# @since 1.0.0
def using_object_ids?
View
6 lib/mongoid/fields/foreign_key.rb
@@ -87,12 +87,12 @@ def mongoize(object)
end
end
- # Is the field a BSON::ObjectId?
+ # Is the field a Moped::BSON::ObjectId?
#
- # @example Is the field a BSON::ObjectId?
+ # @example Is the field a Moped::BSON::ObjectId?
# field.object_id_field?
#
- # @return [ true, false ] If the field is a BSON::ObjectId.
+ # @return [ true, false ] If the field is a Moped::BSON::ObjectId.
#
# @since 2.2.0
def object_id_field?
View
6 lib/mongoid/fields/standard.rb
@@ -112,12 +112,12 @@ def metadata
@metadata ||= options[:metadata]
end
- # Is the field a BSON::ObjectId?
+ # Is the field a Moped::BSON::ObjectId?
#
- # @example Is the field a BSON::ObjectId?
+ # @example Is the field a Moped::BSON::ObjectId?
# field.object_id_field?
#
- # @return [ true, false ] If the field is a BSON::ObjectId.
+ # @return [ true, false ] If the field is a Moped::BSON::ObjectId.
#
# @since 2.2.0
def object_id_field?
View
2  lib/mongoid/finders.rb
@@ -52,7 +52,7 @@ def exists?
# on the conditions provided and the first parameter.
#
# @example Find a single document by an id.
- # Person.find(BSON::ObjectId)
+ # Person.find(Moped::BSON::ObjectId)
#
# @param [ Array ] args An assortment of finder options.
#
View
2  lib/mongoid/relations/accessors.rb
@@ -15,7 +15,7 @@ module Accessors
# person.__build__(:addresses, { :id => 1 }, metadata)
#
# @param [ String, Symbol ] name The name of the relation.
- # @param [ Hash, BSON::ObjectId ] object The id or attributes to use.
+ # @param [ Hash, Moped::BSON::ObjectId ] object The id or attributes to use.
# @param [ Metadata ] metadata The relation's metadata.
# @param [ true, false ] building If we are in a build operation.
#
View
2  lib/mongoid/relations/builder.rb
@@ -17,7 +17,7 @@ class Builder
#
# @param [ Document ] base The base document.
# @param [ Metdata ] metadata The metadata for the relation.
- # @param [ Hash, BSON::ObjectId ] object The attributes to build from or
+ # @param [ Hash, Moped::BSON::ObjectId ] object The attributes to build from or
# id to query with.
#
# @since 2.0.0.rc.1
View
2  lib/mongoid/relations/constraint.rb
@@ -36,7 +36,7 @@ def initialize(metadata)
def convert(object)
return object if metadata.polymorphic?
klass = metadata.klass
- klass.using_object_ids? ? BSON::ObjectId.mongoize(object) : object
+ klass.using_object_ids? ? Moped::BSON::ObjectId.mongoize(object) : object
end
end
end
View
2  lib/mongoid/relations/conversions.rb
@@ -20,7 +20,7 @@ module Conversions
# @since 2.3.0
def flag(object, metadata)
inverse = metadata.inverse_klass
- if inverse.using_object_ids? || object.is_a?(BSON::ObjectId)
+ if inverse.using_object_ids? || object.is_a?(Moped::BSON::ObjectId)
object
else
if object.is_a?(String)
View
4 lib/mongoid/relations/embedded/many.rb
@@ -182,10 +182,10 @@ def destroy_all(conditions = {})
# methods.
#
# @example Find a document by its id.
- # person.addresses.find(BSON::ObjectId.new)
+ # person.addresses.find(Moped::BSON::ObjectId.new)
#
# @example Find documents for multiple ids.
- # person.addresses.find([ BSON::ObjectId.new, BSON::ObjectId.new ])
+ # person.addresses.find([ Moped::BSON::ObjectId.new, Moped::BSON::ObjectId.new ])
#
# @param [ Array<Object> ] args Various arguments.
#
View
4 lib/mongoid/relations/nested_builder.rb
@@ -63,11 +63,11 @@ def update_only?
# @param [ Class ] klass The class we're trying to convert for.
# @param [ String ] id The id, usually coming from the form.
#
- # @return [ BSON::ObjectId, String, Object ] The converted id.
+ # @return [ Moped::BSON::ObjectId, String, Object ] The converted id.
#
# @since 2.0.0.rc.6
def convert_id(klass, id)
- klass.using_object_ids? ? BSON::ObjectId.mongoize(id) : id
+ klass.using_object_ids? ? Moped::BSON::ObjectId.mongoize(id) : id
end
end
end
View
6 lib/mongoid/relations/referenced/many.rb
@@ -170,12 +170,12 @@ def each
# conditions.
#
# @example Find by an id.
- # person.posts.find(BSON::ObjectId.new)
+ # person.posts.find(Moped::BSON::ObjectId.new)
#
# @example Find by multiple ids.
- # person.posts.find([ BSON::ObjectId.new, BSON::ObjectId.new ])
+ # person.posts.find([ Moped::BSON::ObjectId.new, Moped::BSON::ObjectId.new ])
#
- # @param [ BSON::ObjectId, Array<BSON::ObjectId> ] arg The ids.
+ # @param [ Moped::BSON::ObjectId, Array<Moped::BSON::ObjectId> ] arg The ids.
#
# @return [ Document, Criteria ] The matching document(s).
#
Please sign in to comment.
Something went wrong with that request. Please try again.