Class: Parse::Constraint::TimeRangeConstraint

Inherits:
Parse::Constraint 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 Attribute Summary

Attributes inherited from Parse::Constraint

#operand, #operation, #operator, #value

Instance Method Summary collapse

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_datesTimeRangeConstraint

A registered method on a symbol to create the constraint.

Examples:

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

Returns:



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

register :between_dates

#buildHash

Returns the compiled constraint.

Returns:

  • (Hash)

    the compiled constraint.



2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
# File 'lib/parse/query/constraints.rb', line 2411

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