mongoid 7.1.0.rc0 → 7.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/CHANGELOG.md +6 -6
  5. data/LICENSE +1 -1
  6. data/README.md +5 -5
  7. data/Rakefile +14 -0
  8. data/lib/config/locales/en.yml +5 -5
  9. data/lib/mongoid.rb +3 -2
  10. data/lib/mongoid/association/accessors.rb +37 -2
  11. data/lib/mongoid/association/embedded/embeds_many.rb +2 -1
  12. data/lib/mongoid/association/embedded/embeds_one.rb +2 -1
  13. data/lib/mongoid/association/many.rb +3 -2
  14. data/lib/mongoid/association/proxy.rb +6 -4
  15. data/lib/mongoid/association/referenced/belongs_to/binding.rb +1 -1
  16. data/lib/mongoid/association/referenced/belongs_to/eager.rb +38 -2
  17. data/lib/mongoid/association/referenced/eager.rb +29 -9
  18. data/lib/mongoid/association/referenced/has_many/enumerable.rb +2 -22
  19. data/lib/mongoid/association/referenced/has_many/proxy.rb +3 -2
  20. data/lib/mongoid/atomic.rb +13 -3
  21. data/lib/mongoid/attributes.rb +28 -20
  22. data/lib/mongoid/clients/factory.rb +2 -2
  23. data/lib/mongoid/clients/options.rb +8 -8
  24. data/lib/mongoid/clients/sessions.rb +20 -4
  25. data/lib/mongoid/clients/storage_options.rb +5 -5
  26. data/lib/mongoid/config.rb +42 -12
  27. data/lib/mongoid/config/options.rb +5 -2
  28. data/lib/mongoid/contextual.rb +5 -4
  29. data/lib/mongoid/contextual/geo_near.rb +3 -2
  30. data/lib/mongoid/contextual/map_reduce.rb +3 -2
  31. data/lib/mongoid/contextual/mongo.rb +2 -1
  32. data/lib/mongoid/criteria.rb +23 -4
  33. data/lib/mongoid/criteria/modifiable.rb +2 -1
  34. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +1 -1
  35. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +6 -6
  36. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +12 -0
  37. data/lib/mongoid/criteria/queryable/mergeable.rb +75 -8
  38. data/lib/mongoid/criteria/queryable/pipeline.rb +3 -2
  39. data/lib/mongoid/criteria/queryable/selectable.rb +120 -13
  40. data/lib/mongoid/criteria/queryable/storable.rb +104 -99
  41. data/lib/mongoid/errors/eager_load.rb +2 -0
  42. data/lib/mongoid/errors/no_client_config.rb +2 -2
  43. data/lib/mongoid/errors/no_default_client.rb +1 -1
  44. data/lib/mongoid/extensions/hash.rb +4 -2
  45. data/lib/mongoid/extensions/regexp.rb +1 -1
  46. data/lib/mongoid/fields.rb +2 -1
  47. data/lib/mongoid/fields/standard.rb +2 -1
  48. data/lib/mongoid/fields/validators/macro.rb +4 -1
  49. data/lib/mongoid/findable.rb +5 -4
  50. data/lib/mongoid/interceptable.rb +5 -1
  51. data/lib/mongoid/matchable/regexp.rb +2 -2
  52. data/lib/mongoid/persistable/pushable.rb +11 -2
  53. data/lib/mongoid/persistence_context.rb +6 -6
  54. data/lib/mongoid/query_cache.rb +61 -18
  55. data/lib/mongoid/railties/database.rake +7 -0
  56. data/lib/mongoid/serializable.rb +10 -2
  57. data/lib/mongoid/shardable.rb +56 -4
  58. data/lib/mongoid/tasks/database.rake +10 -5
  59. data/lib/mongoid/tasks/database.rb +83 -0
  60. data/lib/mongoid/timestamps/timeless.rb +3 -1
  61. data/lib/mongoid/validatable/uniqueness.rb +1 -1
  62. data/lib/mongoid/version.rb +1 -1
  63. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +32 -23
  64. data/lib/rails/generators/mongoid/model/templates/model.rb.tt +1 -1
  65. data/spec/app/models/coding.rb +4 -0
  66. data/spec/app/models/coding/pull_request.rb +12 -0
  67. data/spec/app/models/delegating_patient.rb +16 -0
  68. data/spec/app/models/passport.rb +1 -0
  69. data/spec/app/models/phone.rb +1 -0
  70. data/spec/app/models/publication.rb +5 -0
  71. data/spec/app/models/publication/encyclopedia.rb +12 -0
  72. data/spec/app/models/publication/review.rb +14 -0
  73. data/spec/integration/app_spec.rb +254 -0
  74. data/spec/integration/associations/embedded_spec.rb +54 -0
  75. data/spec/integration/associations/has_many_spec.rb +34 -0
  76. data/spec/integration/associations/has_one_spec.rb +34 -0
  77. data/spec/integration/bson_regexp_raw_spec.rb +20 -0
  78. data/spec/integration/criteria/date_field_spec.rb +41 -0
  79. data/spec/integration/criteria/logical_spec.rb +13 -0
  80. data/spec/integration/document_spec.rb +22 -0
  81. data/spec/integration/shardable_spec.rb +149 -0
  82. data/spec/lite_spec_helper.rb +15 -4
  83. data/spec/mongoid/association/accessors_spec.rb +238 -63
  84. data/spec/mongoid/association/embedded/embeds_many_models.rb +19 -0
  85. data/spec/mongoid/association/embedded/embeds_many_spec.rb +10 -0
  86. data/spec/mongoid/association/embedded/embeds_one_spec.rb +0 -2
  87. data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +193 -10
  88. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +140 -1
  89. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +146 -68
  90. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +2 -1
  91. data/spec/mongoid/attributes_spec.rb +19 -7
  92. data/spec/mongoid/changeable_spec.rb +23 -0
  93. data/spec/mongoid/clients/factory_spec.rb +8 -8
  94. data/spec/mongoid/clients/options_spec.rb +11 -11
  95. data/spec/mongoid/clients/sessions_spec.rb +8 -4
  96. data/spec/mongoid/clients/transactions_spec.rb +20 -8
  97. data/spec/mongoid/clients_spec.rb +2 -2
  98. data/spec/mongoid/contextual/atomic_spec.rb +22 -11
  99. data/spec/mongoid/contextual/geo_near_spec.rb +11 -2
  100. data/spec/mongoid/contextual/map_reduce_spec.rb +20 -5
  101. data/spec/mongoid/contextual/mongo_spec.rb +76 -53
  102. data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +1 -1
  103. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +7 -7
  104. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +1 -1
  105. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +19 -7
  106. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +28 -1
  107. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +45 -12
  108. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +1051 -392
  109. data/spec/mongoid/criteria/queryable/selectable_spec.rb +52 -0
  110. data/spec/mongoid/criteria/queryable/storable_spec.rb +80 -2
  111. data/spec/mongoid/criteria_spec.rb +36 -2
  112. data/spec/mongoid/document_fields_spec.rb +29 -0
  113. data/spec/mongoid/document_persistence_context_spec.rb +33 -0
  114. data/spec/mongoid/errors/no_client_config_spec.rb +2 -2
  115. data/spec/mongoid/errors/no_client_database_spec.rb +3 -3
  116. data/spec/mongoid/errors/no_client_hosts_spec.rb +3 -3
  117. data/spec/mongoid/fields_spec.rb +24 -1
  118. data/spec/mongoid/indexable_spec.rb +6 -4
  119. data/spec/mongoid/interceptable_spec.rb +62 -0
  120. data/spec/mongoid/interceptable_spec_models.rb +76 -0
  121. data/spec/mongoid/matchable/default_spec.rb +1 -1
  122. data/spec/mongoid/matchable/regexp_spec.rb +2 -2
  123. data/spec/mongoid/matchable_spec.rb +2 -2
  124. data/spec/mongoid/persistable/pushable_spec.rb +55 -1
  125. data/spec/mongoid/query_cache_spec.rb +77 -9
  126. data/spec/mongoid/relations/proxy_spec.rb +1 -1
  127. data/spec/mongoid/scopable_spec.rb +2 -1
  128. data/spec/mongoid/serializable_spec.rb +129 -18
  129. data/spec/mongoid/shardable_models.rb +61 -0
  130. data/spec/mongoid/shardable_spec.rb +69 -16
  131. data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
  132. data/spec/mongoid/tasks/database_spec.rb +1 -1
  133. data/spec/spec_helper.rb +2 -31
  134. data/spec/support/child_process_helper.rb +76 -0
  135. data/spec/support/cluster_config.rb +3 -3
  136. data/spec/support/constraints.rb +26 -10
  137. data/spec/support/expectations.rb +3 -1
  138. data/spec/support/helpers.rb +11 -0
  139. data/spec/support/lite_constraints.rb +22 -0
  140. data/spec/support/session_registry.rb +50 -0
  141. data/spec/support/spec_config.rb +12 -4
  142. metadata +518 -480
  143. metadata.gz.sig +0 -0
