File: optional.rb

package info (click to toggle)
ruby-origin 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 248 kB
  • sloc: ruby: 1,196; makefile: 3
file content (392 lines) | stat: -rw-r--r-- 10,923 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# encoding: utf-8
module Origin

  # The optional module includes all behaviour that has to do with extra
  # options surrounding queries, like skip, limit, sorting, etc.
  module Optional
    extend Macroable

    # @attribute [rw] options The query options.
    attr_accessor :options

    # Add ascending sorting options for all the provided fields.
    #
    # @example Add ascending sorting.
    #   optional.ascending(:first_name, :last_name)
    #
    # @param [ Array<Symbol> ] fields The fields to sort.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 1.0.0
    def ascending(*fields)
      sort_with_list(*fields, 1)
    end
    alias :asc :ascending
    key :asc, :override, 1
    key :ascending, :override, 1

    # Adds the option for telling MongoDB how many documents to retrieve in
    # it's batching.
    #
    # @example Apply the batch size options.
    #   optional.batch_size(500)
    #
    # @param [ Integer ] value The batch size.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 1.0.0
    def batch_size(value = nil)
      option(value) { |options| options.store(:batch_size, value) }
    end

    # Add descending sorting options for all the provided fields.
    #
    # @example Add descending sorting.
    #   optional.descending(:first_name, :last_name)
    #
    # @param [ Array<Symbol> ] fields The fields to sort.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 1.0.0
    def descending(*fields)
      sort_with_list(*fields, -1)
    end
    alias :desc :descending
    key :desc, :override, -1
    key :descending, :override, -1

    # Add an index hint to the query options.
    #
    # @example Add an index hint.
    #   optional.hint("$natural" => 1)
    #
    # @param [ Hash ] value The index hint.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 1.0.0
    def hint(value = nil)
      option(value) { |options| options.store(:hint, value) }
    end

    # Add the number of documents to limit in the returned results.
    #
    # @example Limit the number of returned documents.
    #   optional.limit(20)
    #
    # @param [ Integer ] value The number of documents to return.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 1.0.0
    def limit(value = nil)
      option(value) do |options, query|
        val = value.to_i
        options.store(:limit, val)
        query.pipeline.push("$limit" => val) if aggregating?
      end
    end

    # Adds the option to limit the number of documents scanned in the
    # collection.
    #
    # @example Add the max scan limit.
    #   optional.max_scan(1000)
    #
    # @param [ Integer ] value The max number of documents to scan.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 1.0.0
    def max_scan(value = nil)
      option(value) { |options| options.store(:max_scan, value) }
    end

    # Tell the query not to timeout.
    #
    # @example Tell the query not to timeout.
    #   optional.no_timeout
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 1.0.0
    def no_timeout
      clone.tap { |query| query.options.store(:timeout, false) }
    end

    # Limits the results to only contain the fields provided.
    #
    # @example Limit the results to the provided fields.
    #   optional.only(:name, :dob)
    #
    # @param [ Array<Symbol> ] args The fields to return.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 1.0.0
    def only(*args)
      args = args.flatten
      option(*args) do |options|
        options.store(
          :fields, args.inject(options[:fields] || {}){ |sub, field| sub.tap { sub[field] = 1 }}
        )
      end
    end

    # Adds sorting criterion to the options.
    #
    # @example Add sorting options via a hash with integer directions.
    #   optional.order_by(name: 1, dob: -1)
    #
    # @example Add sorting options via a hash with symbol directions.
    #   optional.order_by(name: :asc, dob: :desc)
    #
    # @example Add sorting options via a hash with string directions.
    #   optional.order_by(name: "asc", dob: "desc")
    #
    # @example Add sorting options via an array with integer directions.
    #   optional.order_by([[ name, 1 ], [ dob, -1 ]])
    #
    # @example Add sorting options via an array with symbol directions.
    #   optional.order_by([[ name, :asc ], [ dob, :desc ]])
    #
    # @example Add sorting options via an array with string directions.
    #   optional.order_by([[ name, "asc" ], [ dob, "desc" ]])
    #
    # @example Add sorting options with keys.
    #   optional.order_by(:name.asc, :dob.desc)
    #
    # @example Add sorting options via a string.
    #   optional.order_by("name ASC, dob DESC")
    #
    # @param [ Array, Hash, String ] spec The sorting specification.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 1.0.0
    def order_by(*spec)
      option(spec) do |options, query|
        spec.compact.each do |criterion|
          criterion.__sort_option__.each_pair do |field, direction|
            add_sort_option(options, field, direction)
          end
          query.pipeline.push("$sort" => options[:sort]) if aggregating?
        end
      end
    end
    alias :order :order_by

    # Instead of merging the order criteria, use this method to completely
    # replace the existing ordering with the provided.
    #
    # @example Replace the ordering.
    #   optional.reorder(name: :asc)
    #
    # @param [ Array, Hash, String ] spec The sorting specification.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 2.1.0
    def reorder(*spec)
      options.delete(:sort)
      order_by(*spec)
    end

    # Add the number of documents to skip.
    #
    # @example Add the number to skip.
    #   optional.skip(100)
    #
    # @param [ Integer ] value The number to skip.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 1.0.0
    def skip(value = nil)
      option(value) do |options, query|
        val = value.to_i
        options.store(:skip, val)
        query.pipeline.push("$skip" => val) if aggregating?
      end
    end
    alias :offset :skip

    # Limit the returned results via slicing embedded arrays.
    #
    # @example Slice the returned results.
    #   optional.slice(aliases: [ 0, 5 ])
    #
    # @param [ Hash ] criterion The slice options.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 1.0.0
    def slice(criterion = nil)
      option(criterion) do |options|
        options.__union__(
          fields: criterion.inject({}) do |option, (field, val)|
            option.tap { |opt| opt.store(field, { "$slice" => val }) }
          end
        )
      end
    end

    # Tell the query to operate in snapshot mode.
    #
    # @example Add the snapshot option.
    #   optional.snapshot
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 1.0.0
    def snapshot
      clone.tap do |query|
        query.options.store(:snapshot, true)
      end
    end

    # Limits the results to only contain the fields not provided.
    #
    # @example Limit the results to the fields not provided.
    #   optional.without(:name, :dob)
    #
    # @param [ Array<Symbol> ] args The fields to ignore.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 1.0.0
    def without(*args)
      args = args.flatten
      option(*args) do |options|
        options.store(
          :fields, args.inject(options[:fields] || {}){ |sub, field| sub.tap { sub[field] = 0 }}
        )
      end
    end

    # Associate a comment with the query.
    #
    # @example Add a comment.
    #   optional.comment('slow query')
    #
    # @note Set profilingLevel to 2 and the comment will be logged in the profile
    #   collection along with the query.
    #
    # @param [ String ] comment The comment to be associated with the query.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 2.2.0
    def comment(comment = nil)
      clone.tap do |query|
        query.options.store(:comment, comment)
      end
    end

    # Set the cursor type.
    #
    # @example Set the cursor type.
    #   optional.cursor_type(:tailable)
    #   optional.cursor_type(:tailable_await)
    #
    # @note The cursor can be type :tailable or :tailable_await.
    #
    # @param [ Symbol ] type The type of cursor to create.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 2.2.0
    def cursor_type(type)
      clone.tap { |query| query.options.store(:cursor_type, type) }
    end

    private

    # Add a single sort option.
    #
    # @api private
    #
    # @example Add a single sort option.
    #   optional.add_sort_option({}, :name, 1)
    #
    # @param [ Hash ] options The options.
    # @param [ String ] field The field name.
    # @param [ Integer ] direction The sort direction.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 1.0.0
    def add_sort_option(options, field, direction)
      if driver == :mongo1x
        sorting = (options[:sort] || []).dup
        sorting.push([ field, direction ])
        options.store(:sort, sorting)
      else
        sorting = (options[:sort] || {}).dup
        sorting[field] = direction
        options.store(:sort, sorting)
      end
    end

    # Take the provided criterion and store it as an option in the query
    # options.
    #
    # @api private
    #
    # @example Store the option.
    #   optional.option({ skip: 10 })
    #
    # @param [ Array ] args The options.
    #
    # @return [ Queryable ] The cloned queryable.
    #
    # @since 1.0.0
    def option(*args)
      clone.tap do |query|
        unless args.compact.empty?
          yield(query.options, query)
        end
      end
    end

    # Add multiple sort options at once.
    #
    # @api private
    #
    # @example Add multiple sort options.
    #   optional.sort_with_list(:name, :dob, 1)
    #
    # @param [ Array<String> ] fields The field names.
    # @param [ Integer ] direction The sort direction.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 1.0.0
    def sort_with_list(*fields, direction)
      option(fields) do |options, query|
        fields.flatten.compact.each do |field|
          add_sort_option(options, field, direction)
        end
        query.pipeline.push("$sort" => options[:sort]) if aggregating?
      end
    end

    class << self

      # Get the methods on the optional that can be forwarded to from a model.
      #
      # @example Get the forwardable methods.
      #   Optional.forwardables
      #
      # @return [ Array<Symbol> ] The names of the forwardable methods.
      #
      # @since 1.0.0
      def forwardables
        public_instance_methods(false) - [ :options, :options= ]
      end
    end
  end
end