Class: Parse::Constraint::EndsWithConstraint

Inherits:
Parse::Constraint show all
Defined in:
lib/parse/query/constraints.rb

Overview

Equivalent to using the $regex Parse query operation with a suffix pattern. This is useful for matching fields that end with a specific string.

# Find files whose name ends with ".pdf" File.where(:name.ends_with => ".pdf") # Generates: "name": { "$regex": "\.pdf$", "$options": "i" }

# Opt into Unicode-aware case-insensitive matching (Parse Server 8.3.0+ # over REST, MongoDB 6.1+ mongo-direct): Post.where(:title.ends_with => { value: "café", unicode: true }) # Generates: "title": { "$regex": "café$", "$options": "iu" }

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, #regex_unicode_option, register, #to_s

Constructor Details

This class inherits a constructor from Parse::Constraint

Instance Method Details

#buildHash

Returns the compiled constraint.

Returns:

  • (Hash)

    the compiled constraint.



2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
# File 'lib/parse/query/constraints.rb', line 2448

def build
  raw, unicode = regex_unicode_option(@value)
  value = self.class.formatted_value(raw)
  unless value.is_a?(String)
    raise ArgumentError, "#{self.class}: Value must be a string for ends_with constraint"
  end

  # Validate length to prevent performance issues
  if value.length > Parse::RegexSecurity::MAX_PATTERN_LENGTH
    raise ArgumentError, "#{self.class}: Value too long (#{value.length} chars, max #{Parse::RegexSecurity::MAX_PATTERN_LENGTH})"
  end

  # Escape special regex characters in the suffix
  escaped_value = Regexp.escape(value)
  regex_pattern = "#{escaped_value}$"

  { @operation.operand => { :$regex => regex_pattern, :$options => (unicode ? "iu" : "i") } }
end

#ends_withEndsWithConstraint

A registered method on a symbol to create the constraint. Maps to Parse operator "$regex".

Examples:

q.where :field.ends_with => "suffix"

Returns:



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

constraint_keyword :$regex