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, #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.



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

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



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

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.



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

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)



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

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.



50
51
52
# File 'lib/mongoid/criteria.rb', line 50

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.



56
57
58
# File 'lib/mongoid/criteria.rb', line 56

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.



68
69
70
71
72
73
74
75
76
77
# File 'lib/mongoid/criteria.rb', line 68

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

#blank?Boolean Also known as: empty?

Returns true if the criteria is empty.

Example:

criteria.blank?

Returns:

  • (Boolean)


84
85
86
# File 'lib/mongoid/criteria.rb', line 84

def blank?
  count < 1
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.



94
95
96
# File 'lib/mongoid/criteria.rb', line 94

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 }



104
105
106
107
108
109
110
# File 'lib/mongoid/criteria.rb', line 104

def each(&block)
  return caching(&block) if cached?
  if block_given?
    execute.each { |doc| yield doc }
  end
  self
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



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

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" } })



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

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.



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

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