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 157 def initialize(pattern, options = '') if pattern.include?(NULL_BYTE) raise Error::InvalidRegexpPattern, "Regexp pattern cannot contain a null byte: #{pattern}" elsif options.is_a?(String) || options.is_a?(Symbol) if options.to_s.include?(NULL_BYTE) raise Error::InvalidRegexpPattern, "Regexp options cannot contain a null byte: #{options}" end elsif !options.is_a?(Integer) raise ArgumentError, "Regexp options must be a String, Symbol, or Integer" end @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 253 def ==(other) return false unless other.is_a?(::Regexp::Raw) pattern == other.pattern && options == other.options end
Converts this object to a representation directly serializable to Extended JSON
(github.com/mongodb/specifications/blob/master/source/extended-json.rst).
@option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
(default is canonical extended JSON)
@return [ Hash
] The extended json representation.
# File lib/bson/regexp.rb, line 235 def as_extended_json(**opts) if opts[:mode] == :legacy { "$regex" => source, "$options" => options } else {"$regularExpression" => {'pattern' => source, "options" => options}} end 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 140 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 178 def respond_to?(method, include_private = false) if defined?(@pattern) compile.respond_to?(method, include_private) || super else # YAML calls #respond_to? during deserialization, before the object # is initialized. super end 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 210 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 262 def method_missing(method, *arguments) return super unless respond_to?(method) compile.send(method, *arguments) end
# File lib/bson/regexp.rb, line 267 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