Skip to content

Commit fb1d280

Browse files
johnnyshieldsjamis
andauthored
MONGOID-5671 [Monkey Patch Removal] Remove Object#blank_criteria? and Hash#__mongoid_unsatisfiable_criteria? (#5700)
* - Remove ``Object#blank_criteria?`` method entirely (was previously deprecated and not used in code.) - Remove ``Hash#_mongoid_unsatisfiable_criteria?`` method is removed (was previously marked @api private) and move it to a private method of Referenced::HasMany::Enumerable. * Update enumerable.rb * removing the specs for unsatisifiable_criteria this tests an implementation detail and not a behavior, which is fragile. I'm not even sure this is an implementation detail we want, and testing it specifically pours metaphorical concrete around it, making it that much harder to remove later. --------- Co-authored-by: Jamis Buck <[email protected]>
1 parent 7378141 commit fb1d280

File tree

8 files changed

+38
-226
lines changed

8 files changed

+38
-226
lines changed

docs/release-notes/mongoid-9.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Deprecated functionality removed
7474
The method ``Mongoid::QueryCache#clear_cache`` should be replaced with ``Mongo::QueryCache#clear``.
7575
All other methods and submodules are identically named. Refer to the `driver query cache documentation
7676
<https://mongodb.com/docs/ruby-driver/current/reference/query-cache/>`_ for more details.
77+
- ``Object#blank_criteria?`` method is removed (was previously deprecated.)
7778

7879

7980
``touch`` method now clears changed state

lib/mongoid/association/referenced/has_many/enumerable.rb

+37-1
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,48 @@ def set_base(document)
478478
end
479479

480480
def unloaded_documents
481-
if _unloaded.selector._mongoid_unsatisfiable_criteria?
481+
if unsatisfiable_criteria?(_unloaded.selector)
482482
[]
483483
else
484484
_unloaded
485485
end
486486
end
487+
488+
# Checks whether conditions in the given hash are known to be
489+
# unsatisfiable, i.e. querying with this hash will always return no
490+
# documents.
491+
#
492+
# This method only handles condition shapes that Mongoid itself uses when
493+
# it builds association queries. Return value true indicates the condition
494+
# always produces an empty document set. Note however that return value false
495+
# is not a guarantee that the condition won't produce an empty document set.
496+
#
497+
# @example Unsatisfiable conditions
498+
# unsatisfiable_criteria?({'_id' => {'$in' => []}})
499+
# # => true
500+
#
501+
# @example Conditions which may be satisfiable
502+
# unsatisfiable_criteria?({'_id' => '123'})
503+
# # => false
504+
#
505+
# @example Conditions which are unsatisfiable that this method does not handle
506+
# unsatisfiable_criteria?({'foo' => {'$in' => []}})
507+
# # => false
508+
#
509+
# @param [ Hash ] selector The conditions to check.
510+
#
511+
# @return [ true | false ] Whether hash contains known unsatisfiable
512+
# conditions.
513+
def unsatisfiable_criteria?(selector)
514+
unsatisfiable_criteria = { '_id' => { '$in' => [] } }
515+
return true if selector == unsatisfiable_criteria
516+
return false unless selector.length == 1 && selector.keys == %w[$and]
517+
518+
value = selector.values.first
519+
value.is_a?(Array) && value.any? do |sub_value|
520+
sub_value.is_a?(Hash) && unsatisfiable_criteria?(sub_value)
521+
end
522+
end
487523
end
488524
end
489525
end

lib/mongoid/extensions/array.rb

-22
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,6 @@ def __mongoize_time__
5555
::Time.configured.local(*self)
5656
end
5757

58-
# Checks whether conditions given in this array are known to be
59-
# unsatisfiable, i.e., querying with this array will always return no
60-
# documents.
61-
#
62-
# This method used to assume that the array is the list of criteria
63-
# to be used with an $and operator. This assumption is no longer made;
64-
# therefore, since the interpretation of conditions in the array differs
65-
# between $and, $or and $nor operators, this method now always returns
66-
# false.
67-
#
68-
# This method is deprecated. Mongoid now uses
69-
# +_mongoid_unsatisfiable_criteria?+ internally; this method is retained
70-
# for backwards compatibility only. It always returns false.
71-
#
72-
# @return [ false ] Always false.
73-
# @deprecated
74-
def blank_criteria?
75-
false
76-
end
77-
7858
# Is the array a set of multiple arguments in a method?
7959
#
8060
# @example Is this multi args?
@@ -175,5 +155,3 @@ def resizable?
175155