@@ -74,7 +74,8 @@ module Mongoid
74
74
  # @since 5.1.0
75
75
  def create_with(attrs = {})
76
76
  tap do
77
- (@create_attrs ||= {}).merge!(attrs)
77
+ @create_attrs ||= {}
78
+ @create_attrs.update(attrs)
78
79
  end
79
80
  end
80
81
 
@@ -60,7 +60,7 @@ module Mongoid
60
60
  #
61
61
  # @since 1.0.0
62
62
  def __numeric__(object)
63
- object.to_s =~ /(^[-+]?[0-9]+$)|(\.0+$)|(\.$)/ ? object.to_i : Float(object)
63
+ object.to_s =~ /(\A[-+]?[0-9]+\z)|(\.0+\z)|(\.\z)/ ? object.to_i : Float(object)
64
64
  end
65
65
 
66
66
  # Evolve the object to an integer.
@@ -12,7 +12,7 @@ module Mongoid
12
12
  # Is the object a regexp?
13
13
  #
14
14
  # @example Is the object a regex?
15
- # /^[123]/.regexp?
15
+ # /\A[123]/.regexp?
16
16
  #
17
17
  # @return [ true ] Always true.
18
18
  #
@@ -24,7 +24,7 @@ module Mongoid
24
24
  # Evolve the object into a regex.
