Module: Mongoid::Criterion::Optional
- Included in:
- Mongoid::Criteria
- Defined in:
- lib/mongoid/criterion/optional.rb
Instance Method Summary collapse
-
#ascending(*fields) ⇒ Object
(also: #asc)
Adds fields to be sorted in ascending order.
-
#cache ⇒ Object
Tells the criteria that the cursor that gets returned needs to be cached.
-
#cached? ⇒ Boolean
Will return true if the cache option has been set.
-
#descending(*fields) ⇒ Object
(also: #desc)
Adds fields to be sorted in descending order.
-
#extras(extras) ⇒ Object
Adds a criterion to the
Criteriathat specifies additional options to be passed to the Ruby driver, in the exact format for the driver. -
#for_ids(*ids) ⇒ Object
Adds a criterion to the
Criteriathat specifies an id that must be matched. -
#limit(value = 20) ⇒ Object
Adds a criterion to the
Criteriathat specifies the maximum number of results to return. -
#offset(*args) ⇒ Object
Returns the offset option.
-
#order_by(*args) ⇒ Object
(also: #order)
Adds a criterion to the
Criteriathat specifies the sort order of the returned documents in the database. -
#skip(value = 0) ⇒ Object
Adds a criterion to the
Criteriathat specifies how many results to skip when returning Documents. -
#type(types) ⇒ Object
Adds a criterion to the
Criteriathat specifies a type or an Array of type that must be matched.
Instance Method Details
#ascending(*fields) ⇒ Object Also known as: asc
Adds fields to be sorted in ascending order. Will add them in the order they were passed into the method.
Example:
criteria.ascending(:title, :dob)
12 13 14 15 16 17 |
# File 'lib/mongoid/criterion/optional.rb', line 12 def ascending(*fields) clone.tap do |crit| crit.[:sort] = [] unless [:sort] || fields.first.nil? fields.flatten.each { |field| crit.[:sort] << [ field, :asc ] } end end |
#cache ⇒ Object
Tells the criteria that the cursor that gets returned needs to be cached. This is so multiple iterations don't hit the database multiple times, however this is not advisable when working with large data sets as the entire results will get stored in memory.
Example:
criteria.cache
28 29 30 |
# File 'lib/mongoid/criterion/optional.rb', line 28 def cache clone.tap { |crit| crit..merge!(:cache => true) } end |
#cached? ⇒ Boolean
Will return true if the cache option has been set.
Example:
criteria.cached?
37 38 39 |
# File 'lib/mongoid/criterion/optional.rb', line 37 def cached? [:cache] == true end |
#descending(*fields) ⇒ Object Also known as: desc
Adds fields to be sorted in descending order. Will add them in the order they were passed into the method.
Example:
criteria.descending(:title, :dob)
47 48 49 50 51 52 |
# File 'lib/mongoid/criterion/optional.rb', line 47 def descending(*fields) clone.tap do |crit| crit.[:sort] = [] unless [:sort] || fields.first.nil? fields.flatten.each { |field| crit.[:sort] << [ field, :desc ] } end end |
#extras(extras) ⇒ Object
Adds a criterion to the Criteria that specifies additional options
to be passed to the Ruby driver, in the exact format for the driver.
Options:
extras: A Hash that gets set to the driver options.
Example:
criteria.extras(:limit => 20, :skip => 40)
Returns: self
67 68 69 70 71 |
# File 'lib/mongoid/criterion/optional.rb', line 67 def extras(extras) clone.tap do |crit| crit..merge!(extras) end end |
#for_ids(*ids) ⇒ Object
Adds a criterion to the Criteria that specifies an id that must be matched.
Options:
object_id: A single id or an array of ids in String or BSON::ObjectId format
Example:
criteria.for_ids("4ab2bc4b8ad548971900005c") criteria.for_ids(["4ab2bc4b8ad548971900005c", "4c454e7ebf4b98032d000001"])
Returns: self
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/mongoid/criterion/optional.rb', line 85 def for_ids(*ids) ids.flatten! if ids.size > 1 any_in( :_id => ::BSON::ObjectId.convert(klass, ids) ) else clone.tap do |crit| crit.selector[:_id] = ::BSON::ObjectId.convert(klass, ids.first) end end end |
#limit(value = 20) ⇒ Object
Adds a criterion to the Criteria that specifies the maximum number of
results to return. This is mostly used in conjunction with skip()
to handle paginated results.
Options:
value: An Integer specifying the max number of results. Defaults to 20.
Example:
criteria.limit(100)
Returns: self
112 113 114 |
# File 'lib/mongoid/criterion/optional.rb', line 112 def limit(value = 20) clone.tap { |crit| crit.[:limit] = value } end |
#offset(*args) ⇒ Object
Returns the offset option. If a per_page option is in the list then it will replace it with a skip parameter and return the same value. Defaults to 20 if nothing was provided.
119 120 121 |
# File 'lib/mongoid/criterion/optional.rb', line 119 def offset(*args) args.size > 0 ? skip(args.first) : [:skip] end |
#order_by(*args) ⇒ Object Also known as: order
Adds a criterion to the Criteria that specifies the sort order of
the returned documents in the database. Similar to a SQL "ORDER BY".
Options:
params: An Array of [field, direction] sorting pairs.
Example:
criteria.order_by([[:field1, :asc], [:field2, :desc]])
Returns: self
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/mongoid/criterion/optional.rb', line 135 def order_by(*args) clone.tap do |crit| crit.[:sort] = [] unless [:sort] || args.first.nil? arguments = args.first case arguments when Hash arguments.each do |field, direction| crit.[:sort] << [ field, direction ] end when Array crit.[:sort].concat(arguments) when Complex args.flatten.each do |complex| crit.[:sort] << [ complex.key, complex.operator.to_sym ] end end end end |
#skip(value = 0) ⇒ Object
Adds a criterion to the Criteria that specifies how many results to skip
when returning Documents. This is mostly used in conjunction with
limit() to handle paginated results, and is similar to the
traditional "offset" parameter.
Options:
value: An Integer specifying the number of results to skip. Defaults to 0.
Example:
criteria.skip(20)
Returns: self
169 170 171 |
# File 'lib/mongoid/criterion/optional.rb', line 169 def skip(value = 0) clone.tap { |crit| crit.[:skip] = value } end |
#type(types) ⇒ Object
Adds a criterion to the Criteria that specifies a type or an Array of
type that must be matched.
Options:
types : An Array of types of a String representing the Type of you search
Example:
criteria.type('Browser') criteria.type(['Firefox', 'Browser'])
Returns: self
186 187 188 189 |
# File 'lib/mongoid/criterion/optional.rb', line 186 def type(types) types = [types] unless types.is_a?(Array) any_in(:_type => types) end |