模块:Mongoid::Matcher::EqImpl Private

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

Overview

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

该模块由 $eq 和其他需要执行 $eq 所执行的匹配的操作符使用(例如,$ne 对 $eq 的结果求反)。 与 $eq 不同,该模块将原始操作符作为matches?的附加参数,以提供反映首次调用的操作符的正确异常消息。

类方法摘要折叠

类方法详细信息

匹配? (exists, value,condition,original_operator) ⇒ true | false ,布尔值

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

返回一个值是否满足 $eq(或类似)表达式。

参数:

  • 存在 ( true | false )

    未使用。

  • ( Object )

    要检查的值。

  • 条件 (对象 | 范围)

    相等条件谓词。

  • 原始操作符 ( string )

    在异常消息中使用的操作符。

返回:

  • ( true | false )

    值是否匹配。

  • 布尔值


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

module_function def 匹配?(存在, , 条件, 原始操作符)
  案例 条件
  when 范围
    # 由于 $ne 会调用 $eq,因此异常消息需要处理
    # 两个操作符。
    提高 Errors::InvalidQuery, "不支持将范围作为 ' #{ original_operator } '的参数"
=begin
    if value.is_a?(Array)
      value.any? { |elt| condition.include?(elt) }
    else
      condition.include?(value)
    end
=end
  else
    # 与 Time 对象进行比较时,使用毫秒精度进行比较
    if .Kind_of?(时间) && 条件.Kind_of?(时间)
      time_eq?(, 条件)
    elsif .is_a?(阵列) && 条件.Kind_of?(时间)
      .map do |v|
        if v.Kind_of?(时间)
          time_rounded_to_millis(v)
        else
          v
        end
      end.包括?(time_rounded_to_millis(条件))
    else
       == 条件 ||
      .is_a?(阵列) && .包括?(条件)
    end
  end
end

time_eq? (time_a, time_b) ⇒ true | false ,布尔值

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

根据www.mongodb.com/zh-cn/docs/Ruby-driver/current/tutorials/bson-v 4 /#time-instances , > BSON (和MongoDB)中的时间只能具有毫秒精度。 当Ruby Time 实例序列化为BSON或扩展JSON时,时间将精确到毫秒。

> 由于此下限,强烈建议应用程序使用整数数学来执行所有时间计算,因为浮点计算的不精确性可能会产生意外的结果。

因此,请执行与 bson-Ruby gem 类似的操作。

参数:

  • time_a 时间

    第一个时间值。

  • time_b 时间

    第二个时间值。

返回:

  • ( true | false )

    两个时间是否等于毫秒。

  • 布尔值


70
71
72
# File 'lib/mongoid/matcher/eq_impl.rb', line 70

module_function def time_eq?(time_a, time_b)
  time_rounded_to_millis(time_a) == time_rounded_to_millis(time_b)
end

time_rounded_to_millis (time) ⇒ true | false

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

将时间值四舍五入到最接近的毫秒。

参数:

  • 时间 时间

    时间值。

返回:

  • ( true | false )

    时间四舍五入到毫秒。



79
80
81
# File 'lib/mongoid/matcher/eq_impl.rb', line 79

module_function def time_rounded_to_millis(时间)
  return 时间._bson_to_i * 1000 + 时间.usec.divmod(1000).first
end