Module: Mongoid::Matcher::Not Private

Defined in:
lib/mongoid/matcher/not.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.

In-memory matcher for $not expression.

Class Method Summary collapse

Class Method Details

.matches?(exists, value, condition) ⇒ 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 value satisfies an $not expression.

Parameters:

  • Whether the value exists.

  • The value to check.

  • The $not condition predicate.

Returns:

  • Whether the value matches.

API:

  • private

  • private



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mongoid/matcher/not.rb', line 22

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