Class: Mongory::Matchers::PresentMatcher

Inherits:
AbstractMatcher show all
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.

Examples:

matcher = PresentMatcher.build(true)
matcher.match?('hello')     #=> true
matcher.match?(nil)         #=> false
matcher.match?([])          #=> false

matcher = PresentMatcher.build(false)
matcher.match?(nil)         #=> true

See Also:

Constant Summary

Constants inherited from AbstractMatcher

AbstractMatcher::KEY_NOT_FOUND

Instance Attribute Summary

Attributes inherited from AbstractMatcher

#condition, #context

Instance Method Summary collapse

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.

Raises:

  • (TypeError)

    if condition is not true or false



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

#priorityObject



38
39
40
# File 'lib/mongory/matchers/present_matcher.rb', line 38

def priority
  2
end

#raw_procProc

Creates a raw Proc that performs the presence check. The Proc checks if the record's presence matches the condition.

Returns:

  • (Proc)

    A proc that performs presence check



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