module BSON::Regexp

Injects behaviour for encoding and decoding regular expression values to and from raw bytes as specified by the BSON spec.

@see bsonspec.org/#/specification

@since 2.0.0

Constants

BSON_TYPE

A regular expression is type 0x0B in the BSON spec.

@since 2.0.0

EXTENDED_VALUE

Extended value constant.

@since 3.2.6

IGNORECASE_VALUE

Ignore case constant.

@since 3.2.6

MULTILINE_VALUE

Multiline constant.

@since 3.2.6

NEWLINE_VALUE

Newline constant.

@since 3.2.6

RUBY_MULTILINE_VALUE

Ruby multiline constant.

@since 3.2.6

@deprecated Will be removed in 5.0

Public Instance Methods

as_json(*args) click to toggle source

Get the regexp as JSON hash data.

@example Get the regexp as a JSON hash.

regexp.as_json

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

@since 2.0.0

# File lib/bson/regexp.rb, line 66
def as_json(*args)
  { "$regex" => source, "$options" => bson_options }
end
to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) click to toggle source

Get the regular expression as encoded BSON.

@example Get the regular expression as encoded BSON.

%r{\d+}.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 2.0.0

# File lib/bson/regexp.rb, line 92
def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
  buffer.put_cstring(source)
  buffer.put_cstring(bson_options)
end

Private Instance Methods

bson_dotall() click to toggle source
# File lib/bson/regexp.rb, line 112
def bson_dotall
  # Ruby Regexp's MULTILINE is equivalent to BSON's dotall value
  (options & ::Regexp::MULTILINE != 0) ? NEWLINE_VALUE : NO_VALUE
end
bson_extended() click to toggle source
# File lib/bson/regexp.rb, line 104
def bson_extended
  (options & ::Regexp::EXTENDED != 0) ? EXTENDED_VALUE : NO_VALUE
end
bson_ignorecase() click to toggle source
# File lib/bson/regexp.rb, line 108
def bson_ignorecase
  (options & ::Regexp::IGNORECASE != 0) ? IGNORECASE_VALUE : NO_VALUE
end
bson_options() click to toggle source
# File lib/bson/regexp.rb, line 99
def bson_options
  # Ruby's Regexp always has BSON's equivalent of 'm' on, so always add it
  bson_ignorecase + MULTILINE_VALUE + bson_dotall + bson_extended
end