176156
::Array.__send__(:include, Mongoid::Extensions::Array)
177157
::Array.extend(Mongoid::Extensions::Array::ClassMethods)
178-
179-
::Mongoid.deprecate(Array, :blank_criteria)

lib/mongoid/extensions/hash.rb

-50
Original file line numberDiff line numberDiff line change
@@ -47,54 +47,6 @@ def __consolidate__(klass)
4747
end
4848
Mongoid.deprecate(self, :__consolidate__)
4949

50-
# Checks whether conditions given in this hash are known to be
51-
# unsatisfiable, i.e., querying with this hash will always return no
52-
# documents.
53-
#
54-
# This method only handles condition shapes that Mongoid itself uses when
55-
# it builds association queries. It does not guarantee that a false
56-
# return value means the condition can produce a non-empty document set -
57-
# only that if the return value is true, the condition always produces
58-
# an empty document set.
59-
#
60-
# @example Unsatisfiable conditions
61-
# {'_id' => {'$in' => []}}._mongoid_unsatisfiable_criteria?
62-
# # => true
63-
#
64-
# @example Conditions which could be satisfiable
65-
# {'_id' => '123'}._mongoid_unsatisfiable_criteria?
66-
# # => false
67-
#
68-
# @example Conditions which are unsatisfiable that this method does not handle
69-
# {'foo' => {'$in' => []}}._mongoid_unsatisfiable_criteria?
70-
# # => false
71-
#
72-
# @return [ true | false ] Whether hash contains known unsatisfiable
73-
# conditions.
74-
# @api private
75-
def _mongoid_unsatisfiable_criteria?
76-
unsatisfiable_criteria = { "_id" => { "$in" => [] }}
77-
return true if self == unsatisfiable_criteria
78-
return false unless length == 1 && keys == %w($and)
79-
value = values.first
80-
value.is_a?(Array) && value.any? do |sub_v|
81-
sub_v.is_a?(Hash) && sub_v._mongoid_unsatisfiable_criteria?
82-
end
83-
end
84-
85-
# Checks whether conditions given in this hash are known to be
86-
# unsatisfiable, i.e., querying with this hash will always return no
87-
# documents.
88-
#
89-
# This method is deprecated. Mongoid now uses
90-
# +_mongoid_unsatisfiable_criteria?+ internally; this method is retained
91-
# for backwards compatibility only.
92-
#
93-
# @return [ true | false ] Whether hash contains known unsatisfiable
94-
# conditions.
95-
# @deprecated
96-
alias :blank_criteria? :_mongoid_unsatisfiable_criteria?
97-
9850
# Deletes an id value from the hash.
9951
#
10052
# @example Delete an id value.
@@ -196,5 +148,3 @@ def resizable?
196148

197149
::Hash.__send__(:include, Mongoid::Extensions::Hash)
198150
::Hash.extend(Mongoid::Extensions::Hash::ClassMethods)
199-
200-
::Mongoid.deprecate(Hash, :blank_criteria)

lib/mongoid/extensions/object.rb

-15
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,6 @@ def __to_inc__
8484
end
8585
Mongoid.deprecate(self, :__to_inc__)
8686

87-
# Checks whether conditions given in this object are known to be
88-
# unsatisfiable, i.e., querying with this object will always return no
89-
# documents.
90-
#
91-
# This method is deprecated. Mongoid now uses
92-
# +_mongoid_unsatisfiable_criteria?+ internally; this method is retained
93-
# for backwards compatibility only. It always returns false.
94-
#
95-
# @return [ false ] Always false.
96-
# @deprecated
97-
def blank_criteria?
98-
false
99-
end
100-
Mongoid.deprecate(self, :blank_criteria?)
101-
10287
# Do or do not, there is no try. -- Yoda.
10388
#
10489
# @example Do or do not.

spec/mongoid/extensions/array_spec.rb

-28
Original file line numberDiff line numberDiff line change
@@ -378,34 +378,6 @@
378378
end
379379
end
380380

