class BSON::Regexp::Raw

Represents the raw values for the regular expression.

@see jira.mongodb.org/browse/RUBY-698

Attributes

options[R]

@return [ String ] options The options.

pattern[R]

@return [ String ] pattern The regex pattern.

Public Class Methods

new(pattern, options = '') click to toggle source

Initialize the new raw regular expression.

@example Initialize the raw regexp.

Raw.new(pattern, options)

@param [ String ] pattern The regular expression pattern. @param [ String | Symbol ] options The options.

# File lib/bson/regexp.rb, line 143
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
  else
    raise ArgumentError, 'Regexp options must be a String or Symbol'
  end

  @pattern = pattern
  @options = options.to_s
end

Public Instance Methods

==(other) click to toggle source

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.

# File lib/bson/regexp.rb, line 225
def ==(other)
  return false unless other.is_a?(::Regexp::Raw)

  pattern == other.pattern && options == other.options
end
Also aliased as: eql?
as_extended_json(**opts) click to toggle source

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 209
def as_extended_json(**opts)
  if opts[:mode] == :legacy
    { '$regex' => source, '$options' => options }
  else
    { '$regularExpression' => { 'pattern' => source, 'options' => options } }
  end
end
as_json(*) click to toggle source

Get the raw BSON regexp as JSON hash data.

@example Get the raw regexp as a JSON hash.

raw_regexp.as_json

@return [ Hash ] The raw regexp as a JSON hash.

# File lib/bson/regexp.rb, line 198
def as_json(*)
  as_extended_json(mode: :legacy)
end
compile() click to toggle source

Compile the Regular expression into the native type.

@example Compile the regular expression.

raw.compile

@return [ ::Regexp ] The compiled regular expression.

# File lib/bson/regexp.rb, line 132
def compile
  @compile ||= ::Regexp.new(pattern, options_to_int)
end
eql?(other)
Alias for: ==
respond_to_missing?(method, include_private = false) click to toggle source

Allow automatic delegation of methods to the Regexp object returned by compile.

@param [ String] method The name of a method.

# File lib/bson/regexp.rb, line 162
def respond_to_missing?(method, include_private = false)
  # YAML calls #respond_to? during deserialization, before the object
  # is initialized.
  defined?(@pattern) && compile.respond_to?(method, include_private)
end
to_bson(buffer = ByteBuffer.new) click to toggle source

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.

@return [ BSON::ByteBuffer ] The buffer with the encoded object.

@see bsonspec.org/#/specification

# File lib/bson/regexp.rb, line 187
def to_bson(buffer = ByteBuffer.new)
  buffer.put_cstring(source)
  buffer.put_cstring(options.chars.sort.join)
end

Private Instance Methods

method_missing(method, *arguments) click to toggle source
Calls superclass method
# File lib/bson/regexp.rb, line 234
def method_missing(method, *arguments)
  return super unless respond_to?(method)

  compile.send(method, *arguments)
end
options_to_int() click to toggle source
# File lib/bson/regexp.rb, line 240
def options_to_int
  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