Class: Parse::Constraint::BetweenConstraint
- Inherits:
-
Parse::Constraint
- Object
- Parse::Constraint
- Parse::Constraint::BetweenConstraint
- Defined in:
- lib/parse/query/constraints.rb
Overview
A general range constraint that combines greater-than-or-equal and less-than-or-equal constraints for numeric, date/time, and string range queries. This is equivalent to using both $gte and $lte. This constraint works with numbers, dates, times, strings (alphabetical), and any comparable values.
# Find products with price between 10 and 50
Product.where(:price.between => [10, 50])
# Generates: "price": { "$gte": 10, "$lte": 50 }
# Find events between two dates
Event.where(:created_at.between => [start_date, end_date])
# Generates: "created_at": { "$gte": start_date, "$lte": end_date }
# Find users with age between 18 and 65
User.where(:age.between => [18, 65])
# Generates: "age": { "$gte": 18, "$lte": 65 }
# Find users with names alphabetically between "Alice" and "John"
User.where(:name.between => ["Alice", "John"])
# Generates: "name": { "$gte": "Alice", "$lte": "John" }
Instance Attribute Summary
Attributes inherited from Parse::Constraint
#operand, #operation, #operator, #value
Instance Method Summary collapse
-
#between ⇒ BetweenConstraint
A registered method on a symbol to create the constraint.
-
#build ⇒ Hash
The compiled 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
#between ⇒ BetweenConstraint
A registered method on a symbol to create the constraint.
2456 |
# File 'lib/parse/query/constraints.rb', line 2456 register :between |
#build ⇒ Hash
Returns the compiled constraint.
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 |
# File 'lib/parse/query/constraints.rb', line 2459 def build value = formatted_value unless value.is_a?(Array) && value.length == 2 raise ArgumentError, "#{self.class}: Value must be an array with exactly 2 elements [min_value, max_value]" end min_value, max_value = value # Format the values using Parse's formatting (handles dates, numbers, etc.) formatted_min = Parse::Constraint.formatted_value(min_value) formatted_max = Parse::Constraint.formatted_value(max_value) { @operation.operand => { Parse::Constraint::GreaterThanOrEqualConstraint.key => formatted_min, Parse::Constraint::LessThanOrEqualConstraint.key => formatted_max, } } end |