Module: Mongoid::Matcher::Expression Private

Defined in:
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
33
34
35
36
37
# File '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.start_with?('$')
      ExpressionOperator.get(k).matches?(document, expr_v)
    else
      exists, value, expanded = Matcher.extract_attribute(document, k)
      # The value may have been expanded into an array, but then
      # array may have been shrunk back to a scalar (or hash) when
      # path contained a numeric position.
      # Do not treat a hash as an array here (both are iterable).
      if expanded && Array === value
        if value == []
          # Empty array is technically equivalent to exists: false.
          FieldExpression.matches?(false, nil, expr_v)
        else
          value.any? do |v|
            FieldExpression.matches?(true, v, expr_v)
          end
        end
      else
        FieldExpression.matches?(exists, value, expr_v)
      end
    end
  end
end