381-
describe "#blank_criteria?" do
382-
383-
context "when the array has an empty _id criteria" do
384-
385-
context "when only the id criteria is in the array" do
386-
387-
let(:array) do
388-
[{ "_id" => { "$in" => [] }}]
389-
end
390-
391-
it "is false" do
392-
expect(array.blank_criteria?).to be false
393-
end
394-
end
395-
396-
context "when the id criteria is in the array with others" do
397-
398-
let(:array) do
399-
[{ "_id" => "test" }, { "_id" => { "$in" => [] }}]
400-
end
401-
402-
it "is false" do
403-
expect(array.blank_criteria?).to be false
404-
end
405-
end
406-
end
407-
end
408-
409381
describe "#delete_one" do
410382

411383
context "when the object doesn't exist" do

spec/mongoid/extensions/hash_spec.rb

-103
Original file line numberDiff line numberDiff line change
@@ -294,107 +294,4 @@
294294
expect(Hash).to be_resizable
295295
end
296296
end
297-
298-
shared_examples_for 'unsatisfiable criteria method' do
299-
300-
context "when the hash has only an empty _id criteria" do
301-
302-
let(:hash) do
303-
{ "_id" => { "$in" => [] }}
304-
end
305-
306-
it "is true" do
307-
expect(hash.send(meth)).to be true
308-
end
309-
end
310-
311-
context "when the hash has an empty _id criteria and another criteria" do
312-
313-
let(:hash) do
314-
{ "_id" => { "$in" => [] }, 'foo' => 'bar'}
315-
end
316-
317-
it "is false" do
318-
expect(hash.send(meth)).to be false
319-
end
320-
end
321-
322-
context "when the hash has an empty _id criteria via $and" do
323-
324-
let(:hash) do
325-
{'$and' => [{ "_id" => { "$in" => [] }}]}
326-
end
327-
328-
it "is true" do
329-
expect(hash.send(meth)).to be true
330-
end
331-
end
332-
333-
context "when the hash has an empty _id criteria via $and and another criteria at top level" do
334-
335-
let(:hash) do
336-
{'$and' => [{ "_id" => { "$in" => [] }}], 'foo' => 'bar'}
337-
end
338-
339-
it "is false" do
340-
expect(hash.send(meth)).to be false
341-
end
342-
end
343-
344-
context "when the hash has an empty _id criteria via $and and another criteria in $and" do
345-
346-
let(:hash) do
347-
{'$and' => [{ "_id" => { "$in" => [] }}, {'foo' => 'bar'}]}
348-
end
349-
350-
it "is true" do
351-
expect(hash.send(meth)).to be true
352-
end
353-
end
354-
355-
context "when the hash has an empty _id criteria via $and and another criteria in $and value" do
356-
357-
let(:hash) do
358-
{'$and' => [{ "_id" => { "$in" => [] }, 'foo' => 'bar'}]}
359-
end
360-
361-
it "is false" do
362-
expect(hash.send(meth)).to be false
363-
end
364-
end
365-
366-
context "when the hash has an empty _id criteria via $or" do
367-
368-
let(:hash) do
369-
{'$or' => [{ "_id" => { "$in" => [] }}]}
370-
end
371-
372-
it "is false" do
373-
expect(hash.send(meth)).to be false
374-
end
375-
end
376-
377-
context "when the hash has an empty _id criteria via $nor" do
378-
379-
let(:hash) do
380-
{'$nor' => [{ "_id" => { "$in" => [] }}]}
381-
end
382-
383-
it "is false" do
384-
expect(hash.send(meth)).to be false
385-
end
386-
end
387-
end
388-
389-
describe "#blank_criteria?" do
390-
let(:meth) { :blank_criteria? }
391-
392-
it_behaves_like 'unsatisfiable criteria method'
393-
end
394-
395-
describe "#_mongoid_unsatisfiable_criteria?" do
396-
let(:meth) { :_mongoid_unsatisfiable_criteria? }
397-
398-
it_behaves_like 'unsatisfiable criteria method'
399-
end
400297
end

spec/mongoid/extensions/object_spec.rb

-7
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,4 @@
265265
expect(object.numeric?).to eq(false)
266266
end
267267
end
268-
269-
describe "#blank_criteria?" do
270-
271-
it "is false" do
272-
expect(object.blank_criteria?).to be false
273-
end
274-
end
275268
end

0 commit comments

Comments
 (0)