模块:Mongoid::Matcher::FieldExpression Private

定义于:
lib/mongoid/matcher/field_expression.rb

Overview

该模块是私有 API 的一部分。 您应尽可能避免使用此模块,因为它将来可能会被删除或更改。

Singleton 模块,用于评估内存中的给定值是否与特定字段相关的 MSQL查询表达式相匹配。

类方法摘要折叠

类方法详细信息

。匹配? (exists, value, Condition) ⇒ true | false ,布尔值

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

返回值是否满足条件。

参数:

  • 存在 ( true | false ) —

    值是否存在。

  • 值 ( Object ) —

    要检查的值。

  • 条件 (哈希 | 对象) —

    条件谓词。

返回:

  • ( true | false ) —

    值是否匹配。

  • (布尔值)


18
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mongoid/matcher/field_expression.rb', line 18

module_function def 匹配?(存在, 值, 条件)
  if 条件.is_a?(哈希)
    条件.全部? do |k, cond_v|
      k = k.to_s
      if k.start_with?('$')
        if %w[$regex $options].包括?(k)
          除非 条件.键?(' $regex ')
            提高 Errors::InvalidQuery, " $regex 为必填项,前提是给出了 $options: #{ Errors :: InvalidQuery . truncate_expr ( condition ) } "
          end

          if k == ' $regex '
            if 选项 = 条件['$options']
              cond_v = 案例 cond_v
                       when regexp
                         BSON::regexp::原始.new(cond_v.来源, 选项)
                       when BSON::regexp::原始
                         BSON::regexp::原始.new(cond_v.模式, 选项)
                       else
                         BSON::regexp::原始.new(cond_v, 选项)
                       end
            elsif cond_v.is_a?(字符串)
              cond_v = BSON::regexp::原始.new(cond_v)
            end

            FieldOperator.获取(k).匹配?(存在, 值, cond_v)
          else
            # $options 作为 $regex 的一部分进行匹配
            true
          end
        else
          FieldOperator.获取(k).匹配?(存在, 值, cond_v)
        end
      elsif 值.is_a?(哈希)
        sub_values = 匹配器.extract_attribute(值, k)
        if sub_values.长度 > 0
          sub_values.任何? do |sub_v|
            等式.匹配?(true, sub_v, cond_v)
          end
        else
          等式.匹配?(false, nil, cond_v)
        end
      else
        false
      end
    end
  else
    案例 条件
    when ::regexp, BSON::regexp::原始
      正则表达式(Regex).matches_array_or_scalar?(值, 条件)
    else
      等式.匹配?(存在, 值, 条件)
    end
  end
end