25
25
  #
26
26
  # @example Evolve the object to a regex.
27
- # Regexp.evolve("^[123]")
27
+ # Regexp.evolve("\A[123]")
28
28
  #
29
29
  # @param [ Regexp, String ] object The object to evolve.
30
30
  #
@@ -38,7 +38,7 @@ module Mongoid
38
38
  end
39
39
  end
40
40
 
41
- module Raw
41
+ module Raw_
42
42
 
43
43
  # Is the object a regexp?
44
44
  #
@@ -55,7 +55,7 @@ module Mongoid
55
55
  # Evolve the object into a raw bson regex.
56
56
  #
57
57
  # @example Evolve the object to a regex.
58
- # BSON::Regexp::Raw.evolve("^[123]")
58
+ # BSON::Regexp::Raw.evolve("\\A[123]")
59
59
  #
60
60
  # @param [ BSON::Regexp::Raw, String ] object The object to evolve.
61
61
  #
@@ -77,5 +77,5 @@ end
77
77
 
78
78
  ::Regexp.__send__(:include,Mongoid::Criteria::Queryable::Extensions::Regexp)
79
79
  ::Regexp.__send__(:extend, Mongoid::Criteria::Queryable::Extensions::Regexp::ClassMethods)
80
- BSON::Regexp::Raw.__send__(:include,Mongoid::Criteria::Queryable::Extensions::Regexp::Raw)
81
- BSON::Regexp::Raw.__send__(:extend, Mongoid::Criteria::Queryable::Extensions::Regexp::Raw::ClassMethods)
80
+ BSON::Regexp::Raw.__send__(:include,Mongoid::Criteria::Queryable::Extensions::Regexp::Raw_)
81
+ BSON::Regexp::Raw.__send__(:extend, Mongoid::Criteria::Queryable::Extensions::Regexp::Raw_::ClassMethods)
@@ -9,6 +9,18 @@ module Mongoid
9
9
  # This module contains additional time with zone behavior.
10
10
  module TimeWithZone
11
11
 
12
+ # Evolve the time as a date, UTC midnight.
13
+ #
14
+ # @example Evolve the time to a date query format.
15
+ # time.__evolve_date__
16
+ #
17
+ # @return [ Time ] The date at midnight UTC.
18
+ #
19
+ # @since 1.0.0
20
+ def __evolve_date__
21
+ ::Time.utc(year, month, day, 0, 0, 0, 0)
22
+ end
23
+
12
24
  # Evolve the time into a utc time.
13
25
  #
14
26
  # @example Evolve the time.
@@ -163,7 +163,7 @@ module Mongoid
163
163
  if expr.is_a?(Selectable)
164
164
  expr = expr.selector
165
165
  end
166
- normalized = _mongoid_normalize_expr(expr)
166
+ normalized = _mongoid_expand_keys(expr)
167
167
  sel.store(operator, result_criteria.push(normalized))
168
168
  end
169
169
  end
@@ -190,9 +190,9 @@ module Mongoid
190
190
  sel = query.selector
191
191
  _mongoid_flatten_arrays(criteria).each do |criterion|
192
192
  if criterion.is_a?(Selectable)
193
- expr = _mongoid_normalize_expr(criterion.selector)
193
+ expr = _mongoid_expand_keys(criterion.selector)
194
194
  else
195
- expr = criterion
195
+ expr = _mongoid_expand_keys(criterion)
196
196
  end
197
197
  if sel.empty?
198
198
  sel.store(operator, [expr])
@@ -212,7 +212,7 @@ module Mongoid
212
212
  # explicitly only expands Array objects and Array subclasses.
213
213
  private def _mongoid_flatten_arrays(array)
214
214
  out = []
215
- pending = array
215
+ pending = array.dup
216
216
  until pending.empty?
