Class: Parse::Constraint::ArrayEmptyOrNilConstraint
- Inherits:
-
Parse::Constraint
- Object
- Parse::Constraint
- Parse::Constraint::ArrayEmptyOrNilConstraint
- Defined in:
- lib/parse/query/constraints.rb
Overview
Note:
Uses index-friendly operations where possible.
Array empty or nil constraint - matches arrays that are empty OR nil/missing. This is useful for finding records where an array field has no values, whether it’s explicitly empty or was never set.
q.where :tags.empty_or_nil => true # matches [] or nil/missing
q.where :tags.empty_or_nil => false # matches non-empty arrays only
Instance Attribute Summary
Attributes inherited from Parse::Constraint
#operand, #operation, #operator, #value
Instance Method Summary collapse
-
#build ⇒ Hash
The compiled constraint using aggregation pipeline.
-
#empty_or_nil ⇒ ArrayEmptyOrNilConstraint
A registered method on a symbol to create the constraint.
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
#build ⇒ Hash
Returns the compiled constraint using aggregation pipeline.
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 |
# File 'lib/parse/query/constraints.rb', line 708 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 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 } }, ], }, }, ] else # 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" => [] } }, ], }, }, ] end { "__aggregation_pipeline" => pipeline } end |
#empty_or_nil ⇒ ArrayEmptyOrNilConstraint
A registered method on a symbol to create the constraint.
705 |
# File 'lib/parse/query/constraints.rb', line 705 register :empty_or_nil |