class BSON::Regex
generates a wrapped Regexp with lazy compilation. can represent flags not supported in Ruby's core Regexp class before compilation.
Constants
- DOTALL
- EXTENDED
- IGNORECASE
- LOCALE_DEPENDENT
- MULTILINE
- UNICODE
Attributes
Public Class Methods
Attempt to convert a native Ruby Regexp to a BSON::Regex.
@note Warning: Ruby regular expressions use a different syntax and different
set of flags than BSON regular expressions. A regular expression matches different strings when executed in Ruby than it matches when used in a MongoDB query, if it can be used in a query at all.
@param regexp [Regexp] The native Ruby regexp object to convert to BSON::Regex.
@return [BSON::Regex]
# File lib/bson/types/regex.rb, line 75 def self.from_native(regexp) pattern = regexp.source opts = 0 opts |= MULTILINE # multiline mode is always on for Ruby regular expressions opts |= IGNORECASE if (Regexp::IGNORECASE & regexp.options != 0) opts |= DOTALL if (Regexp::MULTILINE & regexp.options != 0) opts |= EXTENDED if (Regexp::EXTENDED & regexp.options != 0) self.new(pattern, opts) end
Create a new regexp.
@param pattern [String] @param opts [Array, String]
# File lib/bson/types/regex.rb, line 36 def initialize(pattern, *opts) @pattern = pattern @options = opts.first.is_a?(Fixnum) ? opts.first : str_opts_to_int(opts.join) end
Public Instance Methods
Check equality of this wrapped Regexp with another.
@param [BSON::Regex] regexp
# File lib/bson/types/regex.rb, line 44 def eql?(regexp) regexp.kind_of?(BSON::Regex) && self.pattern == regexp.pattern && self.options == regexp.options end
Clone or dup the current BSON::Regex.
# File lib/bson/types/regex.rb, line 58 def initialize_copy a_copy = self.dup a_copy.pattern = self.pattern.dup a_copy.options = self.options.dup a_copy end
Compile the BSON::Regex.
@note Warning: regular expressions retrieved from the server may include a pattern
that cannot be compiled into a Ruby regular expression, or which matches a different set of strings in Ruby than it does when used in a MongoDB query, or it may have flags that are not supported by Ruby regular expressions.
@return [Regexp] A ruby core Regexp object.
# File lib/bson/types/regex.rb, line 93 def try_compile regexp_opts = 0 regexp_opts |= Regexp::IGNORECASE if (options & IGNORECASE != 0) regexp_opts |= Regexp::MULTILINE if (options & DOTALL != 0) regexp_opts |= Regexp::EXTENDED if (options & EXTENDED != 0) Regexp.new(pattern, regexp_opts) end
Private Instance Methods
Convert the string options to an integer.
@return [Fixnum] The Integer representation of the options.
# File lib/bson/types/regex.rb, line 105 def str_opts_to_int(str_opts="") opts = 0 opts |= IGNORECASE if str_opts.include?('i') opts |= LOCALE_DEPENDENT if str_opts.include?('l') opts |= MULTILINE if str_opts.include?('m') opts |= DOTALL if str_opts.include?('s') opts |= UNICODE if str_opts.include?('u') opts |= EXTENDED if str_opts.include?('x') opts end