Module: Origin::Optional
Overview
The optional module includes all behaviour that has to do with extra options surrounding queries, like skip, limit, sorting, etc.
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
- #options The query options.(Thequeryoptions.) ⇒ Object
Class Method Summary collapse
-
.forwardables ⇒ Array<Symbol>
Get the methods on the optional that can be forwarded to from a model.
Instance Method Summary collapse
-
#ascending(*fields) ⇒ Optional
(also: #asc)
Add ascending sorting options for all the provided fields.
-
#batch_size(value = nil) ⇒ Optional
Adds the option for telling MongoDB how many documents to retrieve in it’s batching.
-
#descending(*fields) ⇒ Optional
(also: #desc)
Add descending sorting options for all the provided fields.
-
#hint(value = nil) ⇒ Optional
Add an index hint to the query options.
-
#limit(value = nil) ⇒ Optional
Add the number of documents to limit in the returned results.
-
#max_scan(value = nil) ⇒ Optional
Adds the option to limit the number of documents scanned in the collection.
-
#no_timeout ⇒ Optional
Tell the query not to timeout.
-
#only(*args) ⇒ Optional
Limits the results to only contain the fields provided.
-
#order_by(*spec) ⇒ Optional
Adds sorting criterion to the options.
-
#skip(value = nil) ⇒ Optional
(also: #offset)
Add the number of documents to skip.
-
#slice(criterion = nil) ⇒ Optional
Limit the returned results via slicing embedded arrays.
-
#snapshot ⇒ Optional
Tell the query to operate in snapshot mode.
-
#without(*args) ⇒ Optional
Limits the results to only contain the fields not provided.
Methods included from Macroable
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
10 11 12 |
# File 'lib/origin/optional.rb', line 10 def @options end |
#options The query options.(Thequeryoptions.) ⇒ Object
10 |
# File 'lib/origin/optional.rb', line 10 attr_accessor :options |
Class Method Details
.forwardables ⇒ Array<Symbol>
Get the methods on the optional that can be forwarded to from a model.
324 325 326 |
# File 'lib/origin/optional.rb', line 324 def forwardables public_instance_methods(false) - [ :options, :options= ] end |
Instance Method Details
#ascending(*fields) ⇒ Optional Also known as: asc
Add ascending sorting options for all the provided fields.
22 23 24 |
# File 'lib/origin/optional.rb', line 22 def ascending(*fields) sort_with_list(*fields, 1) end |
#batch_size(value = nil) ⇒ Optional
Adds the option for telling MongoDB how many documents to retrieve in it’s batching.
40 41 42 |
# File 'lib/origin/optional.rb', line 40 def batch_size(value = nil) option(value) { || .store(:batch_size, value) } end |
#descending(*fields) ⇒ Optional Also known as: desc
Add descending sorting options for all the provided fields.
54 55 56 |
# File 'lib/origin/optional.rb', line 54 def descending(*fields) sort_with_list(*fields, -1) end |
#hint(value = nil) ⇒ Optional
Add an index hint to the query options.
71 72 73 |
# File 'lib/origin/optional.rb', line 71 def hint(value = nil) option(value) { || .store(:hint, value) } end |
#limit(value = nil) ⇒ Optional
Add the number of documents to limit in the returned results.
85 86 87 |
# File 'lib/origin/optional.rb', line 85 def limit(value = nil) option(value) { || .store(:limit, value.to_i) } end |
#max_scan(value = nil) ⇒ Optional
Adds the option to limit the number of documents scanned in the collection.
100 101 102 |
# File 'lib/origin/optional.rb', line 100 def max_scan(value = nil) option(value) { || .store(:max_scan, value) } end |
#no_timeout ⇒ Optional
Tell the query not to timeout.
112 113 114 |
# File 'lib/origin/optional.rb', line 112 def no_timeout clone.tap { |query| query..store(:timeout, false) } end |
#only(*args) ⇒ Optional
Limits the results to only contain the fields provided.
126 127 128 129 130 131 132 133 |
# File 'lib/origin/optional.rb', line 126 def only(*args) args = args.flatten option(*args) do || .store( :fields, args.inject({}){ |sub, field| sub.tap { sub[field] = 1 }} ) end end |
#order_by(*spec) ⇒ Optional
Adds sorting criterion to the options.
166 167 168 169 170 171 172 173 174 |
# File 'lib/origin/optional.rb', line 166 def order_by(*spec) option(spec) do || spec.compact.each do |criterion| criterion.__sort_option__.each_pair do |field, direction| add_sort_option(, field, direction) end end end end |
#skip(value = nil) ⇒ Optional Also known as: offset
Add the number of documents to skip.
186 187 188 |
# File 'lib/origin/optional.rb', line 186 def skip(value = nil) option(value) { || .store(:skip, value.to_i) } end |
#slice(criterion = nil) ⇒ Optional
Limit the returned results via slicing embedded arrays.
201 202 203 204 205 206 207 208 209 |
# File 'lib/origin/optional.rb', line 201 def slice(criterion = nil) option(criterion) do || .__union__( fields: criterion.inject({}) do |option, (field, val)| option.tap { |opt| opt.store(field, { "$slice" => val }) } end ) end end |
#snapshot ⇒ Optional
Tell the query to operate in snapshot mode.
219 220 221 222 223 |
# File 'lib/origin/optional.rb', line 219 def snapshot clone.tap do |query| query..store(:snapshot, true) end end |
#without(*args) ⇒ Optional
Limits the results to only contain the fields not provided.
235 236 237 238 239 240 241 242 |
# File 'lib/origin/optional.rb', line 235 def without(*args) args = args.flatten option(*args) do || .store( :fields, args.inject({}){ |sub, field| sub.tap { sub[field] = 0 }} ) end end |