/
not.rb
45 lines (41 loc) · 1.46 KB
/
not.rb
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
# rubocop:todo all
module Mongoid
module Matcher
# In-memory matcher for $not expression.
#
# @see https://www.mongodb.com/docs/manual/reference/operator/query/not/
#
# @api private
module Not
# Returns whether a value satisfies an $not expression.
#
# @param [ true | false ] exists Whether the value exists.
# @param [ Object ] value The value to check.
# @param [ Hash | Regexp | BSON::Regexp::Raw ] condition
# The $not condition predicate.
#
# @return [ true | false ] Whether the value matches.
#
# @api private
module_function def matches?(exists, value, condition)
case condition
when ::Regexp, BSON::Regexp::Raw
!Regex.matches?(exists, value, condition)
when Hash
if condition.empty?
raise Errors::InvalidQuery, "$not argument cannot be an empty hash: #{Errors::InvalidQuery.truncate_expr(condition)}"
end
condition.all? do |(k, cond_v)|
k = k.to_s
unless k.start_with?('$')
raise Errors::InvalidQuery, "$not arguments must be operators: #{Errors::InvalidQuery.truncate_expr(k)}"
end
!FieldOperator.get(k).matches?(exists, value, cond_v)
end
else
raise Errors::InvalidQuery, "$not argument must be a Hash or a regular expression: #{Errors::InvalidQuery.truncate_expr(condition)}"
end
end
end
end
end