Class: Mongoid::Matchable::Default

Inherits:
Object
  • Object
show all
Defined in:
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, Exists, Gt, Gte, In, Lt, Lte, Ne, Nin, Or, 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 '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 'lib/mongoid/matchable/default.rb', line 9

def attribute
  @attribute
end

#documentObject

Since:

  • 4.0.0



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

def document
  @document
end

Instance Method Details

#matches?(value) ⇒ true, false

Return true if the attribute and value are equal, or if it is an array if the value is included.

Examples:

Does this value match?

default.matches?("value")

Parameters:

  • value (Object)

    The value to check if it matches.

Returns:

  • (true, false)

    True if matches, false if not.

Since:

  • 1.0.0



34
35
36
# File 'lib/mongoid/matchable/default.rb', line 34

def matches?(value)
  attribute.is_a?(Array) && !value.is_a?(Array) ? attribute.any? { |_attribute| value === _attribute } : value === attribute
end