Class: Parse::Constraint::ContainsConstraint

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

Overview

Equivalent to using the $regex Parse query operation with a contains pattern. This is useful for case-insensitive text search within fields.

Find posts whose title contains "parse"

Post.where(:title.contains => "parse")

Generates: "title": { "$regex": ".parse.", "$options": "i" }

Instance Method Summary collapse

Instance Method Details

#buildHash

Returns the compiled constraint.

Returns:

  • (Hash)

    the compiled constraint.



2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
# File 'lib/parse/query/constraints.rb', line 2342

def build
  value = formatted_value
  unless value.is_a?(String)
    raise ArgumentError, "#{self.class}: Value must be a string for contains 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 search text
  escaped_value = Regexp.escape(value)
  regex_pattern = ".*#{escaped_value}.*"

  { @operation.operand => { :$regex => regex_pattern, :$options => "i" } }
end

#containsContainsConstraint

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

Examples:

q.where :field.contains => "text"

Returns:



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

constraint_keyword :$regex