Module: Mongoid::Matcher::Regex Private

Defined in:
lib/mongoid/matcher/regex.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

In-memory matcher for $regex expression.

Class Method Summary collapse

Class Method Details

.matches?(_exists, value, condition) ⇒ true | false, Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether a value satisfies a $regex expression.

Parameters:

  • exists (true | false)

    Not used.

  • value (String | Array<String>)

    The value to check.

  • condition (Regexp | BSON::Regexp::Raw)

    The $regex condition.

Returns:

  • (true | false)

    Whether the value matches.

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mongoid/matcher/regex.rb', line 18

module_function def matches?(_exists, value, condition)
  unless condition.is_a?(Regexp) || condition.is_a?(BSON::Regexp::Raw)
    # Note that strings must have been converted to a regular expression
    # instance already (with $options taken into account, if provided).
    raise Errors::InvalidQuery, "$regex requires a regular expression argument: #{Errors::InvalidQuery.truncate_expr(condition)}"
  end

  # The condition is compiled by RegexpBudget rather than here, so that
  # the budget's timeout can be baked into the pattern.
  case value
  when Array
    # Object#=~ is gone as of Ruby 3.2, so an element that cannot be
    # matched against has to be rejected rather than passed to =~.
    value.any? do |v|
      v.respond_to?(:=~) && RegexpBudget.match?(v, condition)
    end
  when String
    RegexpBudget.match?(value, condition)
  else
    false
  end
end

.matches_array_or_scalar?(value, condition) ⇒ true | false, Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether an scalar or array value matches a Regexp.

Parameters:

  • exists (true | false)

    Not used.

  • value (String | Array<String>)

    The value to check.

  • condition (Regexp)

    The Regexp condition.

Returns:

  • (true | false)

    Whether the value matches.

  • (Boolean)


50
51
52
53
54
55
56
57
58
# File 'lib/mongoid/matcher/regex.rb', line 50

module_function def matches_array_or_scalar?(value, condition)
  if value.is_a?(Array)
    value.any? do |v|
      matches?(true, v, condition)
    end
  else
    matches?(true, value, condition)
  end
end