217
217
  item = pending.shift
218
218
  if item.nil?
@@ -226,11 +226,78 @@ module Mongoid
226
226
  out
227
227
  end
228
228
 
229
- # @api private
230
- private def _mongoid_normalize_expr(expr)
231
- expr.inject({}) do |hash, (field, value)|
232
- hash.merge!(field.__expr_part__(value.__expand_complex__))
229
+ # Takes a criteria hash and expands Key objects into hashes containing
230
+ # MQL corresponding to said key objects.
231
+ #
232
+ # Ruby does not permit multiple symbol operators. For example,
233
+ # {:foo.gt => 1, :foo.gt => 2} is collapsed to {:foo.gt => 2} by the
234
+ # language. Therefore this method never has to deal with multiple
235
+ # identical operators.
236
+ #
237
+ # Similarly, this method should never need to expand a literal value
238
+ # and an operator at the same time.
239
+ #
240
+ # @param [ Hash ] Criteria including Key instances.
241
+ #
242
+ # @return [ Hash ] Expanded criteria.
243
+ private def _mongoid_expand_keys(expr)
244
+ unless expr.is_a?(Hash)
245
+ raise ArgumentError, 'Argument must be a Hash'
246
+ end
247
+
248
+ result = {}
249
+ expr.each do |field, value|
250
+ field.__expr_part__(value.__expand_complex__).each do |k, v|
251
+ if result[k]
252
+ if result[k].is_a?(Hash)
253
+ # Existing value is an operator.
254
+ # If new value is also an operator, ensure there are no
255
+ # conflicts and add
256
+ if v.is_a?(Hash)
257
+ # The new value is also an operator.
258
+ # If there are no conflicts, combine the hashes, otherwise
259
+ # add new conditions to top level with $and.
260
+ if (v.keys & result[k].keys).empty?
261
+ result[k].update(v)
262
+ else
263
+ raise NotImplementedError, 'Ruby does not allow same symbol operator with different values'
264
+ result['$and'] ||= []
265
+ result['$and'] << {k => v}
266
+ end
267
+ else
268
+ # The new value is a simple value.
269
+ # If there isn't an $eq operator already in the query,
270
+ # transform the new value into an $eq operator and add it
271
+ # to the existing hash. Otherwise add the new condition
272
+ # with $and to the top level.
273
+ if result[k].key?('$eq')
274
+ raise NotImplementedError, 'Ruby does not allow same symbol operator with different values'
275
+ result['$and'] ||= []
276
+ result['$and'] << {k => v}
277
+ else
278
+ result[k].update('$eq' => v)
279
+ end
280
+ end
281
+ else
282
+ # Existing value is a simple value.
283
+ # If we are adding an operator, and the operator is not $eq,
284
+ # convert existing value into $eq and add the new operator
285
+ # to the same hash. Otherwise add the new condition with $and
286
+ # to the top level.
287
+ if v.is_a?(Hash) && !v.key?('$eq')
288
+ result[k] = {'$eq' => result[k]}.update(v)
289
+ else
290
+ raise NotImplementedError, 'Ruby does not allow same symbol operator with different values'
291
+ result['$and'] ||= []
292
+ result['$and'] << {k => v}
293
+ end
294
+ end
295
+ else
296
+ result[k] = v
297
+ end
298
+ end
233
299
  end
300
+ result
234
301
  end
235
302
 
236
303
  # Adds the criterion to the existing selection.
@@ -33,7 +33,7 @@ module Mongoid
33
33
  # Add a group operation to the aggregation pipeline.
34
34
  #
35
35
  # @example Add a group operation.
36
- # pipeline.group(:count.sum => 1, :max.max => "likes")
36
+ # pipeline.group(:_id => "foo", :count.sum => 1, :max.max => "likes")
37
37
  #
38
38
  # @param [ Hash ] entry The group entry.
39
39
  #
@@ -76,7 +76,8 @@ module Mongoid
76
76
  # pipeline.unwind(:field)
77
77
  # pipeline.unwind(document)
78
78
  #
79
- # @param [ String, Symbol, Hash ] field_or_doc The name of the field or a document.
79
+ # @param [ String, Symbol, Hash ] field_or_doc A field name or a
80
+ # document.
80
81
  #
81
82
  # @return [ Pipeline ] The pipeline.
82
83
  #
@@ -89,11 +89,27 @@ module Mongoid
89
89
  if new_s.is_a?(Selectable)
90
90
  new_s = new_s.selector
91
91
  end
92
- normalized = _mongoid_normalize_expr(new_s)
92
+ normalized = _mongoid_expand_keys(new_s)
93
93
  normalized.each do |k, v|
94
94
  k = k.to_s
