Module: Mongoid::Matcher::Expression Private

Defined in:
lib/mongoid/matcher/expression.rb

Overview

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.

Base singleton module used for evaluating whether a given document in-memory matches an MSQL query expression.

Class Method Summary collapse

Class Method Details

.matches?(document, expr) ⇒ true | false, 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 whether a document satisfies a query expression.

Parameters:

Returns:

  • (true | false)

    Whether the document matches.

  • (Boolean)


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

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