Module: Mongoid::Matcher::Type Private

Defined in:
lib/mongoid/matcher/type.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 $type 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 a $type expression.

Parameters:

  • exists (true | false)

    Whether the value exists.

  • value (Object)

    The value to check.

  • condition (Integer | Array<Integer>)

    The $type condition predicate which corresponds to the BSON type enumeration.

Returns:

  • (true | false)

    Whether the value matches.

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mongoid/matcher/type.rb', line 19

module_function def matches?(exists, value, condition)
  conditions = case condition
               when Array
                 condition
               when Integer
                 [ condition ]
               else
                 raise Errors::InvalidQuery, "Unknown $type argument: #{condition}"
               end
  conditions.each do |condition|
    return true if one_matches?(exists, value, condition)
  end
  false
end

.one_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 a single $type expression value.

Parameters:

  • exists (true | false)

    Whether the value exists.

  • value (Object)

    The value to check.

  • condition (Integer)

    The $type condition predicate which corresponds to the BSON type enumeration.

Returns:

  • (true | false)

    Whether the value matches.

  • (Boolean)


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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mongoid/matcher/type.rb', line 45

module_function def one_matches?(exists, value, condition)
  case condition
  when 1
    # Double
    value.is_a?(Float)
  when 2
    # String
    value.is_a?(String)
  when 3
    # Object
    value.is_a?(Hash)
  when 4
    # Array
    value.is_a?(Array)
  when 5
    # Binary data
    value.is_a?(BSON::Binary)
  when 6
    # Undefined
    value.is_a?(BSON::Undefined)
  when 7
    # ObjectId
    value.is_a?(BSON::ObjectId)
  when 8
    # Boolean
    value.is_a?(TrueClass) || value.is_a?(FalseClass)
  when 9
    # Date
    value.is_a?(Date) || value.is_a?(Time) || value.is_a?(DateTime)
  when 10
    # Null
    exists && value.is_a?(NilClass)
  when 11
    # Regex
    value.is_a?(Regexp::Raw) || value.is_a?(::Regexp)
  when 12
    # DBPointer deprecated
    value.is_a?(BSON::DbPointer)
  when 13
    # JavaScript
    value.is_a?(BSON::Code)
  when 14
    # Symbol deprecated
    value.is_a?(Symbol) || value.is_a?(BSON::Symbol::Raw)
  when 15
    # Javascript with code deprecated
    value.is_a?(BSON::CodeWithScope)
  when 16
    # 32-bit int
    value.is_a?(BSON::Int32) || (value.is_a?(Integer) && (-2**32..(2**32) - 1).include?(value))
  when 17
    # Timestamp
    value.is_a?(BSON::Timestamp)
  when 18
    # Long
    value.is_a?(BSON::Int64) ||
      (value.is_a?(Integer) &&
        (-2**64..(2**64) - 1).include?(value) &&
        !(-2**32..(2**32) - 1).include?(value))
  when 19
    # Decimal
    value.is_a?(BSON::Decimal128)
  when -1
    # minKey
    value.is_a?(BSON::MinKey)
  when 127
    # maxKey
    value.is_a?(BSON::MaxKey)
  else
    raise Errors::InvalidQuery, "Unknown $type argument: #{condition}"
  end
end