Class: Mongory::Matchers::SizeMatcher

Inherits:
LiteralMatcher show all
Defined in:
lib/mongory/matchers/size_matcher.rb

Overview

Note:

Ruby's Symbol class already defines a #size method, that will return the size of the symbol object. So, this is the only operator that cannot be used with the symbol snippet syntax (e.g. :tags.size).

Use string key syntax instead: :"tags.$size" => ...

Matcher for the $size operator.

This matcher expects the input to be an array, and delegates the comparison to a literal matcher using the array's size as the value.

For example, the condition { tags: { '$size' => 3 } } will match any document where tags is an array of length 3.

Supported compound usages:

Mongory.where(tags: { '$size' => 3 })                       # exactly 3 elements
Mongory.where(tags: { '$size' => { '$gt' => 1 } })          # more than 1
Mongory.where(comments: { '$size' => { '$gt' => 1, '$lte' => 5 } })     # more than 1, up to 5 elements
Mongory.where(tags: { '$size' => { '$in' => [1, 2, 3] } })  # 1, 2, or 3 elements

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 LiteralMatcher

#array_record_matcher, #check_validity!, #dispatched_matcher, #render_tree

Methods inherited from AbstractMatcher

#cached_proc, #check_validity!, #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

#priorityObject

[View source]

47
48
49
# File 'lib/mongory/matchers/size_matcher.rb', line 47

def priority
  2 + super
end

#raw_procProc

Creates a raw Proc that performs the size matching operation.

The returned Proc checks if the input is an Array. If so, it calculates the array's size and passes it to the wrapped literal matcher Proc.

Returns:

  • (Proc)

    A proc that performs size-based matching

[View source]

37
38
39
40
41
42
43
44
45
# File 'lib/mongory/matchers/size_matcher.rb', line 37

def raw_proc
  super_proc = super

  Proc.new do |record|
    next false unless record.is_a?(Array)

    super_proc.call(record.size)
  end
end