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.
1428 |
# File 'lib/parse/query/constraints.rb', line 1428 register :arr_nempty |
#build ⇒ Hash
Returns the compiled constraint using aggregation pipeline.
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 |
# File 'lib/parse/query/constraints.rb', line 1431 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 |