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)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mongoid/matcher/type.rb', line 22

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|
    if one_matches?(exists, value, condition)
      return true
    end
  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)


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
117
118
119
120
121
# File 'lib/mongoid/matcher/type.rb', line 50

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