Class: Mongory::Matchers::PresentMatcher
- Inherits:
-
AbstractMatcher
- Object
- AbstractMatcher
- Mongory::Matchers::PresentMatcher
- Defined in:
- lib/mongory/matchers/present_matcher.rb
Overview
PresentMatcher implements the $present
operator.
It returns true if the record value is considered "present" (i.e., not nil, not empty, not KEY_NOT_FOUND), and matches the expected boolean condition.
This is similar to $exists
, but evaluates truthiness
of the value instead of mere existence.
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 boolean.
- #priority ⇒ Object
-
#raw_proc ⇒ Proc
Creates a raw Proc that performs the presence 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 boolean.
46 47 48 49 50 |
# File 'lib/mongory/matchers/present_matcher.rb', line 46 def check_validity! return if [true, false].include?(@condition) raise TypeError, '$present needs a boolean' end |
#priority ⇒ Object
38 39 40 |
# File 'lib/mongory/matchers/present_matcher.rb', line 38 def priority 2 end |
#raw_proc ⇒ Proc
Creates a raw Proc that performs the presence check. The Proc checks if the record's presence matches the condition.
29 30 31 32 33 34 35 36 |
# File 'lib/mongory/matchers/present_matcher.rb', line 29 def raw_proc condition = @condition Proc.new do |record| record = nil if record == KEY_NOT_FOUND is_present?(record) == condition end end |