Class: Mongoid::Criteria

Inherits:
Object show all
Includes:
Enumerable, Mongoid::Criterion::Exclusion, Mongoid::Criterion::Inclusion, Mongoid::Criterion::Optional
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.

Example setup:

criteria = Criteria.new

criteria.only(:field).where(:field => "value").skip(20).limit(20)

criteria.execute

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mongoid::Criterion::Optional

#cache, #cached?, #enslave, #enslaved?, #extras, #id, #limit, #offset, #order_by, #skip

Methods included from Mongoid::Criterion::Inclusion

#all, #and, #in, #near, #where

Methods included from Mongoid::Criterion::Exclusion

#excludes, #not_in, #only

Constructor Details

#initialize(klass) ⇒ Criteria

Create the new Criteria object. This will initialize the selector and options hashes, as well as the type of criteria.

Options:

type: One of :all, :first:, or :last klass: The class to execute on.



135
136
137
# File 'lib/mongoid/criteria.rb', line 135

def initialize(klass)
  @selector, @options, @klass, @documents = {}, {}, klass, []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Used for chaining Criteria scopes together in the for of class methods on the Document the criteria is for.

Options:

name: The name of the class method on the Document to chain. args: The arguments passed to the method.

Returns: Criteria



165
166
167
168
169
170
171
172
173
# File 'lib/mongoid/criteria.rb', line 165

def method_missing(name, *args)
  if @klass.respond_to?(name)
    new_scope = @klass.send(name, *args)
    new_scope.merge(self)
    return new_scope
  else
    return entries.send(name, *args)
  end
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



28
29
30
# File 'lib/mongoid/criteria.rb', line 28

def collection
  @collection
end

#documentsObject

Returns the value of attribute documents.



29
30
31
# File 'lib/mongoid/criteria.rb', line 29

def documents
  @documents
end

#idsObject (readonly)

Returns the value of attribute ids.



28
29
30
# File 'lib/mongoid/criteria.rb', line 28

def ids
  @ids
end

#klassObject (readonly)

Returns the value of attribute klass.



28
29
30
# File 'lib/mongoid/criteria.rb', line 28

def klass
  @klass
end

#optionsObject (readonly)

Returns the value of attribute options.



28
29
30
# File 'lib/mongoid/criteria.rb', line 28

def options
  @options
end

#selectorObject (readonly)

Returns the value of attribute selector.



28
29
30
# File 'lib/mongoid/criteria.rb', line 28

def selector
  @selector
end

Class Method Details

.translate(*args) ⇒ Object

Translate the supplied arguments into a Criteria object.

If the passed in args is a single String, then it will construct an id Criteria from it.

If the passed in args are a type and a hash, then it will construct the Criteria with the proper selector, options, and type.

Options:

args: either a String or a Symbol, +Hash combination.

Example:

Criteria.translate(Person, "4ab2bc4b8ad548971900005c") Criteria.translate(Person, :conditions => { :field => "value"}, :limit => 20)



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/mongoid/criteria.rb', line 199

def self.translate(*args)
  klass = args[0]
  params = args[1] || {}
  unless params.is_a?(Hash)
    return new(klass).id_criteria(params)
  end
  conditions = params.delete(:conditions) || {}
  if conditions.include?(:id)
    conditions[:_id] = conditions[:id]
    conditions.delete(:id)
  end
  return new(klass).where(conditions).extras(params)
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.



53
54
55
# File 'lib/mongoid/criteria.rb', line 53

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.



59
60
61
# File 'lib/mongoid/criteria.rb', line 59

def -(other)
  entries - comparable(other)
end

#==(other) ⇒ Object

Returns true if the supplied Enumerable or Criteria is equal to the results of this Criteria or the criteria itself.

This will force a database load when called if an enumerable is passed.

Options:

other: The other Enumerable or Criteria to compare to.



71
72
73
74
75
76
77
78
79
80
# File 'lib/mongoid/criteria.rb', line 71

def ==(other)
  case other
  when Criteria
    self.selector == other.selector && self.options == other.options
  when Enumerable
    return (execute.entries == other)
  else
    return false
  end
end

#contextObject

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.



86
87
88
# File 'lib/mongoid/criteria.rb', line 86

def context
  @context ||= Contexts.context_for(self)
end

#each(&block) ⇒ Object

Iterate over each Document in the results. This can take an optional block to pass to each argument in the results.

Example:

criteria.each { |doc| p doc }



96
97
98
99
# File 'lib/mongoid/criteria.rb', line 96

def each(&block)
  context.iterate(&block)
  self
end

#exists?Boolean

Return true if the criteria has some Document or not

Example:

criteria.exists?

Returns:



106
107
108
# File 'lib/mongoid/criteria.rb', line 106

def exists?
  context.count > 0
end

#fuse(criteria_conditions = {}) ⇒ Object

Merges the supplied argument hash into a single criteria

Options:

criteria_conditions: Hash of criteria keys, and parameter values

Example:

criteria.fuse(:where => { :field => "value"}, :limit => 20)

Returns self



122
123
124
125
126
# File 'lib/mongoid/criteria.rb', line 122

def fuse(criteria_conditions = {})
  criteria_conditions.inject(self) do |criteria, (key, value)|
    criteria.send(key, value)
  end
end

#merge(other) ⇒ Object

Merges another object into this 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.

Options:

other: The Criteria or Hash to merge with.

Example:

criteria.merge({ :conditions => { :title => "Sir" } })



150
151
152
153
154
# File 'lib/mongoid/criteria.rb', line 150

def merge(other)
  @selector.update(other.selector)
  @options.update(other.options)
  @documents = other.documents
end

#scopedObject

Returns the selector and options as a Hash that would be passed to a scope for use with named scopes.



179
180
181
# File 'lib/mongoid/criteria.rb', line 179

def scoped
  { :where => @selector }.merge(@options)
end