Class: Parse::Constraint::ArrayNotEmptyOrNilConstraint

Inherits:
Parse::Constraint show all
Defined in:
lib/parse/query/constraints.rb

Overview

Note:

Uses index-friendly operations where possible.

Array not empty constraint - matches arrays that have at least one element. This is the opposite of empty_or_nil: it matches only non-empty arrays.

q.where :tags.not_empty => true   # matches non-empty arrays only
q.where :tags.not_empty => false  # matches [] or nil/missing

Instance Attribute Summary

Attributes inherited from Parse::Constraint

#operand, #operation, #operator, #value

Instance Method Summary collapse

Methods inherited from Parse::Constraint

#as_json, constraint_keyword, create, formatted_value, #formatted_value, #initialize, #key, #precedence, register, #to_s

Constructor Details

This class inherits a constructor from Parse::Constraint

Instance Method Details

#buildHash

Returns the compiled constraint using aggregation pipeline.

Returns:

  • (Hash)

    the compiled constraint using aggregation pipeline.



770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
# File 'lib/parse/query/constraints.rb', line 770

def build
  value = formatted_value
  unless value == true || value == false
    raise ArgumentError, "#{self.class}: Value must be true or false"
  end

  # Use formatted field name for proper Parse field mapping
  field_name = Parse::Query.format_field(@operation.operand)

  if value
    # Match non-empty arrays (must exist, not nil, and not empty)
    # Use $and to combine multiple conditions without duplicate keys
    pipeline = [
      {
        "$match" => {
          "$and" => [
            { field_name => { "$exists" => true } },
            { field_name => { "$ne" => nil } },
            { field_name => { "$ne" => [] } },
          ],
        },
      },
    ]
  else
    # Match empty array OR nil/missing field
    # Use explicit $eq for empty array check (more reliable through Parse Server)
    pipeline = [
      {
        "$match" => {
          "$or" => [
            { field_name => { "$exists" => true, "$eq" => [] } },
            { field_name => { "$exists" => false } },
            { field_name => { "$eq" => nil } },
          ],
        },
      },
    ]
  end

  { "__aggregation_pipeline" => pipeline }
end

#not_emptyArrayNotEmptyOrNilConstraint

A registered method on a symbol to create the constraint.

Examples:

q.where :field.not_empty => true

Returns:



767
# File 'lib/parse/query/constraints.rb', line 767

register :not_empty