Module: Mongoid::Matcher::EqImpl Private

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

This module is used by $eq and other operators that need to perform the matching that $eq performs (for example, $ne which negates the result of $eq). Unlike $eq this module takes an original operator as an additional argument to matches? to provide the correct exception messages reflecting the operator that was first invoked.

Class Method Summary collapse

Class Method Details

.matches?(exists, value, condition, original_operator) ⇒ 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:



12
13
14
15
16
17
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
# File 'build/mongoid-8.1/lib/mongoid/matcher/eq_impl.rb', line 12

module_function def matches?(exists, value, condition, original_operator)
  case condition
  when Range
    # Since $ne invokes $eq, the exception message needs to handle
    # both operators.
    raise Errors::InvalidQuery, "Range is not supported as an argument to '#{original_operator}'"
=begin
    if value.is_a?(Array)
      value.any? { |elt| condition.include?(elt) }
    else
      condition.include?(value)
    end
=end
  else
    # When doing a comparison with Time objects, compare using millisecond precision
    if Mongoid.compare_time_by_ms
      if value.kind_of?(Time) && condition.kind_of?(Time)
        time_eq?(value, condition)
      elsif value.is_a?(Array) && condition.kind_of?(Time)
        value.map do |v|
          if v.kind_of?(Time)
            time_rounded_to_millis(v)
          else
            v
          end
        end.include?(time_rounded_to_millis(condition))
      else
        value == condition ||
        value.is_a?(Array) && value.include?(condition)
      end
    else
      value == condition ||
      value.is_a?(Array) && value.include?(condition)
    end
  end
end

.time_eq?(time_a, time_b) ⇒ 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.

Per www.mongodb.com/docs/ruby-driver/current/tutorials/bson-v4/#time-instances, > Times in BSON (and MongoDB) can only have millisecond precision. When Ruby Time instances are serialized to BSON or Extended JSON, the times are floored to the nearest millisecond.

> Because of this flooring, applications are strongly recommended to perform all time calculations using integer math, as inexactness of floating point calculations may produce unexpected results.

As such, perform a similar operation to what the bson-ruby gem does

Returns:



58
59
60
# File 'build/mongoid-8.1/lib/mongoid/matcher/eq_impl.rb', line 58

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) ⇒ Object

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.



62
63
64
# File 'build/mongoid-8.1/lib/mongoid/matcher/eq_impl.rb', line 62

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