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.



799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
# File 'lib/parse/query/constraints.rb', line 799

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:



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

register :not_empty