95
- if c.selector[k] || k[0] == ?$
96
- c = c.send(:__multi__, [{k => v}], '$and')
95
+ if c.selector[k]
96
+ # There is already a condition on k.
97
+ # If v is an operator, and all existing conditions are
98
+ # also operators, and v isn't present in existing conditions,
99
+ # we can add to existing conditions.
100
+ # Otherwise use $and.
101
+ if v.is_a?(Hash) &&
102
+ v.length == 1 &&
103
+ (new_k = v.keys.first).start_with?('$') &&
104
+ (existing_kv = c.selector[k]).is_a?(Hash) &&
105
+ !existing_kv.key?(new_k) &&
106
+ existing_kv.keys.all? { |sub_k| sub_k.start_with?('$') }
107
+ then
108
+ merged_v = c.selector[k].merge(v)
109
+ c.selector.store(k, merged_v)
110
+ else
111
+ c = c.send(:__multi__, [k => v], '$and')
112
+ end
97
113
  else
98
114
  c.selector.store(k, v)
99
115
  end
@@ -567,17 +583,21 @@ module Mongoid
567
583
  if new_s.is_a?(Selectable)
568
584
  new_s = new_s.selector
569
585
  end
570
- new_s.each do |k, v|
586
+ _mongoid_expand_keys(new_s).each do |k, v|
571
587
  k = k.to_s
572
588
  if c.selector[k] || k[0] == ?$
573
589
  c = c.send(:__multi__, [{'$nor' => [{k => v}]}], '$and')
574
590
  else
575
- if v.is_a?(Regexp)
576
- negated_operator = '$not'
591
+ if v.is_a?(Hash)
592
+ c = c.send(:__multi__, [{'$nor' => [{k => v}]}], '$and')
577
593
  else
578
- negated_operator = '$ne'
594
+ if v.is_a?(Regexp)
595
+ negated_operator = '$not'
596
+ else
597
+ negated_operator = '$ne'
598
+ end
599
+ c = c.send(:__override__, {k => v}, negated_operator)
579
600
  end
580
- c = c.send(:__override__, {k => v}, negated_operator)
581
601
  end
582
602
  end
583
603
  c
@@ -586,13 +606,34 @@ module Mongoid
586
606
  end
587
607
  key :not, :override, "$not"
588
608
 
589
- # Adds $or selection to the selectable.
609
+ # Creates a disjunction using $or from the existing criteria in the
610
+ # receiver and the provided arguments.
611
+ #
612
+ # This behavior (receiver becoming one of the disjunction operands)
613
+ # matches ActiveRecord's +or+ behavior.
614
+ #
615
+ # Use +any_of+ to add a disjunction of the arguments as an additional
616
+ # constraint to the criteria already existing in the receiver.
617
+ #
618
+ # Each argument can be a Hash, a Criteria object, an array of
619
+ # Hash or Criteria objects, or a nested array. Nested arrays will be
620
+ # flattened and can be of any depth. Passing arrays is deprecated.
590
621
  #
591
- # @example Add the $or selection.
622
+ # @example Add the $or selection where both fields must have the specified values.
592
623
  # selectable.or(field: 1, field: 2)
593
624
  #
594
- # @param [ Array<Hash | Criteria> ] criteria Multiple key/value pair
595
- # matches or Criteria objects.
625
+ # @example Add the $or selection where either value match is sufficient.
626
+ # selectable.or({field: 1}, {field: 2})
627
+ #
628
+ # @example Same as previous example but using the deprecated array wrap.
629
+ # selectable.or([{field: 1}, {field: 2}])
630
+ #
631
+ # @example Same as previous example, also deprecated.
632
+ # selectable.or([{field: 1}], [{field: 2}])
633
+ #
634
+ # @param [ Hash | Criteria | Array<Hash | Criteria>, ... ] criteria
635
+ # Multiple key/value pair matches or Criteria objects, or arrays
636
+ # thereof. Passing arrays is deprecated.
596
637
  #
597
638
  # @return [ Selectable ] The new selectable.
598
639
  #
@@ -600,7 +641,73 @@ module Mongoid
600
641
  def or(*criteria)
601
642
  _mongoid_add_top_level_operation('$or', criteria)
602
643
  end
