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

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 64
def as_json(*args)
  { "$regex" => source, "$options" => bson_options }
end
to_bson(buffer = ByteBuffer.new) 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.

@return [ String ] The encoded string.

@see bsonspec.org/#/specification

@since 2.0.0

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

Private Instance Methods

bson_extended() click to toggle source
# File lib/bson/regexp.rb, line 98
def bson_extended
  (options & ::Regexp::EXTENDED != 0) ? EXTENDED_VALUE : NO_VALUE
end
bson_ignorecase() click to toggle source
# File lib/bson/regexp.rb, line 102
def bson_ignorecase
  (options & ::Regexp::IGNORECASE != 0) ? IGNORECASE_VALUE : NO_VALUE
end
bson_multiline() click to toggle source
# File lib/bson/regexp.rb, line 106
def bson_multiline
  (options & ::Regexp::MULTILINE != 0) ? RUBY_MULTILINE_VALUE : NO_VALUE
end
bson_options() click to toggle source
# File lib/bson/regexp.rb, line 94
def bson_options
  bson_ignorecase + bson_multiline + bson_extended
end