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)


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

module_function def matches?(exists, value, condition)
  condition = case condition
  when Regexp
    condition
  when BSON::Regexp::Raw
    condition.compile
  else
    # 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

  case value
  when Array
    value.any? do |v|
      v =~ condition
    end
  when String
    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)


54
55
56
57
58
59
60
61
62
# File 'lib/mongoid/matcher/regex.rb', line 54

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