Class: BSON::Regexp::Raw

Inherits:
Object
  • Object
show all
Includes:
JSON
Defined in:
lib/bson/regexp.rb

Overview

Represents the raw values for the regular expression.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from JSON

#to_json

Constructor Details

#initialize(pattern, options = '') ⇒ Raw

Initialize the new raw regular expression.

Examples:

Initialize the raw regexp.

Raw.new(pattern, options)

Parameters:

  • pattern (String)

    The regular expression pattern.

  • options (String | Symbol) (defaults to: '')

    The options.

Since:

  • 2.0.0



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments) ⇒ Object (private)

Since:

  • 2.0.0



234
235
236
237
238
# File 'lib/bson/regexp.rb', line 234

def method_missing(method, *arguments)
  return super unless respond_to?(method)

  compile.send(method, *arguments)
end

Instance Attribute Details

#optionsString (readonly)

Returns options The options.

Returns:

  • (String)

    options The options.

Since:

  • 2.0.0



124
125
126
# File 'lib/bson/regexp.rb', line 124

def options
  @options
end

#patternString (readonly)

Returns pattern The regex pattern.

Returns:

  • (String)

    pattern The regex pattern.

Since:

  • 2.0.0



121
122
123
# File 'lib/bson/regexp.rb', line 121

def pattern
  @pattern
end

Instance Method Details

#==(other) ⇒ true, false Also known as: eql?

Check equality of the raw bson regexp against another.

Examples:

Check if the raw bson regexp is equal to the other.

raw_regexp == other

Parameters:

  • other (Object)

    The object to check against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.0.0



225
226
227
228
229
# File 'lib/bson/regexp.rb', line 225

def ==(other)
  return false unless other.is_a?(::Regexp::Raw)

  pattern == other.pattern && options == other.options
end

#as_extended_json(**opts) ⇒ Hash

Converts this object to a representation directly serializable to Extended JSON (github.com/mongodb/specifications/blob/master/source/extended-json.rst).

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :mode (nil | :relaxed | :legacy)

    Serialization mode (default is canonical extended JSON)

Returns:

  • (Hash)

    The extended json representation.

Since:

  • 2.0.0



209
210
211
212
213
214
215
# 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_jsonHash

Get the raw BSON regexp as JSON hash data.

Examples:

Get the raw regexp as a JSON hash.

raw_regexp.as_json

Returns:

  • (Hash)

    The raw regexp as a JSON hash.

Since:

  • 2.0.0



198
199
200
# File 'lib/bson/regexp.rb', line 198

def as_json(*)
  as_extended_json(mode: :legacy)
end

#compile::Regexp

Compile the Regular expression into the native type.

Examples:

Compile the regular expression.

raw.compile

Returns:

  • (::Regexp)

    The compiled regular expression.

Since:

  • 2.0.0



132
133
134
# File 'lib/bson/regexp.rb', line 132

def compile
  @compile ||= ::Regexp.new(pattern, options_to_int)
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

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

Parameters:

  • method (String)

    The name of a method.

Returns:

Since:

  • 2.0.0



162
163
164
165
166
# 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) ⇒ 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.

Encode the Raw Regexp object to BSON.

Examples:

Get the raw regular expression as encoded BSON.

raw_regexp.to_bson

Parameters:

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

    The byte buffer to append to.

Returns:

See Also:

Since:

  • 2.0.0



187
188
189
190
# 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