Class: Parse::Constraint::TimeRangeConstraint
- Inherits:
-
Constraint
- Object
- Constraint
- Parse::Constraint::TimeRangeConstraint
- Defined in:
- lib/parse/query/constraints.rb
Overview
A convenience constraint that combines greater-than-or-equal and less-than-or-equal constraints for date/time range queries. This is equivalent to using both $gte and $lte.
# Find events between two dates Event.where(:created_at.between_dates => [start_date, end_date]) # Generates: "created_at": { "$gte": start_date, "$lte": end_date }
Instance Method Summary collapse
-
#between_dates ⇒ TimeRangeConstraint
A registered method on a symbol to create the constraint.
-
#build ⇒ Hash
The compiled constraint.
Instance Method Details
#between_dates ⇒ TimeRangeConstraint
A registered method on a symbol to create the constraint.
2481 |
# File 'lib/parse/query/constraints.rb', line 2481 register :between_dates |
#build ⇒ Hash
Returns the compiled constraint.
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 |
# File 'lib/parse/query/constraints.rb', line 2484 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 [start_date, end_date]" end start_date, end_date = value # Format the dates using Parse's date formatting formatted_start = Parse::Constraint.formatted_value(start_date) formatted_end = Parse::Constraint.formatted_value(end_date) { @operation.operand => { Parse::Constraint::GreaterThanOrEqualConstraint.key => formatted_start, Parse::Constraint::LessThanOrEqualConstraint.key => formatted_end, } } end |