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.
2410 |
# File 'lib/parse/query/constraints.rb', line 2410 register :between_dates |
#build ⇒ Hash
Returns the compiled constraint.
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 |
# File 'lib/parse/query/constraints.rb', line 2413 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 |