Class: Parse::Constraint::StartsWithConstraint
- Inherits:
-
Constraint
- Object
- 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" }
Instance Method Summary collapse
-
#build ⇒ Hash
The compiled constraint.
-
#starts_with ⇒ StartsWithConstraint
A registered method on a symbol to create the constraint.
Instance Method Details
#build ⇒ Hash
Returns the compiled constraint.
2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 |
# File 'lib/parse/query/constraints.rb', line 2306 def build value = formatted_value 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 => "i" } } end |
#starts_with ⇒ StartsWithConstraint
A registered method on a symbol to create the constraint. Maps to Parse operator "$regex".
2302 |
# File 'lib/parse/query/constraints.rb', line 2302 constraint_keyword :$regex |