Class: BSON::Regexp::Raw
Overview
Represents the raw values for the regular expression.
Instance Attribute Summary collapse
-
#options ⇒ String
readonly
Options The options.
-
#pattern ⇒ String
readonly
Pattern The regex pattern.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
(also: #eql?)
Check equality of the raw bson regexp against another.
-
#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).
-
#as_json ⇒ Hash
Get the raw BSON regexp as JSON hash data.
-
#compile ⇒ ::Regexp
Compile the Regular expression into the native type.
-
#initialize(pattern, options = '') ⇒ Raw
constructor
Initialize the new raw regular expression.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Allow automatic delegation of methods to the Regexp object returned by
compile
. -
#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer
Encode the Raw Regexp object to BSON.
Methods included from JSON
Constructor Details
#initialize(pattern, options = '') ⇒ Raw
Initialize the new raw regular expression.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/bson/regexp.rb', line 143 def initialize(pattern, = '') if pattern.include?(NULL_BYTE) raise Error::InvalidRegexpPattern, "Regexp pattern cannot contain a null byte: #{pattern}" elsif .is_a?(String) || .is_a?(Symbol) if .to_s.include?(NULL_BYTE) raise Error::InvalidRegexpPattern, "Regexp options cannot contain a null byte: #{}" end else raise ArgumentError, 'Regexp options must be a String or Symbol' end @pattern = pattern @options = .to_s end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *arguments) ⇒ Object (private)
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
#options ⇒ String (readonly)
Returns options The options.
124 125 126 |
# File 'lib/bson/regexp.rb', line 124 def @options end |
#pattern ⇒ String (readonly)
Returns pattern The regex pattern.
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.
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 && == other. 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).
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' => } else { '$regularExpression' => { 'pattern' => source, 'options' => } } end end |
#as_json ⇒ Hash
Get the raw BSON regexp as JSON hash data.
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.
132 133 134 |
# File 'lib/bson/regexp.rb', line 132 def compile @compile ||= ::Regexp.new(pattern, ) end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Allow automatic delegation of methods to the Regexp object returned by compile
.
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
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.
187 188 189 190 |
# File 'lib/bson/regexp.rb', line 187 def to_bson(buffer = ByteBuffer.new) buffer.put_cstring(source) buffer.put_cstring(.chars.sort.join) end |