Module: Mongoid::Criterion::Optional
- Included in:
- Mongoid::Criteria
- Defined in:
- lib/mongoid/criterion/optional.rb
Instance Method Summary collapse
-
#ascending(*fields) ⇒ Criteria
(also: #asc)
Adds fields to be sorted in ascending order.
-
#cache ⇒ Criteria
Tells the criteria that the cursor that gets returned needs to be cached.
-
#cached? ⇒ true, false
Will return true if the cache option has been set.
-
#descending(*fields) ⇒ Criteria
(also: #desc)
Adds fields to be sorted in descending order.
-
#extras(extras) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies additional options to be passed to the Ruby driver, in the exact format for the driver. -
#for_ids(*ids) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies an id that must be matched. -
#limit(value = 20) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies the maximum number of results to return. -
#offset(*args) ⇒ Integer
Returns the offset option.
-
#order_by(*args) ⇒ Criteria
(also: #order)
Adds a criterion to the
Criteria
that specifies the sort order of the returned documents in the database. -
#skip(value = 0) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies how many results to skip when returning Documents. -
#type(types) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies a type or an Array of types that must be matched.
Instance Method Details
#ascending(*fields) ⇒ Criteria Also known as: asc
Adds fields to be sorted in ascending order. Will add them in the order they were passed into the method.
16 17 18 19 20 21 |
# File 'lib/mongoid/criterion/optional.rb', line 16 def ascending(*fields) clone.tap do |crit| (crit.) unless fields.first.nil? fields.flatten.each { |field| (crit.[:sort], [ localize(field), :asc ]) } end end |
#cache ⇒ Criteria
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.
33 34 35 |
# File 'lib/mongoid/criterion/optional.rb', line 33 def cache clone.tap { |crit| crit..merge!(:cache => true) } end |
#cached? ⇒ true, false
Will return true if the cache option has been set.
43 44 45 |
# File 'lib/mongoid/criterion/optional.rb', line 43 def cached? [:cache] == true end |
#descending(*fields) ⇒ Criteria Also known as: desc
Adds fields to be sorted in descending order. Will add them in the order they were passed into the method.
57 58 59 60 61 62 |
# File 'lib/mongoid/criterion/optional.rb', line 57 def descending(*fields) clone.tap do |crit| (crit.) unless fields.first.nil? fields.flatten.each { |field| (crit.[:sort], [ localize(field), :desc ]) } end end |
#extras(extras) ⇒ Criteria
Adds a criterion to the Criteria
that specifies additional options to be passed to the Ruby driver, in the exact format for the driver.
74 75 76 77 78 |
# File 'lib/mongoid/criterion/optional.rb', line 74 def extras(extras) clone.tap do |crit| crit..merge!(extras) end end |
#for_ids(*ids) ⇒ Criteria
Adds a criterion to the Criteria
that specifies an id that must be matched.
91 92 93 94 95 96 97 98 99 |
# File 'lib/mongoid/criterion/optional.rb', line 91 def for_ids(*ids) field = klass.fields["_id"] ids.flatten! if ids.size > 1 any_in(:_id => ids.map{ |id| field.serialize(id) }) else where(:_id => field.serialize(ids.first)) end end |
#limit(value = 20) ⇒ Criteria
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.
111 112 113 |
# File 'lib/mongoid/criterion/optional.rb', line 111 def limit(value = 20) clone.tap { |crit| crit.[:limit] = value.to_i } end |
#offset(*args) ⇒ Integer
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.
123 124 125 |
# File 'lib/mongoid/criterion/optional.rb', line 123 def offset(*args) args.size > 0 ? skip(args.first) : [:skip] end |
#order_by(*args) ⇒ Criteria 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”.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/mongoid/criterion/optional.rb', line 136 def order_by(*args) clone.tap do |crit| arguments = args.size == 1 ? args.first : args (crit.) unless args.first.nil? if arguments.is_a?(Array) #[:name, :asc] if arguments.size == 2 && (arguments.first.is_a?(Symbol) || arguments.first.is_a?(String)) (arguments, crit) else arguments.each { |argument| (argument, crit) } end else (arguments, crit) end end end |
#skip(value = 0) ⇒ Criteria
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.
165 166 167 |
# File 'lib/mongoid/criterion/optional.rb', line 165 def skip(value = 0) clone.tap { |crit| crit.[:skip] = value.to_i } end |