Sha256: 33a19a6b2c1f75af4a4ef6d951ebe339ea91ebcfaf06a3ee2f11310d6b9a6ccf

Contents?: true

Size: 1 KB

Versions: 13

Compression:

Stored size: 1 KB

Contents

# 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 & 1 rubygems

Version Path
mongoid-7.0.13 lib/mongoid/matchable/elem_match.rb
mongoid-7.0.12 lib/mongoid/matchable/elem_match.rb
mongoid-7.0.11 lib/mongoid/matchable/elem_match.rb
mongoid-7.0.10 lib/mongoid/matchable/elem_match.rb
mongoid-7.0.8 lib/mongoid/matchable/elem_match.rb
mongoid-7.0.7 lib/mongoid/matchable/elem_match.rb
mongoid-7.0.6 lib/mongoid/matchable/elem_match.rb
mongoid-7.0.5 lib/mongoid/matchable/elem_match.rb
mongoid-7.0.4 lib/mongoid/matchable/elem_match.rb
mongoid-7.0.3 lib/mongoid/matchable/elem_match.rb
mongoid-7.0.2 lib/mongoid/matchable/elem_match.rb
mongoid-7.0.1 lib/mongoid/matchable/elem_match.rb
mongoid-7.0.0 lib/mongoid/matchable/elem_match.rb