bson 1.12.5-java → 2.0.0-java
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of bson might be problematic. Click here for more details.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/CHANGELOG.md +80 -0
- data/CONTRIBUTING.md +42 -0
- data/NOTICE +2 -0
- data/README.md +190 -0
- data/Rakefile +109 -0
- data/lib/bson-ruby.jar +0 -0
- data/lib/bson.rb +60 -87
- data/lib/bson/array.rb +104 -0
- data/lib/bson/binary.rb +193 -0
- data/lib/bson/boolean.rb +48 -0
- data/lib/bson/code.rb +109 -0
- data/lib/bson/code_with_scope.rb +120 -0
- data/lib/bson/document.rb +549 -0
- data/lib/bson/encodable.rb +86 -0
- data/lib/bson/environment.rb +98 -0
- data/lib/bson/false_class.rb +61 -0
- data/lib/bson/float.rb +82 -0
- data/lib/bson/hash.rb +84 -0
- data/lib/bson/int32.rb +59 -0
- data/lib/bson/int64.rb +59 -0
- data/lib/bson/integer.rb +185 -0
- data/lib/bson/json.rb +37 -0
- data/lib/bson/max_key.rb +70 -0
- data/lib/bson/min_key.rb +70 -0
- data/lib/bson/nil_class.rb +70 -0
- data/lib/bson/object_id.rb +395 -0
- data/lib/bson/regexp.rb +124 -0
- data/lib/bson/registry.rb +70 -0
- data/lib/bson/specialized.rb +74 -0
- data/lib/bson/string.rb +203 -0
- data/lib/bson/symbol.rb +87 -0
- data/lib/bson/time.rb +72 -0
- data/lib/bson/timestamp.rb +113 -0
- data/lib/bson/true_class.rb +61 -0
- data/lib/bson/undefined.rb +74 -0
- data/lib/bson/version.rb +17 -0
- data/spec/bson/array_spec.rb +58 -0
- data/spec/bson/binary_spec.rb +115 -0
- data/spec/bson/boolean_spec.rb +48 -0
- data/spec/bson/code_spec.rb +42 -0
- data/spec/bson/code_with_scope_spec.rb +74 -0
- data/spec/bson/document_spec.rb +778 -0
- data/spec/bson/false_class_spec.rb +28 -0
- data/spec/bson/float_spec.rb +29 -0
- data/spec/bson/hash_spec.rb +56 -0
- data/spec/bson/int32_spec.rb +28 -0
- data/spec/bson/int64_spec.rb +28 -0
- data/spec/bson/integer_spec.rb +76 -0
- data/spec/bson/json_spec.rb +53 -0
- data/spec/bson/max_key_spec.rb +75 -0
- data/spec/bson/min_key_spec.rb +75 -0
- data/spec/bson/nil_class_spec.rb +29 -0
- data/spec/bson/object_id_spec.rb +527 -0
- data/spec/bson/regexp_spec.rb +89 -0
- data/spec/bson/registry_spec.rb +55 -0
- data/spec/bson/string_spec.rb +298 -0
- data/spec/bson/symbol_spec.rb +55 -0
- data/spec/bson/time_spec.rb +43 -0
- data/spec/bson/timestamp_spec.rb +74 -0
- data/spec/bson/true_class_spec.rb +28 -0
- data/spec/bson/undefined_spec.rb +29 -0
- data/{lib/bson/types/dbref.rb → spec/bson_spec.rb} +22 -16
- data/spec/spec_helper.rb +32 -0
- data/spec/support/shared_examples.rb +95 -0
- metadata +116 -48
- metadata.gz.sig +1 -1
- data/VERSION +0 -1
- data/bin/b2json +0 -63
- data/bin/j2bson +0 -64
- data/bson.gemspec +0 -34
- data/ext/jbson/lib/java-bson.jar +0 -0
- data/ext/jbson/target/jbson.jar +0 -0
- data/lib/bson/bson_c.rb +0 -37
- data/lib/bson/bson_java.rb +0 -49
- data/lib/bson/bson_ruby.rb +0 -645
- data/lib/bson/byte_buffer.rb +0 -241
- data/lib/bson/exceptions.rb +0 -37
- data/lib/bson/grow.rb +0 -173
- data/lib/bson/ordered_hash.rb +0 -197
- data/lib/bson/support/hash_with_indifferent_access.rb +0 -174
- data/lib/bson/types/binary.rb +0 -52
- data/lib/bson/types/code.rb +0 -55
- data/lib/bson/types/min_max_keys.rb +0 -56
- data/lib/bson/types/object_id.rb +0 -226
- data/lib/bson/types/regex.rb +0 -116
- data/lib/bson/types/timestamp.rb +0 -72
@@ -0,0 +1,395 @@
|
|
1
|
+
# Copyright (C) 2009-2013 MongoDB Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require "digest/md5"
|
16
|
+
require "socket"
|
17
|
+
require "thread"
|
18
|
+
|
19
|
+
module BSON
|
20
|
+
|
21
|
+
# Represents object_id data.
|
22
|
+
#
|
23
|
+
# @see http://bsonspec.org/#/specification
|
24
|
+
#
|
25
|
+
# @since 2.0.0
|
26
|
+
class ObjectId
|
27
|
+
include Comparable
|
28
|
+
include JSON
|
29
|
+
|
30
|
+
# A object_id is type 0x07 in the BSON spec.
|
31
|
+
#
|
32
|
+
# @since 2.0.0
|
33
|
+
BSON_TYPE = 7.chr.force_encoding(BINARY).freeze
|
34
|
+
|
35
|
+
# Check equality of the object id with another object.
|
36
|
+
#
|
37
|
+
# @example Check if the object id is equal to the other.
|
38
|
+
# object_id == other
|
39
|
+
#
|
40
|
+
# @param [ Object ] other The object to check against.
|
41
|
+
#
|
42
|
+
# @return [ true, false ] If the objects are equal.
|
43
|
+
#
|
44
|
+
# @since 2.0.0
|
45
|
+
def ==(other)
|
46
|
+
return false unless other.is_a?(ObjectId)
|
47
|
+
to_bson == other.to_bson
|
48
|
+
end
|
49
|
+
alias :eql? :==
|
50
|
+
|
51
|
+
# Check case equality on the object id.
|
52
|
+
#
|
53
|
+
# @example Check case equality.
|
54
|
+
# object_id === other
|
55
|
+
#
|
56
|
+
# @param [ Object ] other The object to check against.
|
57
|
+
#
|
58
|
+
# @return [ true, false ] If the objects are equal in a case.
|
59
|
+
#
|
60
|
+
# @since 2.0.0
|
61
|
+
def ===(other)
|
62
|
+
return to_str === other.to_str if other.respond_to?(:to_str)
|
63
|
+
super
|
64
|
+
end
|
65
|
+
|
66
|
+
# Return the object id as a JSON hash representation.
|
67
|
+
#
|
68
|
+
# @example Get the object id as JSON.
|
69
|
+
# object_id.as_json
|
70
|
+
#
|
71
|
+
# @return [ Hash ] The object id as a JSON hash.
|
72
|
+
#
|
73
|
+
# @since 2.0.0
|
74
|
+
def as_json(*args)
|
75
|
+
{ "$oid" => to_s }
|
76
|
+
end
|
77
|
+
|
78
|
+
# Compare this object id with another object for use in sorting.
|
79
|
+
#
|
80
|
+
# @example Compare the object id with the other object.
|
81
|
+
# object <=> other
|
82
|
+
#
|
83
|
+
# @param [ Object ] other The object to compare to.
|
84
|
+
#
|
85
|
+
# @return [ Integer ] The result of the comparison.
|
86
|
+
#
|
87
|
+
# @since 2.0.0
|
88
|
+
def <=>(other)
|
89
|
+
to_bson <=> other.to_bson
|
90
|
+
end
|
91
|
+
|
92
|
+
# Return the UTC time at which this ObjectId was generated. This may
|
93
|
+
# be used instread of a created_at timestamp since this information
|
94
|
+
# is always encoded in the object id.
|
95
|
+
#
|
96
|
+
# @example Get the generation time.
|
97
|
+
# object_id.generation_time
|
98
|
+
#
|
99
|
+
# @return [ Time ] The time the id was generated.
|
100
|
+
#
|
101
|
+
# @since 2.0.0
|
102
|
+
def generation_time
|
103
|
+
::Time.at(to_bson.unpack("N")[0]).utc
|
104
|
+
end
|
105
|
+
|
106
|
+
# Get the hash value for the object id.
|
107
|
+
#
|
108
|
+
# @example Get the hash value.
|
109
|
+
# object_id.hash
|
110
|
+
#
|
111
|
+
# @return [ Integer ] The hash value.
|
112
|
+
#
|
113
|
+
# @since 2.0.0
|
114
|
+
def hash
|
115
|
+
to_bson.hash
|
116
|
+
end
|
117
|
+
|
118
|
+
# Get a nice string for use with object inspection.
|
119
|
+
#
|
120
|
+
# @example Inspect the object id.
|
121
|
+
# obhect_id.inspect
|
122
|
+
#
|
123
|
+
# @return [ String ] The object id in form BSON::ObjectId('id')
|
124
|
+
#
|
125
|
+
# @since 2.0.0
|
126
|
+
def inspect
|
127
|
+
"BSON::ObjectId('#{to_s}')"
|
128
|
+
end
|
129
|
+
|
130
|
+
# Dump the raw bson when calling Marshal.dump.
|
131
|
+
#
|
132
|
+
# @example Dump the raw bson.
|
133
|
+
# Marshal.dump(object_id)
|
134
|
+
#
|
135
|
+
# @return [ String ] The raw bson bytes.
|
136
|
+
#
|
137
|
+
# @since 2.0.0
|
138
|
+
def marshal_dump
|
139
|
+
to_bson
|
140
|
+
end
|
141
|
+
|
142
|
+
# Unmarshal the data into an object id.
|
143
|
+
#
|
144
|
+
# @example Unmarshal the data.
|
145
|
+
# Marshal.load(data)
|
146
|
+
#
|
147
|
+
# @param [ String ] data The raw bson bytes.
|
148
|
+
#
|
149
|
+
# @return [ String ] The raw bson bytes.
|
150
|
+
#
|
151
|
+
# @since 2.0.0
|
152
|
+
def marshal_load(data)
|
153
|
+
@raw_data = data
|
154
|
+
end
|
155
|
+
|
156
|
+
# Get the object id as it's raw BSON data.
|
157
|
+
#
|
158
|
+
# @example Get the raw bson bytes.
|
159
|
+
# object_id.to_bson
|
160
|
+
#
|
161
|
+
# @note Since Moped's BSON and MongoDB BSON before 2.0.0 have different
|
162
|
+
# internal representations, we will attempt to repair the data for cases
|
163
|
+
# where the object was instantiated in a non-standard way. (Like a
|
164
|
+
# Marshal.load)
|
165
|
+
#
|
166
|
+
# @return [ String ] The raw bytes.
|
167
|
+
#
|
168
|
+
# @see http://bsonspec.org/#/specification
|
169
|
+
#
|
170
|
+
# @since 2.0.0
|
171
|
+
def to_bson(encoded = ''.force_encoding(BINARY))
|
172
|
+
repair if defined?(@data)
|
173
|
+
@raw_data ||= @@generator.next
|
174
|
+
encoded << @raw_data
|
175
|
+
end
|
176
|
+
|
177
|
+
# Get the string representation of the object id.
|
178
|
+
#
|
179
|
+
# @example Get the object id as a string.
|
180
|
+
# object_id.to_s
|
181
|
+
#
|
182
|
+
# @return [ String ] The object id as a string.
|
183
|
+
#
|
184
|
+
# @since 2.0.0
|
185
|
+
def to_s
|
186
|
+
to_bson.to_hex_string.force_encoding(UTF8)
|
187
|
+
end
|
188
|
+
alias :to_str :to_s
|
189
|
+
|
190
|
+
# Raised when trying to create an object id with invalid data.
|
191
|
+
#
|
192
|
+
# @since 2.0.0
|
193
|
+
class Invalid < RuntimeError; end
|
194
|
+
|
195
|
+
private
|
196
|
+
|
197
|
+
def repair
|
198
|
+
@raw_data = @data.to_bson_object_id
|
199
|
+
remove_instance_variable(:@data)
|
200
|
+
end
|
201
|
+
|
202
|
+
class << self
|
203
|
+
|
204
|
+
# Deserialize the object id from raw BSON bytes.
|
205
|
+
#
|
206
|
+
# @example Get the object id from BSON.
|
207
|
+
# ObjectId.from_bson(bson)
|
208
|
+
#
|
209
|
+
# @param [ String ] bson The raw BSON bytes.
|
210
|
+
#
|
211
|
+
# @return [ BSON::ObjectId ] The object id.
|
212
|
+
#
|
213
|
+
# @since 2.0.0
|
214
|
+
def from_bson(bson)
|
215
|
+
from_data(bson.read(12))
|
216
|
+
end
|
217
|
+
|
218
|
+
# Create a new object id from raw bytes.
|
219
|
+
#
|
220
|
+
# @example Create an object id from raw bytes.
|
221
|
+
# BSON::ObjectId.from_data(data)
|
222
|
+
#
|
223
|
+
# @param [ String ] data The raw bytes.
|
224
|
+
#
|
225
|
+
# @return [ ObjectId ] The new object id.
|
226
|
+
#
|
227
|
+
# @since 2.0.0
|
228
|
+
def from_data(data)
|
229
|
+
object_id = allocate
|
230
|
+
object_id.instance_variable_set(:@raw_data, data)
|
231
|
+
object_id
|
232
|
+
end
|
233
|
+
|
234
|
+
# Create a new object id from a string.
|
235
|
+
#
|
236
|
+
# @example Create an object id from the string.
|
237
|
+
# BSON::ObjectId.from_string(id)
|
238
|
+
#
|
239
|
+
# @param [ String ] string The string to create the id from.
|
240
|
+
#
|
241
|
+
# @raise [ BSON::ObjectId::Invalid ] If the provided string is invalid.
|
242
|
+
#
|
243
|
+
# @return [ BSON::ObjectId ] The new object id.
|
244
|
+
#
|
245
|
+
# @since 2.0.0
|
246
|
+
def from_string(string)
|
247
|
+
unless legal?(string)
|
248
|
+
raise Invalid.new("'#{string}' is an invalid ObjectId.")
|
249
|
+
end
|
250
|
+
from_data([ string ].pack("H*"))
|
251
|
+
end
|
252
|
+
|
253
|
+
# Create a new object id from a time.
|
254
|
+
#
|
255
|
+
# @example Create an object id from a time.
|
256
|
+
# BSON::ObjectId.from_id(time)
|
257
|
+
#
|
258
|
+
# @example Create an object id from a time, ensuring uniqueness.
|
259
|
+
# BSON::ObjectId.from_id(time, unique: true)
|
260
|
+
#
|
261
|
+
# @param [ Time ] time The time to generate from.
|
262
|
+
# @param [ Hash ] options The options.
|
263
|
+
#
|
264
|
+
# @option options [ true, false ] :unique Whether the id should be
|
265
|
+
# unique.
|
266
|
+
#
|
267
|
+
# @return [ ObjectId ] The new object id.
|
268
|
+
#
|
269
|
+
# @since 2.0.0
|
270
|
+
def from_time(time, options = {})
|
271
|
+
from_data(options[:unique] ? @@generator.next(time.to_i) : [ time.to_i ].pack("Nx8"))
|
272
|
+
end
|
273
|
+
|
274
|
+
# Determine if the provided string is a legal object id.
|
275
|
+
#
|
276
|
+
# @example Is the string a legal object id?
|
277
|
+
# BSON::ObjectId.legal?(string)
|
278
|
+
#
|
279
|
+
# @param [ String ] The string to check.
|
280
|
+
#
|
281
|
+
# @return [ true, false ] If the string is legal.
|
282
|
+
#
|
283
|
+
# @since 2.0.0
|
284
|
+
def legal?(string)
|
285
|
+
string.to_s =~ /^[0-9a-f]{24}$/i ? true : false
|
286
|
+
end
|
287
|
+
|
288
|
+
# Executes the provided block only if the size of the provided object is
|
289
|
+
# 12. Used in legacy id repairs.
|
290
|
+
#
|
291
|
+
# @example Execute in a repairing block.
|
292
|
+
# BSON::ObjectId.repair("test") { obj }
|
293
|
+
#
|
294
|
+
# @param [ String, Array ] object The object to repair.
|
295
|
+
#
|
296
|
+
# @raise [ Invalid ] If the array is not 12 elements.
|
297
|
+
#
|
298
|
+
# @return [ String ] The result of the block.
|
299
|
+
#
|
300
|
+
# @since 2.0.0
|
301
|
+
def repair(object)
|
302
|
+
if object.size == 12
|
303
|
+
block_given? ? yield(object) : object
|
304
|
+
else
|
305
|
+
raise Invalid.new("#{object.inspect} is not a valid object id.")
|
306
|
+
end
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
# Inner class that encapsulates the behaviour of actually generating each
|
311
|
+
# part of the ObjectId.
|
312
|
+
#
|
313
|
+
# @api private
|
314
|
+
#
|
315
|
+
# @since 2.0.0
|
316
|
+
class Generator
|
317
|
+
|
318
|
+
# @!attribute machine_id
|
319
|
+
# @return [ String ] The unique machine id.
|
320
|
+
# @since 2.0.0
|
321
|
+
attr_reader :machine_id
|
322
|
+
|
323
|
+
# Instantiate the new object id generator. Will set the machine id once
|
324
|
+
# on the initial instantiation.
|
325
|
+
#
|
326
|
+
# @example Instantiate the generator.
|
327
|
+
# BSON::ObjectId::Generator.new
|
328
|
+
#
|
329
|
+
# @since 2.0.0
|
330
|
+
def initialize
|
331
|
+
@counter = 0
|
332
|
+
@machine_id = Digest::MD5.digest(Socket.gethostname).unpack("N")[0]
|
333
|
+
@mutex = Mutex.new
|
334
|
+
end
|
335
|
+
|
336
|
+
# Return object id data based on the current time, incrementing the
|
337
|
+
# object id counter. Will use the provided time if not nil.
|
338
|
+
#
|
339
|
+
# @example Get the next object id data.
|
340
|
+
# generator.next
|
341
|
+
#
|
342
|
+
# @param [ Time ] time The optional time to generate with.
|
343
|
+
#
|
344
|
+
# @return [ String ] The raw object id bytes.
|
345
|
+
#
|
346
|
+
# @since 2.0.0
|
347
|
+
def next(time = nil)
|
348
|
+
@mutex.lock
|
349
|
+
begin
|
350
|
+
count = @counter = (@counter + 1) % 0xFFFFFF
|
351
|
+
ensure
|
352
|
+
@mutex.unlock rescue nil
|
353
|
+
end
|
354
|
+
generate(time || ::Time.new.to_i, count)
|
355
|
+
end
|
356
|
+
|
357
|
+
# Generate object id data for a given time using the provided counter.
|
358
|
+
#
|
359
|
+
# @example Generate the object id bytes.
|
360
|
+
# generator.generate(time)
|
361
|
+
#
|
362
|
+
# @param [ Integer ] time The time since epoch in seconds.
|
363
|
+
# @param [ Integer ] counter The optional counter.
|
364
|
+
#
|
365
|
+
# @return [ String ] The raw object id bytes.
|
366
|
+
#
|
367
|
+
# @since 2.0.0
|
368
|
+
def generate(time, counter = 0)
|
369
|
+
[ time, machine_id, process_id, counter << 8 ].pack("N NX lXX NX")
|
370
|
+
end
|
371
|
+
|
372
|
+
private
|
373
|
+
|
374
|
+
if Environment.jruby?
|
375
|
+
def process_id
|
376
|
+
"#{Process.pid}#{Thread.current.object_id}".hash % 0xFFFF
|
377
|
+
end
|
378
|
+
else
|
379
|
+
def process_id
|
380
|
+
Process.pid % 0xFFFF
|
381
|
+
end
|
382
|
+
end
|
383
|
+
end
|
384
|
+
|
385
|
+
# We keep one global generator for object ids.
|
386
|
+
#
|
387
|
+
# @since 2.0.0
|
388
|
+
@@generator = Generator.new
|
389
|
+
|
390
|
+
# Register this type when the module is loaded.
|
391
|
+
#
|
392
|
+
# @since 2.0.0
|
393
|
+
Registry.register(BSON_TYPE, self)
|
394
|
+
end
|
395
|
+
end
|
data/lib/bson/regexp.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# Copyright (C) 2009-2013 MongoDB Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
module BSON
|
16
|
+
|
17
|
+
# Injects behaviour for encoding and decoding regular expression values to
|
18
|
+
# and from raw bytes as specified by the BSON spec.
|
19
|
+
#
|
20
|
+
# @see http://bsonspec.org/#/specification
|
21
|
+
#
|
22
|
+
# @since 2.0.0
|
23
|
+
module Regexp
|
24
|
+
include JSON
|
25
|
+
|
26
|
+
# A regular expression is type 0x0B in the BSON spec.
|
27
|
+
#
|
28
|
+
# @since 2.0.0
|
29
|
+
BSON_TYPE = 11.chr.force_encoding(BINARY).freeze
|
30
|
+
|
31
|
+
# Get the regexp as JSON hash data.
|
32
|
+
#
|
33
|
+
# @example Get the regexp as a JSON hash.
|
34
|
+
# regexp.as_json
|
35
|
+
#
|
36
|
+
# @return [ Hash ] The regexp as a JSON hash.
|
37
|
+
#
|
38
|
+
# @since 2.0.0
|
39
|
+
def as_json(*args)
|
40
|
+
{ "$regex" => source, "$options" => bson_options }
|
41
|
+
end
|
42
|
+
|
43
|
+
# Get the regular expression as encoded BSON.
|
44
|
+
#
|
45
|
+
# @example Get the regular expression as encoded BSON.
|
46
|
+
# %r{\d+}.to_bson
|
47
|
+
#
|
48
|
+
# @note From the BSON spec: The first cstring is the regex pattern,
|
49
|
+
# the second is the regex options string. Options are identified
|
50
|
+
# by characters, which must be stored in alphabetical order.
|
51
|
+
# Valid options are 'i' for case insensitive matching,
|
52
|
+
# 'm' for multiline matching, 'x' for verbose mode,
|
53
|
+
# 'l' to make \w, \W, etc. locale dependent,
|
54
|
+
# 's' for dotall mode ('.' matches everything),
|
55
|
+
# and 'u' to make \w, \W, etc. match unicode.
|
56
|
+
#
|
57
|
+
# @return [ String ] The encoded string.
|
58
|
+
#
|
59
|
+
# @see http://bsonspec.org/#/specification
|
60
|
+
#
|
61
|
+
# @since 2.0.0
|
62
|
+
def to_bson(encoded = ''.force_encoding(BINARY))
|
63
|
+
source.to_bson_cstring(encoded)
|
64
|
+
bson_options.to_bson_cstring(encoded)
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def bson_options
|
70
|
+
bson_ignorecase + bson_multiline + bson_extended
|
71
|
+
end
|
72
|
+
|
73
|
+
def bson_extended
|
74
|
+
(options & ::Regexp::EXTENDED != 0) ? "x" : NO_VALUE
|
75
|
+
end
|
76
|
+
|
77
|
+
def bson_ignorecase
|
78
|
+
(options & ::Regexp::IGNORECASE != 0) ? "i" : NO_VALUE
|
79
|
+
end
|
80
|
+
|
81
|
+
def bson_multiline
|
82
|
+
(options & ::Regexp::MULTILINE != 0) ? "ms" : NO_VALUE
|
83
|
+
end
|
84
|
+
|
85
|
+
module ClassMethods
|
86
|
+
# Deserialize the regular expression from BSON.
|
87
|
+
#
|
88
|
+
# @param [ BSON ] bson The bson representing a regular expression.
|
89
|
+
#
|
90
|
+
# @return [ Regexp ] The decoded regular expression.
|
91
|
+
#
|
92
|
+
# @see http://bsonspec.org/#/specification
|
93
|
+
#
|
94
|
+
# @since 2.0.0
|
95
|
+
def from_bson(bson)
|
96
|
+
pattern = bson.gets(NULL_BYTE).from_bson_string.chop!
|
97
|
+
options = 0
|
98
|
+
while (option = bson.readbyte) != 0
|
99
|
+
case option
|
100
|
+
when 105 # 'i'
|
101
|
+
options |= ::Regexp::IGNORECASE
|
102
|
+
when 109, 115 # 'm', 's'
|
103
|
+
options |= ::Regexp::MULTILINE
|
104
|
+
when 120 # 'x'
|
105
|
+
options |= ::Regexp::EXTENDED
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
new(pattern, options)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# Register this type when the module is loaded.
|
114
|
+
#
|
115
|
+
# @since 2.0.0
|
116
|
+
Registry.register(BSON_TYPE, ::Regexp)
|
117
|
+
end
|
118
|
+
|
119
|
+
# Enrich the core Regexp class with this module.
|
120
|
+
#
|
121
|
+
# @since 2.0.0
|
122
|
+
::Regexp.send(:include, Regexp)
|
123
|
+
::Regexp.send(:extend, Regexp::ClassMethods)
|
124
|
+
end
|