Class: Parse::Constraint::ArrayEmptyOrNilConstraint
- Inherits:
-
Parse::Constraint
- Object
- Parse::Constraint
- Parse::Constraint::ArrayEmptyOrNilConstraint
- Defined in:
- lib/parse/query/constraints.rb
Overview
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.
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 |
# File 'lib/parse/query/constraints.rb', line 737 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.
734 |
# File 'lib/parse/query/constraints.rb', line 734 register :empty_or_nil |