Module: Mongoid::Matcher::EqImpl Private
- Defined in:
- 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
-
.matches?(_exists, value, condition, original_operator) ⇒ true | false, Boolean
private
Returns whether a value satisfies an $eq (or similar) expression.
-
.time_eq?(time_a, time_b) ⇒ true | false, Boolean
private
Per https://www.mongodb.com/docs/ruby-driver/upcoming/data-formats/bson/#time-instances, > Times in BSON (and MongoDB) can only have millisecond precision.
-
.time_rounded_to_millis(time) ⇒ true | false
private
Rounds a time value to nearest millisecond.
Class Method Details
.matches?(_exists, value, condition, original_operator) ⇒ 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 $eq (or similar) expression.
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 |
# File 'lib/mongoid/matcher/eq_impl.rb', line 21 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}'" # if value.is_a?(Array) # value.any? { |elt| condition.include?(elt) } # else # condition.include?(value) # end else # When doing a comparison with Time objects, compare using millisecond precision if value.is_a?(Time) && condition.is_a?(Time) time_eq?(value, condition) elsif value.is_a?(Array) && condition.is_a?(Time) value.map do |v| if v.is_a?(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 end end |
.time_eq?(time_a, time_b) ⇒ 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.
Per https://www.mongodb.com/docs/ruby-driver/upcoming/data-formats/bson/#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.
65 66 67 |
# File 'lib/mongoid/matcher/eq_impl.rb', line 65 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
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.
Rounds a time value to nearest millisecond.
74 75 76 |
# File 'lib/mongoid/matcher/eq_impl.rb', line 74 module_function def time_rounded_to_millis(time) (time._bson_to_i * 1000) + time.usec.divmod(1000).first end |