class BSON::Regexp::Raw
Represents the raw values for the regular expression.
@see jira.mongodb.org/browse/RUBY-698
@since 3.0.0
Attributes
@return [ Integer ] options The options.
@return [ String ] pattern The regex pattern.
Public Class Methods
Initialize the new raw regular expression.
@example Initialize the raw regexp.
Raw.new(pattern, options)
@param [ String ] pattern The regular expression pattern. @param [ String, Integer ] options The options.
@note The ability to specify options as an Integer is deprecated.
Please specify options as a String. The ability to pass options as as Integer will be removed in version 5.0.0.
@since 3.0.0
# File lib/bson/regexp.rb, line 156 def initialize(pattern, options = '') @pattern = pattern @options = options end
Public Instance Methods
Check equality of the raw bson regexp against another.
@example Check if the raw bson regexp is equal to the other.
raw_regexp == other
@param [ Object ] other The object to check against.
@return [ true, false ] If the objects are equal.
@since 4.2.0
# File lib/bson/regexp.rb, line 221 def ==(other) return false unless other.is_a?(::Regexp::Raw) pattern == other.pattern && options == other.options end
Compile the Regular expression into the native type.
@example Compile the regular expression.
raw.compile
@return [ ::Regexp ] The compiled regular expression.
@since 3.0.0
# File lib/bson/regexp.rb, line 139 def compile @compiled ||= ::Regexp.new(pattern, options_to_int) end
Allow automatic delegation of methods to the Regexp object returned by compile
.
@param [ String] method The name of a method.
@since 3.1.0
# File lib/bson/regexp.rb, line 167 def respond_to?(method, include_private = false) compile.respond_to?(method, include_private) || super end
Encode the Raw Regexp object to BSON.
@example Get the raw regular expression as encoded BSON.
raw_regexp.to_bson
@note From the BSON spec: The first cstring is the regex pattern,
the second is the regex options string. Options are identified by characters, which must be stored in alphabetical order. Valid options are 'i' for case insensitive matching, 'm' for multiline matching, 'x' for verbose mode, 'l' to make \w, \W, etc. locale dependent, 's' for dotall mode ('.' matches everything), and 'u' to make \w, \W, etc. match unicode.
@param [ BSON::ByteBuffer ] buffer The byte buffer to append to. @param [ true, false ] validating_keys
@return [ BSON::ByteBuffer ] The buffer with the encoded object.
@see bsonspec.org/#/specification
@since 4.2.0
# File lib/bson/regexp.rb, line 193 def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) return compile.to_bson(buffer, validating_keys) if options.is_a?(Integer) buffer.put_cstring(source) buffer.put_cstring(options.chars.sort.join) end
Private Instance Methods
# File lib/bson/regexp.rb, line 230 def method_missing(method, *arguments) return super unless respond_to?(method) compile.send(method, *arguments) end
# File lib/bson/regexp.rb, line 235 def options_to_int return options if options.is_a?(Integer) opts = 0 opts |= ::Regexp::IGNORECASE if options.include?(IGNORECASE_VALUE) opts |= ::Regexp::MULTILINE if options.include?(NEWLINE_VALUE) opts |= ::Regexp::EXTENDED if options.include?(EXTENDED_VALUE) opts end