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

Note:

The ability to specify options as an Integer is deprecated. Please specify options as a String. The ability to pass options as as Integer will be removed in version 5.0.0.

Initialize the new raw regular expression.

Examples:

Initialize the raw regexp.

Raw.new(pattern, options)

Parameters:

  • pattern (String)

    The regular expression pattern.

  • options (String, Integer) (defaults to: '')

    The options.

Since:

  • 3.0.0



156
157
158
159
# File 'lib/bson/regexp.rb', line 156

def initialize(pattern, options = '')
  @pattern = pattern
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Since:

  • 3.0.0



230
231
232
233
# File 'lib/bson/regexp.rb', line 230

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

Instance Attribute Details

#optionsInteger (readonly)

Returns options The options.

Returns:

  • (Integer)

    options The options.

Since:

  • 3.0.0



129
130
131
# File 'lib/bson/regexp.rb', line 129

def options
  @options
end

#patternString (readonly)

Returns pattern The regex pattern.

Returns:

  • (String)

    pattern The regex pattern.

Since:

  • 3.0.0



126
127
128
# File 'lib/bson/regexp.rb', line 126

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:

  • 4.2.0



221
222
223
224
225
# File 'lib/bson/regexp.rb', line 221

def ==(other)
  return false unless other.is_a?(::Regexp::Raw)
  pattern == other.pattern &&
    options == other.options
end

#as_json(*args) ⇒ Hash

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:

  • 4.2.0



207
208
209
# File 'lib/bson/regexp.rb', line 207

def as_json(*args)
  { "$regex" => source, "$options" => options }
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:

  • 3.0.0



139
140
141
# File 'lib/bson/regexp.rb', line 139

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

#respond_to?(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:

  • 3.1.0



167
168
169
# File 'lib/bson/regexp.rb', line 167

def respond_to?(method, include_private = false)
  compile.respond_to?(method, include_private) || super
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.

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.

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

Returns:

See Also:

Since:

  • 4.2.0



193
194
195
196
197
# File 'lib/bson/regexp.rb', line 193

def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
  return compile.to_bson(buffer, validating_keys) if options.is_a?(Integer)
  buffer.put_cstring(source)
  buffer.put_cstring(options.chars.sort.join)
end