Module: Mongoid::Matcher::Expression Private

Defined in:
build/mongoid-7.3/lib/mongoid/matcher/expression.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Class Method Summary collapse

Class Method Details

.matches?(document, expr) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



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
# File 'build/mongoid-7.3/lib/mongoid/matcher/expression.rb', line 6

module_function def matches?(document, expr)
  if expr.nil?
    raise Errors::InvalidQuery, "Nil condition in expression context"
  end
  unless Hash === expr
    raise Errors::InvalidQuery, "MQL query must be provided as a Hash"
  end
  expr.all? do |k, expr_v|
    k = k.to_s
    if k == "$comment"
      # Nothing
      return true
    end
    if k.start_with?('$')
      ExpressionOperator.get(k).matches?(document, expr_v)
    else
      values = Matcher.extract_attribute(document, k)
      if values.length > 0
        values.any? do |v|
          FieldExpression.matches?(true, v, expr_v)
        end
      else
        FieldExpression.matches?(false, nil, expr_v)
      end
    end
  end
end