Class: Mongoid::Criteria
- Includes:
- Enumerable, Mongoid::Criterion::Builder, Mongoid::Criterion::Creational, Mongoid::Criterion::Exclusion, Mongoid::Criterion::Inclusion, Mongoid::Criterion::Inspection, Mongoid::Criterion::Optional, Mongoid::Criterion::Scoping
- Defined in:
- lib/mongoid/criteria.rb
Overview
The Criteria
class is the core object needed in Mongoid to retrieve objects from the database. It is a DSL that essentially sets up the selector and options arguments that get passed on to a Mongo::Collection in the Ruby driver. Each method on the Criteria
returns self to they can be chained in order to create a readable criterion to be executed against the database.
Instance Attribute Summary collapse
-
#documents ⇒ Object
Returns the value of attribute documents.
-
#embedded ⇒ Object
Returns the value of attribute embedded.
-
#field_list ⇒ Object
Returns the value of attribute field_list.
-
#ids ⇒ Object
Returns the value of attribute ids.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#options ⇒ Object
Returns the value of attribute options.
-
#selector ⇒ Object
Returns the value of attribute selector.
Attributes included from Mongoid::Criterion::Scoping
Instance Method Summary collapse
-
#+(other) ⇒ Object
Concatinate the criteria with another enumerable.
-
#-(other) ⇒ Object
Returns the difference between the criteria and another enumerable.
-
#==(other) ⇒ true, false
Returns true if the supplied
Enumerable
orCriteria
is equal to the results of thisCriteria
or the criteria itself. -
#as_conditions ⇒ Hash
Returns the selector and options as a
Hash
that would be passed to a scope for use with named scopes. -
#as_json(options = nil) ⇒ String
Needed to properly get a criteria back as json.
-
#collection ⇒ Collection
Get the collection associated with the criteria.
-
#context ⇒ Mongo, Enumerable
Return or create the context in which this criteria should be executed.
-
#each(&block) ⇒ Criteria
Iterate over each
Document
in the results. -
#exists? ⇒ true, false
Return true if the criteria has some Document or not.
-
#extract_id ⇒ Object
Extract a single id from the provided criteria.
-
#freeze ⇒ Criteria
When freezing a criteria we need to initialize the context first otherwise the setting of the context on attempted iteration will raise a runtime error.
-
#fuse(criteria_conditions = {}) ⇒ Criteria
Merges the supplied argument hash into a single criteria.
-
#initialize(klass, embedded = false) ⇒ Criteria
constructor
Create the new
Criteria
object. -
#merge(other) ⇒ Criteria
Merges another object with this
Criteria
and returns a new criteria. -
#raise_invalid ⇒ Object
Convenience method of raising an invalid options error.
-
#respond_to?(name, include_private = false) ⇒ true, false
Returns true if criteria responds to the given method.
-
#search(*args) ⇒ Array<Symbol, Criteria>
Search for documents based on a variety of args.
Methods included from Mongoid::Criterion::Scoping
#apply_default_scope, #clear_scoping, #default_scopable?, #scoped, #unscoped
Methods included from Mongoid::Criterion::Optional
#ascending, #cache, #cached?, #descending, #extras, #for_ids, #limit, #offset, #order_by, #skip, #type
Methods included from Mongoid::Criterion::Inspection
Methods included from Mongoid::Criterion::Inclusion
#all, #all_of, #also_in, #and, #any_of, #execute_or_raise, #find, #from_map_or_db, #in, #includes, #inclusions, #near, #where
Methods included from Mongoid::Criterion::Exclusion
#excludes, #fields, #not_in, #only, #without
Methods included from Mongoid::Criterion::Creational
Methods included from Mongoid::Criterion::Builder
Constructor Details
#initialize(klass, embedded = false) ⇒ Criteria
Create the new Criteria
object. This will initialize the selector and options hashes, as well as the type of criteria.
207 208 209 210 |
# File 'lib/mongoid/criteria.rb', line 207 def initialize(klass, = false) @selector = Criterion::Selector.new(klass) @options, @klass, @documents, @embedded = {}, klass, [], end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (protected)
Used for chaining Criteria
scopes together in the for of class methods on the Document
the criteria is for.
380 381 382 383 384 385 386 387 388 389 |
# File 'lib/mongoid/criteria.rb', line 380 def method_missing(name, *args, &block) super if [ :safely, :unsafely ].include?(name) if @klass.respond_to?(name) @klass.send(:with_scope, self) do @klass.send(name, *args, &block) end else return entries.send(name, *args) end end |
Instance Attribute Details
#documents ⇒ Object
Returns the value of attribute documents.
35 36 37 |
# File 'lib/mongoid/criteria.rb', line 35 def documents @documents end |
#embedded ⇒ Object
Returns the value of attribute embedded.
35 36 37 |
# File 'lib/mongoid/criteria.rb', line 35 def @embedded end |
#field_list ⇒ Object
Returns the value of attribute field_list.
35 36 37 |
# File 'lib/mongoid/criteria.rb', line 35 def field_list @field_list end |
#ids ⇒ Object
Returns the value of attribute ids.
35 36 37 |
# File 'lib/mongoid/criteria.rb', line 35 def ids @ids end |
#klass ⇒ Object
Returns the value of attribute klass.
35 36 37 |
# File 'lib/mongoid/criteria.rb', line 35 def klass @klass end |
#options ⇒ Object
Returns the value of attribute options.
35 36 37 |
# File 'lib/mongoid/criteria.rb', line 35 def @options end |
#selector ⇒ Object
Returns the value of attribute selector.
35 36 37 |
# File 'lib/mongoid/criteria.rb', line 35 def selector @selector end |
Instance Method Details
#+(other) ⇒ Object
Concatinate the criteria with another enumerable. If the other is a Criteria
then it needs to get the collection from it.
78 79 80 |
# File 'lib/mongoid/criteria.rb', line 78 def +(other) entries + comparable(other) end |
#-(other) ⇒ Object
Returns the difference between the criteria and another enumerable. If the other is a Criteria
then it needs to get the collection from it.
89 90 91 |
# File 'lib/mongoid/criteria.rb', line 89 def -(other) entries - comparable(other) end |
#==(other) ⇒ true, false
This will force a database load when called if an enumerable is passed.
Returns true if the supplied Enumerable
or Criteria
is equal to the results of this Criteria
or the criteria itself.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mongoid/criteria.rb', line 101 def ==(other) case other when Criteria self.selector == other.selector && self. == other. when Enumerable return (execute.entries == other) else return false end end |
#as_conditions ⇒ Hash
Returns the selector and options as a Hash
that would be passed to a scope for use with named scopes.
262 263 264 265 266 267 268 |
# File 'lib/mongoid/criteria.rb', line 262 def as_conditions = @options.dup sorting = .delete(:sort) [:order_by] = sorting if sorting [:includes] = inclusions.map(&:name) if inclusions.any? { :where => @selector }.merge() end |
#as_json(options = nil) ⇒ String
Needed to properly get a criteria back as json
279 280 281 |
# File 'lib/mongoid/criteria.rb', line 279 def as_json( = nil) entries.as_json() end |
#collection ⇒ Collection
Get the collection associated with the criteria.
120 121 122 |
# File 'lib/mongoid/criteria.rb', line 120 def collection klass.collection end |
#context ⇒ Mongo, Enumerable
Return or create the context in which this criteria should be executed.
This will return an Enumerable context if the class is embedded, otherwise it will return a Mongo context for root classes.
133 134 135 |
# File 'lib/mongoid/criteria.rb', line 133 def context @context ||= Contexts.context_for(self, ) end |
#each(&block) ⇒ Criteria
Iterate over each Document
in the results. This can take an optional block to pass to each argument in the results.
144 145 146 |
# File 'lib/mongoid/criteria.rb', line 144 def each(&block) tap { context.iterate(&block) } end |
#exists? ⇒ true, false
Return true if the criteria has some Document or not.
154 155 156 |
# File 'lib/mongoid/criteria.rb', line 154 def exists? context.count > 0 end |
#extract_id ⇒ Object
Extract a single id from the provided criteria. Could be in an $and query or a straight _id query.
167 168 169 |
# File 'lib/mongoid/criteria.rb', line 167 def extract_id selector[:_id] end |
#freeze ⇒ Criteria
When freezing a criteria we need to initialize the context first otherwise the setting of the context on attempted iteration will raise a runtime error.
181 182 183 |
# File 'lib/mongoid/criteria.rb', line 181 def freeze context and inclusions and super end |
#fuse(criteria_conditions = {}) ⇒ Criteria
Merges the supplied argument hash into a single criteria
193 194 195 196 197 |
# File 'lib/mongoid/criteria.rb', line 193 def fuse(criteria_conditions = {}) criteria_conditions.inject(self) do |criteria, (key, value)| criteria.send(key, value) end end |
#merge(other) ⇒ Criteria
Merges another object with this Criteria
and returns a new criteria. The other object may be a Criteria
or a Hash
. This is used to combine multiple scopes together, where a chained scope situation may be desired.
226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/mongoid/criteria.rb', line 226 def merge(other) clone.tap do |crit| if other.is_a?(Criteria) crit.selector.update(other.selector) crit..update(other.) crit.documents = other.documents else duped = other.dup crit.selector.update(duped.delete(:conditions) || {}) crit..update(duped) end end end |
#raise_invalid ⇒ Object
Convenience method of raising an invalid options error.
328 329 330 |
# File 'lib/mongoid/criteria.rb', line 328 def raise_invalid raise Errors::InvalidFind.new end |
#respond_to?(name, include_private = false) ⇒ true, false
Returns true if criteria responds to the given method.
249 250 251 252 253 |
# File 'lib/mongoid/criteria.rb', line 249 def respond_to?(name, include_private = false) return false if [ :safely, :unsafely ].include?(name) # don't include klass private methods because method_missing won't call them super || @klass.respond_to?(name) || entries.respond_to?(name, include_private) end |
#search(*args) ⇒ Array<Symbol, Criteria>
Search for documents based on a variety of args.
307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/mongoid/criteria.rb', line 307 def search(*args) raise_invalid if args[0].nil? type = args[0] params = args[1] || {} return [ :ids, for_ids(type) ] unless type.is_a?(Symbol) conditions = params.delete(:conditions) || {} if conditions.include?(:id) conditions[:_id] = conditions[:id] conditions.delete(:id) end return [ type, where(conditions).extras(params) ] end |