603
- alias :any_of :or
644
+
645
+ # Adds a disjunction of the arguments as an additional constraint
646
+ # to the criteria already existing in the receiver.
647
+ #
648
+ # Use +or+ to make the receiver one of the disjunction operands.
649
+ #
650
+ # Each argument can be a Hash, a Criteria object, an array of
651
+ # Hash or Criteria objects, or a nested array. Nested arrays will be
652
+ # flattened and can be of any depth. Passing arrays is deprecated.
653
+ #
654
+ # @example Add the $or selection where both fields must have the specified values.
655
+ # selectable.any_of(field: 1, field: 2)
656
+ #
657
+ # @example Add the $or selection where either value match is sufficient.
658
+ # selectable.any_of({field: 1}, {field: 2})
659
+ #
660
+ # @example Same as previous example but using the deprecated array wrap.
661
+ # selectable.any_of([{field: 1}, {field: 2}])
662
+ #
663
+ # @example Same as previous example, also deprecated.
664
+ # selectable.any_of([{field: 1}], [{field: 2}])
665
+ #
666
+ # @param [ Hash | Criteria | Array<Hash | Criteria>, ... ] criteria
667
+ # Multiple key/value pair matches or Criteria objects, or arrays
668
+ # thereof. Passing arrays is deprecated.
669
+ #
670
+ # @return [ Selectable ] The new selectable.
671
+ #
672
+ # @since 1.0.0
673
+ def any_of(*criteria)
674
+ criteria = _mongoid_flatten_arrays(criteria)
675
+ case criteria.length
676
+ when 0
677
+ clone
678
+ when 1
679
+ # When we have a single criteria, any_of behaves like and.
680
+ # Note: criteria can be a Query object, which #where method does
681
+ # not support.
682
+ self.and(*criteria)
683
+ else
684
+ # When we have multiple criteria, combine them all with $or
685
+ # and add the result to self.
686
+ exprs = criteria.map do |criterion|
687
+ if criterion.is_a?(Selectable)
688
+ _mongoid_expand_keys(criterion.selector)
689
+ else
690
+ Hash[criterion.map do |k, v|
691
+ if k.is_a?(Symbol)
692
+ [k.to_s, v]
693
+ else
694
+ [k, v]
695
+ end
696
+ end]
697
+ end
698
+ end
699
+ # Should be able to do:
700
+ #where('$or' => exprs)
701
+ # But since that is broken do instead:
702
+ clone.tap do |query|
703
+ if query.selector['$or']
704
+ query.selector.store('$or', query.selector['$or'] + exprs)
705
+ else
706
+ query.selector.store('$or', exprs)
707
+ end
708
+ end
709
+ end
710
+ end
604
711
 
605
712
  # Add a $size selection for array fields.
606
713
  #
@@ -17,67 +17,49 @@ module Mongoid
17
17
  # @api private
18
18
  module Storable
19
19
 
20
- # Adds an operator expression to the selector.
21
- #
22
- # This method takes the operator and the operator value expression
23
- # separately for callers' convenience. It can be considered to
24
- # handle storing the hash `{operator => op_expr}`.
25
- #
26
- # If the selector already has the specified operator in it (on the
27
- # top level), the new condition given in op_expr is added to the
28
- # existing conditions for the specified operator. This is
29
- # straightforward for $and; for other logical operators, the behavior
30
- # of this method is to add the new conditions to the existing operator.
31
- # For example, if the selector is currently:
32
- #
33
- # {'foo' => 'bar', '$or' => [{'hello' => 'world'}]}
34
- #
35
- # ... and operator is '$or' and op_expr is `{'test' => 123'}`,
36
- # the resulting selector will be:
37
- #
38
- # {'foo' => 'bar', '$or' => [{'hello' => 'world'}, {'test' => 123}]}
20
+ # Adds a field expression to the query.
39
21
  #
40
- # This does not implement an OR between the existing selector and the
41
- # new operator expression - handling this is the job of upstream
42
- # methods. This method simply stores op_expr into the selector on the
43
- # assumption that the existing selector is the correct left hand side
44
- # of the operation already.
22
+ # +field+ must be a field name, and it must be a string. The upstream
23
+ # code must have converted other field/key types to the simple string
24
+ # form by the time this method is invoked.
45
25
  #
46
- # For non-logical query-level operators like $where and $text, if
47
- # there already is a top-level operator with the same name, the
48
- # op_expr is added to the selector via a top-level $and operator,
49
- # thus producing a selector having both operator values.
26
+ # +value+ can be of any type, it is written into the selector unchanged.
50
27
  #
51
- # This method does not simplify values (i.e. if the selector is
52
- # currently empty and operator is $and, op_expr is written to the
53
- # selector with $and even if the $and can in principle be elided).
28
+ # This method performs no processing on the provided field value.
54
29
  #
55
- # This method mutates the receiver.
30
+ # Mutates the receiver.
56
31
  #
57
- # @param [ String ] operator The operator to add.
58
- # @param [ Hash ] op_expr Operator value to add.
32
+ # @param [ String ] field The field name.
33
+ # @param [ Object ] value The field value.
59
34
  #
60
35
  # @return [ Storable ] self.
