Module: BSON::Regexp

Includes:
JSON
Defined in:
lib/bson/regexp.rb

Overview

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

See Also:

Since:

  • 2.0.0

Defined Under Namespace

Modules: ClassMethods Classes: Raw

Constant Summary collapse

BSON_TYPE =

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

Since:

  • 2.0.0

11.chr.force_encoding(BINARY).freeze
EXTENDED_VALUE =

Extended value constant.

Since:

  • 3.2.6

'x'.freeze
IGNORECASE_VALUE =

Ignore case constant.

Since:

  • 3.2.6

'i'.freeze
MULTILINE_VALUE =

Multiline constant.

Since:

  • 3.2.6

'm'.freeze
NEWLINE_VALUE =

Newline constant.

Since:

  • 3.2.6

's'.freeze
RUBY_MULTILINE_VALUE =
Deprecated.

Will be removed in 5.0

Ruby multiline constant.

Since:

  • 3.2.6

'ms'.freeze

Instance Method Summary collapse

Methods included from JSON

#to_json

Instance Method Details

#as_json(*args) ⇒ Hash

Get the regexp as JSON hash data.

Examples:

Get the regexp as a JSON hash.

regexp.as_json

Returns:

  • (Hash)

    The regexp as a JSON hash.

Since:

  • 2.0.0



66
67
68
# 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?) ⇒ BSON::ByteBuffer

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.

Get the regular expression as encoded BSON.

Examples:

Get the regular expression as encoded BSON.

%r{\d+}.to_bson

Parameters:

  • buffer (BSON::ByteBuffer) (defaults to: ByteBuffer.new)

    The byte buffer to append to.

  • validating_keys (true, false) (defaults to: Config.validating_keys?)

Returns:

  • (BSON::ByteBuffer)

    The buffer with the encoded object.

See Also:

Since:

  • 2.0.0



92
93
94
95
# 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