Permalink
Browse files

Adding convenience methods for getting collection and driver from cri…

…teria
  • Loading branch information...
1 parent 1d6c9a7 commit 47d62939a4ebd0e13ecd6448763970b5eca5890f @durran durran committed Aug 28, 2011
View
@@ -32,7 +32,6 @@ class Criteria
include Criterion::Optional
attr_accessor \
- :collection,
:documents,
:embedded,
:ids,
@@ -109,6 +108,18 @@ def ==(other)
end
end
+ # Get the collection associated with the criteria.
+ #
+ # @example Get the collection.
+ # criteria.collection
+ #
+ # @return [ Collection ] The collection.
+ #
+ # @since 2.2.0
+ def collection
+ klass.collection
+ end
+
# Return or create the context in which this criteria should be executed.
#
# This will return an Enumerable context if the class is embedded,
@@ -316,6 +327,18 @@ def comparable(other)
other.is_a?(Criteria) ? other.entries : other
end
+ # Get the raw driver collection from the criteria.
+ #
+ # @example Get the raw driver collection.
+ # criteria.driver
+ #
+ # @return [ Mongo::Collection ] The driver collection.
+ #
+ # @since 2.2.0
+ def driver
+ collection.driver
+ end
+
# Clone or dup the current +Criteria+. This will return a new criteria with
# the selector, options, klass, embedded options, etc intact.
#
@@ -73,23 +73,6 @@ def any_of(*args)
end
alias :or :any_of
- # Loads an array of ids only for the current criteria. Used by eager
- # loading to determine the documents to load.
- #
- # @example Load the related ids.
- # criteria.load_ids("person_id")
- #
- # @param [ String ] key The id or foriegn key string.
- #
- # @return [ Array<String, BSON::ObjectId> ] The ids to load.
- #
- # @since 2.2.0
- def load_ids(key)
- klass.collection.driver.find(
- selector, { :fields => { key => 1 }}
- ).map { |doc| doc[key] }.uniq
- end
-
# Find the matchind document in the criteria, either based on id or
# conditions.
#
@@ -185,6 +168,21 @@ def inclusions
@inclusions ||= []
end
+ # Loads an array of ids only for the current criteria. Used by eager
+ # loading to determine the documents to load.
+ #
+ # @example Load the related ids.
+ # criteria.load_ids("person_id")
+ #
+ # @param [ String ] key The id or foriegn key string.
+ #
+ # @return [ Array<String, BSON::ObjectId> ] The ids to load.
+ #
+ # @since 2.2.0
+ def load_ids(key)
+ driver.find(selector, { :fields => { key => 1 }}).map { |doc| doc[key] }
+ end
+
# Adds a criterion to the +Criteria+ that specifies values to do
# geospacial searches by. The field must be indexed with the "2d" option.
#
@@ -123,7 +123,7 @@ def criteria(metadata, object, type = nil)
# @since 2.2.0
def eager_load(metadata, criteria)
klass, foreign_key = metadata.klass, metadata.foreign_key
- klass.any_in("_id" => criteria.load_ids(foreign_key)).each do |doc|
+ klass.any_in("_id" => criteria.load_ids(foreign_key).uniq).each do |doc|
IdentityMap.set(doc)
end
end
@@ -483,7 +483,7 @@ def criteria(metadata, object, type = nil)
# @since 2.2.0
def eager_load(metadata, criteria)
klass, foreign_key = metadata.klass, metadata.foreign_key
- klass.any_in(foreign_key => criteria.load_ids("_id")).each do |doc|
+ klass.any_in(foreign_key => criteria.load_ids("_id").uniq).each do |doc|
IdentityMap.set_many(doc, foreign_key => doc.send(foreign_key))
end
end
@@ -136,7 +136,7 @@ def criteria(metadata, object, type = nil)
# @since 2.2.0
def eager_load(metadata, criteria)
klass, foreign_key = metadata.klass, metadata.foreign_key
- klass.any_in(foreign_key => criteria.load_ids("_id")).each do |doc|
+ klass.any_in(foreign_key => criteria.load_ids("_id").uniq).each do |doc|
IdentityMap.set_one(doc, foreign_key => doc.send(foreign_key))
end
end

0 comments on commit 47d6293

Please sign in to comment.