class BSON::Regexp::Raw

@see jira.mongodb.org/browse/RUBY-698<br><br>Represents the raw values for the regular expression.

def ==(other)

Returns:
  • (true, false) - If the objects are equal.

Parameters:
  • other (Object) -- The object to check against.

Other tags:
    Example: Check if the raw bson regexp is equal to the other. -
def ==(other)
  return false unless other.is_a?(::Regexp::Raw)
  pattern == other.pattern && options == other.options
end

def as_extended_json(**opts)

Returns:
  • (Hash) - The extended json representation.

Options Hash: (**opts)
  • :mode (nil | :relaxed | :legacy) -- Serialization mode
def as_extended_json(**opts)
  if opts[:mode] == :legacy
    { '$regex' => source, '$options' => options }
  else
    { '$regularExpression' => { 'pattern' => source, 'options' => options } }
  end
end

def as_json(*)

Returns:
  • (Hash) - The raw regexp as a JSON hash.

Other tags:
    Example: Get the raw regexp as a JSON hash. -
def as_json(*)
  as_extended_json(mode: :legacy)
end

def compile

Returns:
  • (::Regexp) - The compiled regular expression.

Other tags:
    Example: Compile the regular expression. -
def compile
  @compile ||= ::Regexp.new(pattern, options_to_int)
end

def initialize(pattern, options = '')

Parameters:
  • options (String | Symbol) -- The options.
  • pattern (String) -- The regular expression pattern.

Other tags:
    Example: Initialize the raw regexp. -
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

def method_missing(method, *arguments)

def method_missing(method, *arguments)
  return super unless respond_to?(method)
  compile.send(method, *arguments)
end

def options_to_int

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

def respond_to_missing?(method, include_private = false)

Parameters:
  • method (String) -- The name of a method.
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

def to_bson(buffer = ByteBuffer.new)

Other tags:
    See: http://bsonspec.org/#/specification -

Returns:
  • (BSON::ByteBuffer) - The buffer with the encoded object.

Parameters:
  • buffer (BSON::ByteBuffer) -- The byte buffer to append to.

Other tags:
    Note: - From the BSON spec: The first cstring is the regex pattern,

Other tags:
    Example: Get the raw regular expression as encoded BSON. -
def to_bson(buffer = ByteBuffer.new)
  buffer.put_cstring(source)
  buffer.put_cstring(options.chars.sort.join)
end