Class: Mongory::Matchers::ExistsMatcher
- Inherits:
-
AbstractMatcher
- Object
- AbstractMatcher
- Mongory::Matchers::ExistsMatcher
- Defined in:
- lib/mongory/matchers/exists_matcher.rb
Overview
ExistsMatcher implements the $exists
operator, which checks whether a key exists.
It transforms the presence (or absence) of a field into a boolean value,
then compares it to the condition using the ==
operator.
This matcher ensures the condition is strictly a boolean (true
or false
).
Constant Summary
Constants inherited from AbstractMatcher
AbstractMatcher::KEY_NOT_FOUND
Instance Attribute Summary
Attributes inherited from AbstractMatcher
Instance Method Summary collapse
-
#check_validity! ⇒ void
Ensures that the condition value is a valid boolean.
- #priority ⇒ Object
-
#raw_proc ⇒ Proc
Creates a raw Proc that performs the existence check.
Methods inherited from AbstractMatcher
#cached_proc, #debug_proc, define_matcher, #initialize, #match, #match?, #render_tree, #uniq_key
Methods included from Utils
included, included_classes, #is_blank?, #is_present?
Constructor Details
This class inherits a constructor from Mongory::Matchers::AbstractMatcher
Instance Method Details
#check_validity! ⇒ void
This method returns an undefined value.
Ensures that the condition value is a valid boolean.
44 45 46 47 48 |
# File 'lib/mongory/matchers/exists_matcher.rb', line 44 def check_validity! return if [true, false].include?(@condition) raise TypeError, "$exists needs a boolean, but got #{@condition.inspect}" end |
#priority ⇒ Object
36 37 38 |
# File 'lib/mongory/matchers/exists_matcher.rb', line 36 def priority 2 end |
#raw_proc ⇒ Proc
Creates a raw Proc that performs the existence check. The Proc checks if the record exists and compares it to the condition.
26 27 28 29 30 31 32 33 34 |
# File 'lib/mongory/matchers/exists_matcher.rb', line 26 def raw_proc condition = @condition Proc.new do |record| # Check if the record is nil or KEY_NOT_FOUND # and compare it to the condition. (record != KEY_NOT_FOUND) == condition end end |