61
- def add_operator_expression(operator, op_expr)
62
- unless operator.is_a?(String)
63
- raise ArgumentError, "Operator must be a string: #{operator}"
64
- end
65
-
66
- unless operator[0] == ?$
67
- raise ArgumentError, "Operator must begin with $: #{operator}"
36
+ def add_field_expression(field, value)
37
+ unless field.is_a?(String)
38
+ raise ArgumentError, "Field must be a string: #{field}"
68
39
  end
69
40
 
70
- if %w($and $nor $or).include?(operator)
71
- return add_logical_operator_expression(operator, op_expr)
41
+ if field[0] == ?$
42
+ raise ArgumentError, "Field cannot be an operator (i.e. begin with $): #{field}"
72
43
  end
73
44
 
74
- # For other operators, if the operator already exists in the
75
- # query, add the new condition with $and, otherwise add the
76
- # new condition to the top level.
77
- if selector[operator]
78
- add_logical_operator_expression('$and', [{operator => op_expr}])
45
+ if selector[field]
46
+ # We already have a restriction by the field we are trying
47
+ # to restrict, combine the restrictions.
48
+ if value.is_a?(Hash) && selector[field].is_a?(Hash) &&
49
+ value.keys.all? { |key|
50
+ key_s = key.to_s
51
+ key_s[0] == ?$ && !selector[field].key?(key_s)
52
+ }
53
+ then
54
+ # Multiple operators can be combined on the same field by
55
+ # adding them to the existing hash.
56
+ new_value = selector[field].merge(value)
57
+ selector.store(field, new_value)
58
+ else
59
+ add_operator_expression('$and', [{field => value}])
60
+ end
79
61
  else
80
- selector.store(operator, op_expr)
62
+ selector.store(field, value)
81
63
  end
82
64
 
83
65
  self
@@ -87,41 +69,40 @@ module Mongoid
87
69
  #
88
70
  # This method only handles logical operators ($and, $nor and $or).
89
71
  # It raises ArgumentError if called with another operator. Note that
90
- # in MongoDB, $not is a field-level operator and not a query-level one
91
- # $not is not handled by this method as a result.
72
+ # in MQL, $not is a field-level operator and not a query-level one,
73
+ # and therefore $not is not handled by this method.
92
74
  #
93
75
  # This method takes the operator and the operator value expression
94
76
  # separately for callers' convenience. It can be considered to
95
77
  # handle storing the hash `{operator => op_expr}`.
96
78
  #
97
- # If the selector already has the specified operator in it (on the
98
- # top level), the new condition given in op_expr is added to the
99
- # existing conditions for the specified operator. This is
100
- # straightforward for $and; for other logical operators, the behavior
101
- # of this method is to add the new conditions to the existing operator.
79
+ # If the selector consists of a single condition which is the specified
80
+ # operator (on the top level), the new condition given in op_expr is
81
+ # added to the existing conditions for the specified operator.
102
82
  # For example, if the selector is currently:
103
83
  #
104
- # {'foo' => 'bar', '$or' => [{'hello' => 'world'}]}
84
+ # {'$or' => [{'hello' => 'world'}]}
105
85
  #
106
- # ... and operator is '$or' and op_expr is `{'test' => 123'}`,
86
+ # ... and operator is '$or' and op_expr is `[{'test' => 123'}]`,
107
87
  # the resulting selector will be:
108
88
  #
109
- # {'foo' => 'bar', '$or' => [{'hello' => 'world'}, {'test' => 123}]}
89
+ # {'$or' => [{'hello' => 'world'}, {'test' => 123}]}
110
90
  #
111
- # This does not implement an OR between the existing selector and the
112
- # new operator expression - handling this is the job of upstream
113
- # methods. This method simply stores op_expr into the selector on the
114
- # assumption that the existing selector is the correct left hand side
115
- # of the operation already.
91
+ # This method always adds the new conditions as additional requirements;
92
+ # in other words, it does not implement the ActiveRecord or/nor behavior
93
+ # where the receiver becomes one of the operands. It is expected that
94
+ # code upstream of this method implements such behavior.
116
95
  #
117
96
  # This method does not simplify values (i.e. if the selector is
118
97
  # currently empty and operator is $and, op_expr is written to the
119
98
  # selector with $and even if the $and can in principle be elided).
99
+ # Such simplification is also expected to have already been performed
100
+ # by the upstream code.
120
101
  #
121
102
  # This method mutates the receiver.
122
103
  #
123
104
  # @param [ String ] operator The operator to add.
124
- # @param [ Hash ] op_expr Operator value to add.
105
+ # @param [ Array<Hash> ] op_expr Operator value to add.
125
106
  #
126
107
  # @return [ Storable ] self.
127
108
  def add_logical_operator_expression(operator, op_expr)
