Class: Mongoid::Matchable::ElemMatch

Inherits:
Default
  • Object
show all
Defined in:
build/mongoid-7.0/lib/mongoid/matchable/elem_match.rb

Overview

Since:

  • 4.0.0

Instance Attribute Summary

Attributes inherited from Default

#attribute, #document

Instance Method Summary collapse

Methods inherited from Default

#initialize

Constructor Details

This class inherits a constructor from Mongoid::Matchable::Default

Instance Method Details

#_matches?(value) ⇒ true, false

Return true if a given predicate matches a sub document entirely

Examples:

Do the values match?

matcher._matches?({"$elemMatch" => {"a" => 1, "b" => 2}})

Parameters:

  • value (Hash)

    The values to check.

Returns:

  • (true, false)

    If the values match.

Since:

  • 4.0.0



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'build/mongoid-7.0/lib/mongoid/matchable/elem_match.rb', line 15

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