Sha256: a3e74b4164a733a34f63c17d635d8c6f0419b6f9533502c3bb7d64e609176f37

Contents?: true

Size: 1.15 KB

Versions: 6

Compression:

Stored size: 1.15 KB

Contents

module Mongoid
  module Matcher

    # @api private
    module FieldOperator
      MAP = {
        '$all' => All,
        '$elemMatch' => ElemMatch,
        '$eq' => Eq,
        '$exists' => Exists,
        '$gt' => Gt,
        '$gte' => Gte,
        '$in' => In,
        '$lt' => Lt,
        '$lte' => Lte,
        '$nin' => Nin,
        '$ne' => Ne,
        '$not' => Not,
        '$regex' => Regex,
        '$size' => Size,
      }.freeze

      module_function def get(op)
        MAP.fetch(op)
      rescue KeyError
        raise Errors::InvalidFieldOperator.new(op)
      end

      module_function def apply_array_field_operator(exists, value, condition)
        if Array === value
          value.any? { |v| yield v }
        else
          yield value
        end
      end

      module_function def apply_comparison_operator(operator, left, right)
        left.send(operator, right)
      rescue ArgumentError, NoMethodError, TypeError
        # We silence bogus comparison attempts, e.g. number to string
        # comparisons.
        # Several different exceptions may be produced depending on the types
        # involved.
        false
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mongoid-7.2.6 lib/mongoid/matcher/field_operator.rb
mongoid-7.2.5 lib/mongoid/matcher/field_operator.rb
mongoid-7.2.4 lib/mongoid/matcher/field_operator.rb
mongoid-7.2.3 lib/mongoid/matcher/field_operator.rb
mongoid-7.2.2 lib/mongoid/matcher/field_operator.rb
mongoid-7.2.1 lib/mongoid/matcher/field_operator.rb