Sha256: d95b46fb623de85df862399ec62f3ca8daa0c2d296153e418a52660462935b97

Contents?: true

Size: 1.03 KB

Versions: 13

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

module Mongoid
  module Matchable

    class ElemMatch < Default

      # Return true if a given predicate matches a sub document entirely
      #
      # @example Do the values match?
      #   matcher._matches?({"$elemMatch" => {"a" => 1, "b" => 2}})
      #
      # @param [ Hash ] value The values to check.
      #
      # @return [ true, false ] If the values match.
      def _matches?(value)
        elem_match = value["$elemMatch"] || value[:$elemMatch]

        if !@attribute.is_a?(Array) || !value.kind_of?(Hash) || !elem_match.kind_of?(Hash)
          return false
        end

        return @attribute.any? do |sub_document|
          elem_match.all? do |k, v|
            if v.try(:first).try(:[],0) == "$not".freeze || v.try(:first).try(:[],0) == :$not
              !Matchable.matcher(sub_document, k, v.first[1])._matches?(v.first[1])
            else
              Matchable.matcher(sub_document, k, v)._matches?(v)
            end
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
mongoid-7.1.11 lib/mongoid/matchable/elem_match.rb
mongoid-7.1.10 lib/mongoid/matchable/elem_match.rb
mongoid-7.1.9 lib/mongoid/matchable/elem_match.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/lib/mongoid/matchable/elem_match.rb
mongoid-7.1.8 lib/mongoid/matchable/elem_match.rb
mongoid-7.1.7 lib/mongoid/matchable/elem_match.rb
mongoid-7.1.6 lib/mongoid/matchable/elem_match.rb
mongoid-7.1.5 lib/mongoid/matchable/elem_match.rb
mongoid-7.1.4 lib/mongoid/matchable/elem_match.rb
mongoid-7.1.2 lib/mongoid/matchable/elem_match.rb
mongoid-7.1.1 lib/mongoid/matchable/elem_match.rb
mongoid-7.1.0 lib/mongoid/matchable/elem_match.rb
mongoid-7.1.0.rc0 lib/mongoid/matchable/elem_match.rb