Class: Mongoid::Matchable::Default

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

Overview

Contains all the default behavior for checking for matching documents given MongoDB expressions.

Since:

  • 4.0.0

Direct Known Subclasses

All, And, ElemMatch, Eq, Exists, Gt, Gte, In, Lt, Lte, Ne, Nin, Nor, Or, Regexp, Size

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, document = nil) ⇒ Default

Creating a new matcher only requires the value.

Examples:

Create a new matcher.

Default.new("attribute")

Parameters:

  • attribute (Object)

    The current attribute to check against.

Since:

  • 1.0.0



19
20
21
# File 'build/mongoid-7.0/lib/mongoid/matchable/default.rb', line 19

def initialize(attribute, document = nil)
  @attribute, @document = attribute, document
end

Instance Attribute Details

#attributeObject

Since:

  • 4.0.0



9
10
11
# File 'build/mongoid-7.0/lib/mongoid/matchable/default.rb', line 9

def attribute
  @attribute
end

#documentObject

Since:

  • 4.0.0



9
10
11
# File 'build/mongoid-7.0/lib/mongoid/matchable/default.rb', line 9

def document
  @document
end

Instance Method Details

#_matches?(value) ⇒ true, false

Checks whether the attribute matches the value, using the default MongoDB matching logic (i.e., when no operator is specified in the criteria).

If attribute and value are both of basic types like string or number, this method returns true if and only if the attribute equals the value.

Value can also be of a type like Regexp or Range which defines more complex matching/inclusion behavior via the === operator. If so, and attribute is still of a basic type like string or number, this method returns true if and only if the value’s === operator returns true for the attribute. For example, this method returns true if attribute is a string and value is a Regexp and attribute matches the value, of if attribute is a number and value is a Range and the value includes the attribute.

If attribute is an array and value is not an array, the checks just described (i.e. the === operator invocation) are performed on each item of the attribute array. If any of the items in the attribute match the value according to the value type’s === operator, this method returns true.

If attribute and value are both arrays, this method returns true if and only if the arrays are equal (including the order of the elements).

Parameters:

  • value (Object)

    The value to check.

Returns:

  • (true, false)

    True if attribute matches the value, false if not.

Since:

  • 1.0.0



53
54
55
56
57
58
59
# File 'build/mongoid-7.0/lib/mongoid/matchable/default.rb', line 53

def _matches?(value)
  if attribute.is_a?(Array) && !value.is_a?(Array)
    attribute.any? { |_attribute| value === _attribute }
  else
    value === attribute
  end
end