Sha256: 9cf1290f8fdce9f4d09e26b31989d546991ca3c73dea3777f6279517c600f696

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

module Mongoid
  module Matcher

    # @api private
    module Expression
      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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongoid-7.2.1 lib/mongoid/matcher/expression.rb
mongoid-7.2.0 lib/mongoid/matcher/expression.rb
mongoid-7.2.0.rc1 lib/mongoid/matcher/expression.rb