Class: Parse::Constraint::StartsWithConstraint
- Inherits:
-
Parse::Constraint
- Object
- Parse::Constraint
- Parse::Constraint::StartsWithConstraint
- Defined in:
- lib/parse/query/constraints.rb
Overview
Equivalent to using the $regex Parse query operation with a prefix pattern.
This is useful for autocomplete functionality and prefix matching.
# Find users whose name starts with "John" User.where(:name.starts_with => "John") # Generates: "name": { "$regex": "^John", "$options": "i" }
# Opt into Unicode-aware case-insensitive matching (Parse Server 8.3.0+ # over REST, MongoDB 6.1+ mongo-direct): User.where(:name.starts_with => { value: "café", unicode: true }) # Generates: "name": { "$regex": "^café", "$options": "iu" }
Instance Attribute Summary
Attributes inherited from Parse::Constraint
#operand, #operation, #operator, #value
Instance Method Summary collapse
-
#build ⇒ Hash
The compiled constraint.
-
#starts_with ⇒ StartsWithConstraint
A registered method on a symbol to create the constraint.
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
#build ⇒ Hash
Returns the compiled constraint.
2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 |
# File 'lib/parse/query/constraints.rb', line 2364 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 starts_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 prefix escaped_value = Regexp.escape(value) regex_pattern = "^#{escaped_value}" { @operation.operand => { :$regex => regex_pattern, :$options => (unicode ? "iu" : "i") } } end |
#starts_with ⇒ StartsWithConstraint
A registered method on a symbol to create the constraint. Maps to Parse operator "$regex".
2360 |
# File 'lib/parse/query/constraints.rb', line 2360 constraint_keyword :$regex |