@@ -145,56 +126,80 @@ module Mongoid
145
126
  # with whatever other conditions exist.
146
127
  selector.store(operator, op_expr)
147
128
  else
148
- # Other operators need to operate explicitly on the previous
149
- # conditions and the new condition.
150
- new_value = [selector.to_hash.dup] + op_expr
151
- selector.replace(operator => new_value)
129
+ # Other operators need to be added separately
130
+ if selector[operator]
131
+ add_logical_operator_expression('$and', [operator => op_expr])
132
+ else
133
+ selector.store(operator, op_expr)
134
+ end
152
135
  end
153
136
 
154
137
  self
155
138
  end
156
139
 
157
- # Adds a field expression to the query.
140
+ # Adds an operator expression to the selector.
158
141
  #
159
- # field must be a field name, and it must be a string. The upstream
160
- # code must have converted other field/key types to the simple string
161
- # form by the time this method is invoked.
142
+ # This method takes the operator and the operator value expression
143
+ # separately for callers' convenience. It can be considered to
144
+ # handle storing the hash `{operator => op_expr}`.
162
145
  #
163
- # This method performs no processing on the provided field value.
146
+ # The operator value can be of any type.
164
147
  #
165
- # Mutates the receiver.
148
+ # If the selector already has the specified operator in it (on the
149
+ # top level), the new condition given in op_expr is added to the
150
+ # existing conditions for the specified operator. This is
151
+ # straightforward for $and; for other logical operators, the behavior
152
+ # of this method is to add the new conditions to the existing operator.
153
+ # For example, if the selector is currently:
166
154
  #
167
- # @param [ String ] field The field name.
168
- # @param [ Object ] value The field value.
155
+ # {'foo' => 'bar', '$or' => [{'hello' => 'world'}]}
156
+ #
157
+ # ... and operator is '$or' and op_expr is `{'test' => 123'}`,
158
+ # the resulting selector will be:
159
+ #
160
+ # {'foo' => 'bar', '$or' => [{'hello' => 'world'}, {'test' => 123}]}
161
+ #
162
+ # This does not implement an OR between the existing selector and the
163
+ # new operator expression - handling this is the job of upstream
164
+ # methods. This method simply stores op_expr into the selector on the
165
+ # assumption that the existing selector is the correct left hand side
166
+ # of the operation already.
167
+ #
168
+ # For non-logical query-level operators like $where and $text, if
169
+ # there already is a top-level operator with the same name, the
170
+ # op_expr is added to the selector via a top-level $and operator,
171
+ # thus producing a selector having both operator values.
172
+ #
173
+ # This method does not simplify values (i.e. if the selector is
174
+ # currently empty and operator is $and, op_expr is written to the
175
+ # selector with $and even if the $and can in principle be elided).
176
+ #
177
+ # This method mutates the receiver.
178
+ #
179
+ # @param [ String ] operator The operator to add.
180
+ # @param [ Object ] op_expr Operator value to add.
169
181
  #
170
182
  # @return [ Storable ] self.
171
- def add_field_expression(field, value)
172
- unless field.is_a?(String)
173
- raise ArgumentError, "Field must be a string: #{field}"
183
+ def add_operator_expression(operator, op_expr)
184
+ unless operator.is_a?(String)
185
+ raise ArgumentError, "Operator must be a string: #{operator}"
174
186
  end
175
187
 
176
- if field[0] == ?$
177
- raise ArgumentError, "Field cannot be an operator (i.e. begin with $): #{field}"
188
+ unless operator[0] == ?$
189
+ raise ArgumentError, "Operator must begin with $: #{operator}"
178
190
  end
179
191
 
180
- if selector[field]
181
- # We already have a restriction by the field we are trying
182
- # to restrict, combine the restrictions.
183
- if value.is_a?(Hash) && selector[field].is_a?(Hash) &&
184
- value.keys.all? { |key|
185
- key_s = key.to_s
186
- key_s[0] == ?$ && !selector[field].key?(key_s)
187
- }
188
- then
189
- # Multiple operators can be combined on the same field by
190
- # adding them to the existing hash.
191
- new_value = selector[field].merge(value)
192
- selector.store(field, new_value)
193
- else
194
- add_operator_expression('$and', [{field => value}])
195
- end
192
+ if %w($and $nor $or).include?(operator)
193
+ return add_logical_operator_expression(operator, op_expr)
194
+ end
195
+
196
+ # For other operators, if the operator already exists in the
197
+ # query, add the new condition with $and, otherwise add the
198
+ # new condition to the top level.
199
+ if selector[operator]
200
+ add_logical_operator_expression('$and', [{operator => op_expr}])
196
201
  else
197
- selector.store(field, value)
202
+ selector.store(operator, op_expr)
198
203
  end
199
204
 
200
205
  self