Class: Mongoid::Matchable::Default
- Inherits:
-
Object
- Object
- Mongoid::Matchable::Default
- Defined in:
- lib/mongoid/matchable/default.rb
Overview
Contains all the default behavior for checking for matching documents given MongoDB expressions.
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
-
#_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).
-
#initialize(attribute, document = nil) ⇒ Default
constructor
Creating a new matcher only requires the value.
Constructor Details
#initialize(attribute, document = nil) ⇒ Default
Creating a new matcher only requires the value.
19 20 21 |
# File 'lib/mongoid/matchable/default.rb', line 19 def initialize(attribute, document = nil) @attribute, @document = attribute, document end |
Instance Attribute Details
#attribute ⇒ Object
9 10 11 |
# File 'lib/mongoid/matchable/default.rb', line 9 def attribute @attribute end |
#document ⇒ Object
9 10 11 |
# File '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).
53 54 55 56 57 58 59 |
# File '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 |