Module: Mongoid::Criterion::Optional
- Includes:
- Mongoid::Contexts::IdConversion
- Included in:
- Mongoid::Criteria
- Defined in:
- lib/mongoid/criterion/optional.rb
Instance Method Summary collapse
-
#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.
-
#enslave ⇒ Object
Flags the criteria to execute against a read-only slave in the pool instead of master.
-
#enslaved? ⇒ Boolean
Will return true if the criteria is enslaved.
-
#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. -
#id(*args) ⇒ Object
Adds a criterion to the
Criteria
that specifies an id that must be matched. -
#limit(value = 20) ⇒ Object
Adds a criterion to the
Criteria
that specifies the maximum number of results to return. -
#offset(*args) ⇒ Object
Returns the offset option.
-
#order_by(params = []) ⇒ Object
Adds a criterion to the
Criteria
that specifies the sort order of the returned documents in the database. -
#skip(value = 0) ⇒ Object
Adds a criterion to the
Criteria
that specifies how many results to skip when returning Documents.
Instance Method Details
#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
18 19 20 |
# File 'lib/mongoid/criterion/optional.rb', line 18 def cache @options.merge!(:cache => true); self end |
#cached? ⇒ Boolean
Will return true if the cache option has been set.
Example:
criteria.cached?
27 28 29 |
# File 'lib/mongoid/criterion/optional.rb', line 27 def cached? @options[:cache] == true end |
#enslave ⇒ Object
Flags the criteria to execute against a read-only slave in the pool instead of master.
Example:
criteria.enslave
37 38 39 |
# File 'lib/mongoid/criterion/optional.rb', line 37 def enslave @options.merge!(:enslave => true); self end |
#enslaved? ⇒ Boolean
Will return true if the criteria is enslaved.
Example:
criteria.enslaved?
46 47 48 |
# File 'lib/mongoid/criterion/optional.rb', line 46 def enslaved? @options[:enslave] == true 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
62 63 64 |
# File 'lib/mongoid/criterion/optional.rb', line 62 def extras(extras) @options.merge!(extras); ; self end |
#id(*args) ⇒ Object
Adds a criterion to the Criteria
that specifies an id that must be matched.
Options:
object_id: A String
representation of a BSON::ObjectId
Example:
criteria.id("4ab2bc4b8ad548971900005c")
Returns: self
77 78 79 80 81 82 83 84 85 |
# File 'lib/mongoid/criterion/optional.rb', line 77 def id(*args) ids = strings_to_object_ids(args.flatten) if ids.size > 1 self.in(:_id => ids) else @selector[:_id] = ids.first self 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
100 101 102 |
# File 'lib/mongoid/criterion/optional.rb', line 100 def limit(value = 20) @options[:limit] = value; self 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.
107 108 109 |
# File 'lib/mongoid/criterion/optional.rb', line 107 def offset(*args) args.size > 0 ? skip(args.first) : @options[:skip] end |
#order_by(params = []) ⇒ Object
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
123 124 125 |
# File 'lib/mongoid/criterion/optional.rb', line 123 def order_by(params = []) @options[:sort] = params; self 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
141 142 143 |
# File 'lib/mongoid/criterion/optional.rb', line 141 def skip(value = 0) @options[:skip] = value; self end |