Class: Parse::Constraint::ArrayNotEmptyConstraint
- Inherits:
-
Constraint
- Object
- Constraint
- Parse::Constraint::ArrayNotEmptyConstraint
- Defined in:
- lib/parse/query/constraints.rb
Overview
Array not-empty constraint - shorthand for size > 0. Matches arrays that have at least one element.
q.where :tags.arr_nempty => true # arrays with 1+ elements q.where :tags.arr_nempty => false # arrays with 0 elements (same as empty)
Instance Method Summary collapse
-
#arr_nempty ⇒ ArrayNotEmptyConstraint
A registered method on a symbol to create the constraint.
-
#build ⇒ Hash
The compiled constraint using aggregation pipeline.
Instance Method Details
#arr_nempty ⇒ ArrayNotEmptyConstraint
A registered method on a symbol to create the constraint.
661 |
# File 'lib/parse/query/constraints.rb', line 661 register :arr_nempty |
#build ⇒ Hash
Returns the compiled constraint using aggregation pipeline.
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 |
# File 'lib/parse/query/constraints.rb', line 664 def build value = formatted_value unless value == true || value == false raise ArgumentError, "#{self.class}: Value must be true or false" end field_name = Parse::Query.format_field(@operation.operand) size_expr = { "$size" => { "$ifNull" => ["$#{field_name}", []] } } # If true, match size > 0; if false, match size == 0 comparison = value ? { "$gt" => [size_expr, 0] } : { "$eq" => [size_expr, 0] } pipeline = [ { "$match" => { "$expr" => comparison, }, }, ] { "__aggregation_pipeline" => pipeline } end |