Class: BSON::Regex
- Inherits:
-
Object
- Object
- BSON::Regex
- Defined in:
- lib/bson/types/regex.rb
Overview
generates a wrapped Regexp with lazy compilation. can represent flags not supported in Ruby’s core Regexp class before compilation.
Constant Summary collapse
- IGNORECASE =
0x01
- LOCALE_DEPENDENT =
0x02
- MULTILINE =
0x04
- DOTALL =
0x08
- UNICODE =
0x10
- EXTENDED =
0x20
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#pattern ⇒ Object
(also: #source)
Returns the value of attribute pattern.
Class Method Summary collapse
-
.from_native(regexp) ⇒ BSON::Regex
Attempt to convert a native Ruby Regexp to a BSON::Regex.
Instance Method Summary collapse
-
#eql?(regexp) ⇒ Boolean
(also: #==)
Check equality of this wrapped Regexp with another.
-
#initialize(pattern, *opts) ⇒ Regex
constructor
Create a new regexp.
-
#initialize_copy ⇒ Object
Clone or dup the current BSON::Regex.
-
#inspect ⇒ Object
Get a human-readable representation of this BSON Regex.
-
#try_compile ⇒ Regexp
Compile the BSON::Regex.
Constructor Details
#initialize(pattern, *opts) ⇒ Regex
Create a new regexp.
36 37 38 39 |
# File 'lib/bson/types/regex.rb', line 36 def initialize(pattern, *opts) @pattern = pattern @options = opts.first.is_a?(Fixnum) ? opts.first : str_opts_to_int(opts.join) end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
30 31 32 |
# File 'lib/bson/types/regex.rb', line 30 def @options end |
#pattern ⇒ Object Also known as: source
Returns the value of attribute pattern.
28 29 30 |
# File 'lib/bson/types/regex.rb', line 28 def pattern @pattern end |
Class Method Details
.from_native(regexp) ⇒ BSON::Regex
Warning: Ruby regular expressions use a different syntax and different set of flags than BSON regular expressions. A regular expression matches different strings when executed in Ruby than it matches when used in a MongoDB query, if it can be used in a query at all.
Attempt to convert a native Ruby Regexp to a BSON::Regex.
75 76 77 78 79 80 81 82 83 |
# File 'lib/bson/types/regex.rb', line 75 def self.from_native(regexp) pattern = regexp.source opts = 0 opts |= MULTILINE # multiline mode is always on for Ruby regular expressions opts |= IGNORECASE if (Regexp::IGNORECASE & regexp. != 0) opts |= DOTALL if (Regexp::MULTILINE & regexp. != 0) opts |= EXTENDED if (Regexp::EXTENDED & regexp. != 0) self.new(pattern, opts) end |
Instance Method Details
#eql?(regexp) ⇒ Boolean Also known as: ==
Check equality of this wrapped Regexp with another.
44 45 46 47 48 |
# File 'lib/bson/types/regex.rb', line 44 def eql?(regexp) regexp.kind_of?(BSON::Regex) && self.pattern == regexp.pattern && self. == regexp. end |
#initialize_copy ⇒ Object
Clone or dup the current BSON::Regex.
58 59 60 61 62 63 |
# File 'lib/bson/types/regex.rb', line 58 def initialize_copy a_copy = self.dup a_copy.pattern = self.pattern.dup a_copy. = self..dup a_copy end |
#inspect ⇒ Object
Get a human-readable representation of this BSON Regex.
52 53 54 55 |
# File 'lib/bson/types/regex.rb', line 52 def inspect "#<BSON::Regex:0x#{self.object_id} " << "@pattern=#{@pattern}>, @options=#{@options}>" end |
#try_compile ⇒ Regexp
Warning: regular expressions retrieved from the server may include a pattern that cannot be compiled into a Ruby regular expression, or which matches a different set of strings in Ruby than it does when used in a MongoDB query, or it may have flags that are not supported by Ruby regular expressions.
Compile the BSON::Regex.
93 94 95 96 97 98 99 |
# File 'lib/bson/types/regex.rb', line 93 def try_compile regexp_opts = 0 regexp_opts |= Regexp::IGNORECASE if ( & IGNORECASE != 0) regexp_opts |= Regexp::MULTILINE if ( & DOTALL != 0) regexp_opts |= Regexp::EXTENDED if ( & EXTENDED != 0) Regexp.new(pattern, regexp_opts) end |