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

Direct Known Subclasses

MongoDoc::Criteria::CriteriaWrapper

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.



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

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



155
156
157
158
159
160
161
162
163
# File 'lib/mongoid/criteria.rb', line 155

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)



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/mongoid/criteria.rb', line 189

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

#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



112
113
114
115
116
# File 'lib/mongoid/criteria.rb', line 112

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



140
141
142
143
144
# File 'lib/mongoid/criteria.rb', line 140

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.



169
170
171
# File 'lib/mongoid/criteria.rb', line 169

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