Class: Parse::Constraint::TimeRangeConstraint

Inherits:
Constraint
  • Object
show all
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

Instance Method Details

#between_datesTimeRangeConstraint

A registered method on a symbol to create the constraint.

Examples:

q.where :field.between_dates => [start_date, end_date]

Returns:



2410
# File 'lib/parse/query/constraints.rb', line 2410

register :between_dates

#buildHash

Returns the compiled constraint.

Returns:

  • (Hash)

    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