Class: BSON::BSON_RUBY
- Inherits:
-
Object
- Object
- BSON::BSON_RUBY
- Defined in:
- lib/bson/bson_ruby.rb
Overview
A BSON seralizer/deserializer in pure Ruby.
Constant Summary collapse
- DEFAULT_MAX_BSON_SIZE =
4 * 1024 * 1024
- MINKEY =
-1
- EOO =
0
- NUMBER =
1
- STRING =
2
- OBJECT =
3
- ARRAY =
4
- BINARY =
5
- UNDEFINED =
6
- OID =
7
- BOOLEAN =
8
- DATE =
9
- NULL =
10
- REGEX =
11
- REF =
12
- CODE =
13
- SYMBOL =
14
- CODE_W_SCOPE =
15
- NUMBER_INT =
16
- TIMESTAMP =
17
- NUMBER_LONG =
18
- MAXKEY =
127
- NULL_BYTE =
"\0"
- UTF8_ENCODING =
Encoding.find('utf-8')
- BINARY_ENCODING =
Encoding.find('binary')
- @@max_bson_size =
DEFAULT_MAX_BSON_SIZE
Class Method Summary collapse
- .deserialize(buf = nil) ⇒ Object
- .max_bson_size ⇒ Object
-
.serialize(obj, check_keys = false, move_id = false, max_bson_size = BSON::DEFAULT_MAX_BSON_SIZE) ⇒ Object
Serializes an object.
- .serialize_cstr(buf, val) ⇒ Object
- .serialize_key(buf, key) ⇒ Object
- .to_utf8_binary(str) ⇒ Object
- .update_max_bson_size(connection) ⇒ Object
Instance Method Summary collapse
- #bson_type(o) ⇒ Object
- #deserialize(buf = nil) ⇒ Object
- #deserialize_array_data(buf) ⇒ Object
- #deserialize_binary_data(buf) ⇒ Object
- #deserialize_boolean_data(buf) ⇒ Object
- #deserialize_code_w_scope_data(buf) ⇒ Object
- #deserialize_cstr(buf) ⇒ Object
- #deserialize_date_data(buf) ⇒ Object
- #deserialize_dbref_data(buf) ⇒ Object
- #deserialize_number_data(buf) ⇒ Object
- #deserialize_number_int_data(buf) ⇒ Object
- #deserialize_number_long_data(buf) ⇒ Object
- #deserialize_object_data(buf) ⇒ Object
- #deserialize_oid_data(buf) ⇒ Object
- #deserialize_regex_data(buf) ⇒ Object
- #deserialize_string_data(buf) ⇒ Object
- #deserialize_timestamp_data(buf) ⇒ Object
- #encoded_str(str) ⇒ Object
-
#hex_dump ⇒ Object
For debugging.
-
#initialize(max_bson_size = BSON::DEFAULT_MAX_BSON_SIZE) ⇒ BSON_RUBY
constructor
A new instance of BSON_RUBY.
- #serialize(obj, check_keys = false, move_id = false) ⇒ Object
- #serialize_array_element(buf, key, val, check_keys) ⇒ Object
- #serialize_binary_element(buf, key, val) ⇒ Object
- #serialize_boolean_element(buf, key, val) ⇒ Object
- #serialize_code_w_scope(buf, key, val) ⇒ Object
- #serialize_date_element(buf, key, val) ⇒ Object
- #serialize_dbref_element(buf, key, val) ⇒ Object
- #serialize_eoo_element(buf) ⇒ Object
- #serialize_key_value(k, v, check_keys) ⇒ Object
- #serialize_max_key_element(buf, key) ⇒ Object
- #serialize_min_key_element(buf, key) ⇒ Object
- #serialize_null_element(buf, key) ⇒ Object
- #serialize_number_element(buf, key, val, type) ⇒ Object
- #serialize_object_element(buf, key, val, check_keys, opcode = OBJECT) ⇒ Object
- #serialize_oid_element(buf, key, val) ⇒ Object
- #serialize_regex_element(buf, key, val) ⇒ Object
- #serialize_string_element(buf, key, val, type) ⇒ Object
- #serialize_timestamp_element(buf, key, val) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
-
#unpack(arg) ⇒ Object
Returns the array stored in the buffer.
Constructor Details
#initialize(max_bson_size = BSON::DEFAULT_MAX_BSON_SIZE) ⇒ BSON_RUBY
Returns a new instance of BSON_RUBY.
49 50 51 52 |
# File 'lib/bson/bson_ruby.rb', line 49 def initialize(max_bson_size=BSON::DEFAULT_MAX_BSON_SIZE) @buf = ByteBuffer.new('', max_bson_size) @encoder = BSON_RUBY end |
Class Method Details
.deserialize(buf = nil) ⇒ Object
114 115 116 |
# File 'lib/bson/bson_ruby.rb', line 114 def self.deserialize(buf=nil) new.deserialize(buf) end |
.max_bson_size ⇒ Object
85 86 87 88 |
# File 'lib/bson/bson_ruby.rb', line 85 def self.max_bson_size warn "BSON::BSON_CODER.max_bson_size is deprecated and will be removed in v2.0." @@max_bson_size end |
.serialize(obj, check_keys = false, move_id = false, max_bson_size = BSON::DEFAULT_MAX_BSON_SIZE) ⇒ Object
Serializes an object. Implemented to ensure an API compatible with BSON extension.
110 111 112 |
# File 'lib/bson/bson_ruby.rb', line 110 def self.serialize(obj, check_keys=false, move_id=false, max_bson_size=BSON::DEFAULT_MAX_BSON_SIZE) new(max_bson_size).serialize(obj, check_keys, move_id) end |
.serialize_cstr(buf, val) ⇒ Object
90 91 92 93 |
# File 'lib/bson/bson_ruby.rb', line 90 def self.serialize_cstr(buf, val) buf.put_binary(to_utf8_binary(val.to_s)) buf.put_binary(NULL_BYTE) end |
.serialize_key(buf, key) ⇒ Object
95 96 97 98 |
# File 'lib/bson/bson_ruby.rb', line 95 def self.serialize_key(buf, key) raise InvalidDocument, "Key names / regex patterns must not contain the NULL byte" if key.include? "\x00" self.serialize_cstr(buf, key) end |
.to_utf8_binary(str) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/bson/bson_ruby.rb', line 59 def self.to_utf8_binary(str) begin str.unpack("U*") rescue => ex raise InvalidStringEncoding, "String not valid utf-8: #{str.inspect}" end str.encode(UTF8_ENCODING).force_encoding(BINARY_ENCODING) end |
.update_max_bson_size(connection) ⇒ Object
80 81 82 83 |
# File 'lib/bson/bson_ruby.rb', line 80 def self.update_max_bson_size(connection) warn "BSON::BSON_CODER.update_max_bson_size is deprecated and now a no-op. It will be removed in v2.0." @@max_bson_size = connection.max_bson_size end |
Instance Method Details
#bson_type(o) ⇒ Object
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
# File 'lib/bson/bson_ruby.rb', line 589 def bson_type(o) case o when nil NULL when Integer NUMBER_INT when Float NUMBER when ByteBuffer BINARY when Code CODE_W_SCOPE when String STRING when Array ARRAY when Regexp REGEX when ObjectId OID when DBRef REF when true, false BOOLEAN when Time DATE when Hash OBJECT when Symbol SYMBOL when MaxKey MAXKEY when MinKey MINKEY when Timestamp TIMESTAMP when Numeric raise InvalidDocument, "Cannot serialize the Numeric type #{o.class} as BSON; only Fixum, Bignum, and Float are supported." when Date, DateTime raise InvalidDocument, "#{o.class} is not currently supported; " + "use a UTC Time instance instead." else if defined?(ActiveSupport::TimeWithZone) && o.is_a?(ActiveSupport::TimeWithZone) raise InvalidDocument, "ActiveSupport::TimeWithZone is not currently supported; " + "use a UTC Time instance instead." else raise InvalidDocument, "Cannot serialize #{o.class} as a BSON type; it either isn't supported or won't translate to BSON." end end end |
#deserialize(buf = nil) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/bson/bson_ruby.rb', line 205 def deserialize(buf=nil) # If buf is nil, use @buf, assumed to contain already-serialized BSON. # This is only true during testing. if buf.is_a? String @buf = ByteBuffer.new(buf.unpack("C*")) if buf else @buf = ByteBuffer.new(buf.to_a) if buf end @buf.rewind @buf.get_int # eat message size doc = BSON::OrderedHash.new while @buf.more? type = @buf.get case type when STRING, CODE key = deserialize_cstr(@buf) doc[key] = deserialize_string_data(@buf) when SYMBOL key = deserialize_cstr(@buf) doc[key] = deserialize_string_data(@buf).intern when NUMBER key = deserialize_cstr(@buf) doc[key] = deserialize_number_data(@buf) when NUMBER_INT key = deserialize_cstr(@buf) doc[key] = deserialize_number_int_data(@buf) when NUMBER_LONG key = deserialize_cstr(@buf) doc[key] = deserialize_number_long_data(@buf) when OID key = deserialize_cstr(@buf) doc[key] = deserialize_oid_data(@buf) when ARRAY key = deserialize_cstr(@buf) doc[key] = deserialize_array_data(@buf) when REGEX key = deserialize_cstr(@buf) doc[key] = deserialize_regex_data(@buf) when OBJECT key = deserialize_cstr(@buf) doc[key] = deserialize_object_data(@buf) when BOOLEAN key = deserialize_cstr(@buf) doc[key] = deserialize_boolean_data(@buf) when DATE key = deserialize_cstr(@buf) doc[key] = deserialize_date_data(@buf) when NULL key = deserialize_cstr(@buf) doc[key] = nil when UNDEFINED key = deserialize_cstr(@buf) doc[key] = nil when REF key = deserialize_cstr(@buf) doc[key] = deserialize_dbref_data(@buf) when BINARY key = deserialize_cstr(@buf) doc[key] = deserialize_binary_data(@buf) when CODE_W_SCOPE key = deserialize_cstr(@buf) doc[key] = deserialize_code_w_scope_data(@buf) when TIMESTAMP key = deserialize_cstr(@buf) doc[key] = (@buf) when MAXKEY key = deserialize_cstr(@buf) doc[key] = MaxKey.new when MINKEY, 255 # This is currently easier than unpack the type byte as an unsigned char. key = deserialize_cstr(@buf) doc[key] = MinKey.new when EOO break else raise "Unknown type #{type}, key = #{key}" end end @buf.rewind doc end |
#deserialize_array_data(buf) ⇒ Object
336 337 338 339 340 341 |
# File 'lib/bson/bson_ruby.rb', line 336 def deserialize_array_data(buf) h = deserialize_object_data(buf) a = [] h.each { |k, v| a[k.to_i] = v } a end |
#deserialize_binary_data(buf) ⇒ Object
405 406 407 408 409 410 |
# File 'lib/bson/bson_ruby.rb', line 405 def deserialize_binary_data(buf) len = buf.get_int type = buf.get len = buf.get_int if type == Binary::SUBTYPE_BYTES Binary.new(buf.get(len), type) end |
#deserialize_boolean_data(buf) ⇒ Object
307 308 309 |
# File 'lib/bson/bson_ruby.rb', line 307 def deserialize_boolean_data(buf) buf.get == 1 end |
#deserialize_code_w_scope_data(buf) ⇒ Object
380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/bson/bson_ruby.rb', line 380 def deserialize_code_w_scope_data(buf) buf.get_int len = buf.get_int code = buf.get(len)[0..-2] if code.respond_to? "pack" code = code.pack("C*") end scope_size = buf.get_int buf.position -= 4 scope = @encoder.new().deserialize(buf.get(scope_size)) Code.new(encoded_str(code), scope) end |
#deserialize_cstr(buf) ⇒ Object
579 580 581 582 583 584 585 586 587 |
# File 'lib/bson/bson_ruby.rb', line 579 def deserialize_cstr(buf) chars = "" while true b = buf.get break if b == 0 chars << b.chr end encoded_str(chars) end |
#deserialize_date_data(buf) ⇒ Object
301 302 303 304 305 |
# File 'lib/bson/bson_ruby.rb', line 301 def deserialize_date_data(buf) unsigned = buf.get_long() milliseconds = unsigned >= 2 ** 64 / 2 ? unsigned - 2**64 : unsigned Time.at(milliseconds.to_f / 1000.0).utc # at() takes fractional seconds end |
#deserialize_dbref_data(buf) ⇒ Object
399 400 401 402 403 |
# File 'lib/bson/bson_ruby.rb', line 399 def deserialize_dbref_data(buf) ns = deserialize_string_data(buf) oid = deserialize_oid_data(buf) DBRef.new(ns, oid) end |
#deserialize_number_data(buf) ⇒ Object
311 312 313 |
# File 'lib/bson/bson_ruby.rb', line 311 def deserialize_number_data(buf) buf.get_double end |
#deserialize_number_int_data(buf) ⇒ Object
315 316 317 318 |
# File 'lib/bson/bson_ruby.rb', line 315 def deserialize_number_int_data(buf) unsigned = buf.get_int unsigned >= 2**32 / 2 ? unsigned - 2**32 : unsigned end |
#deserialize_number_long_data(buf) ⇒ Object
320 321 322 323 |
# File 'lib/bson/bson_ruby.rb', line 320 def deserialize_number_long_data(buf) unsigned = buf.get_long unsigned >= 2 ** 64 / 2 ? unsigned - 2**64 : unsigned end |
#deserialize_object_data(buf) ⇒ Object
325 326 327 328 329 330 331 332 333 334 |
# File 'lib/bson/bson_ruby.rb', line 325 def deserialize_object_data(buf) size = buf.get_int buf.position -= 4 object = @encoder.new().deserialize(buf.get(size)) if object.has_key? "$ref" DBRef.new(object["$ref"], object["$id"]) else object end end |
#deserialize_oid_data(buf) ⇒ Object
395 396 397 |
# File 'lib/bson/bson_ruby.rb', line 395 def deserialize_oid_data(buf) ObjectId.new(buf.get(12)) end |
#deserialize_regex_data(buf) ⇒ Object
343 344 345 346 347 348 349 350 351 352 |
# File 'lib/bson/bson_ruby.rb', line 343 def deserialize_regex_data(buf) str = deserialize_cstr(buf) = deserialize_cstr(buf) opts = 0 opts |= Regexp::IGNORECASE if .include?('i') opts |= Regexp::MULTILINE if .include?('m') opts |= Regexp::MULTILINE if .include?('s') opts |= Regexp::EXTENDED if .include?('x') Regexp.new(str, opts) end |
#deserialize_string_data(buf) ⇒ Object
370 371 372 373 374 375 376 377 378 |
# File 'lib/bson/bson_ruby.rb', line 370 def deserialize_string_data(buf) len = buf.get_int bytes = buf.get(len) str = bytes[0..-2] if str.respond_to? "pack" str = str.pack("C*") end encoded_str(str) end |
#deserialize_timestamp_data(buf) ⇒ Object
354 355 356 357 358 |
# File 'lib/bson/bson_ruby.rb', line 354 def (buf) increment = buf.get_int seconds = buf.get_int Timestamp.new(seconds, increment) end |
#encoded_str(str) ⇒ Object
360 361 362 363 364 365 366 367 368 |
# File 'lib/bson/bson_ruby.rb', line 360 def encoded_str(str) if RUBY_VERSION >= '1.9' str.force_encoding("utf-8") if Encoding.default_internal str.encode!(Encoding.default_internal) end end str end |
#hex_dump ⇒ Object
For debugging.
287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/bson/bson_ruby.rb', line 287 def hex_dump str = '' @buf.to_a.each_with_index { |b,i| if (i % 8) == 0 str << "\n" if i > 0 str << '%4d: ' % i else str << ' ' end str << '%02X' % b } str end |
#serialize(obj, check_keys = false, move_id = false) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/bson/bson_ruby.rb', line 118 def serialize(obj, check_keys=false, move_id=false) raise(InvalidDocument, "BSON.serialize takes a Hash but got a #{obj.class}") unless obj.is_a?(Hash) raise "Document is null" unless obj @buf.rewind # put in a placeholder for the total size @buf.put_int(0) # Write key/value pairs. Always write _id first if it exists. if move_id if obj.has_key? '_id' serialize_key_value('_id', obj['_id'], false) elsif obj.has_key? :_id serialize_key_value('_id', obj[:_id], false) end obj.each {|k, v| serialize_key_value(k, v, check_keys) unless k == '_id' || k == :_id } else if obj.has_key?('_id') && obj.has_key?(:_id) obj['_id'] = obj.delete(:_id) end obj.each {|k, v| serialize_key_value(k, v, check_keys) } end serialize_eoo_element(@buf) if @buf.size > @buf.max_size raise InvalidDocument, "Document is too large (#{@buf.size}). " + "This BSON documents is limited to #{@buf.max_size} bytes." end @buf.put_int(@buf.size, 0) @buf end |
#serialize_array_element(buf, key, val, check_keys) ⇒ Object
487 488 489 490 491 492 493 |
# File 'lib/bson/bson_ruby.rb', line 487 def serialize_array_element(buf, key, val, check_keys) # Turn array into hash with integer indices as keys h = BSON::OrderedHash.new i = 0 val.each { |v| h[i] = v; i += 1 } serialize_object_element(buf, key, h, check_keys, ARRAY) end |
#serialize_binary_element(buf, key, val) ⇒ Object
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/bson/bson_ruby.rb', line 428 def serialize_binary_element(buf, key, val) buf.put(BINARY) self.class.serialize_key(buf, key) bytes = val.to_a num_bytes = bytes.length subtype = val.respond_to?(:subtype) ? val.subtype : Binary::SUBTYPE_BYTES if subtype == Binary::SUBTYPE_BYTES buf.put_int(num_bytes + 4) buf.put(subtype) buf.put_int(num_bytes) buf.put_array(bytes) else buf.put_int(num_bytes) buf.put(subtype) buf.put_array(bytes) end end |
#serialize_boolean_element(buf, key, val) ⇒ Object
447 448 449 450 451 |
# File 'lib/bson/bson_ruby.rb', line 447 def serialize_boolean_element(buf, key, val) buf.put(BOOLEAN) self.class.serialize_key(buf, key) buf.put(val ? 1 : 0) end |
#serialize_code_w_scope(buf, key, val) ⇒ Object
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
# File 'lib/bson/bson_ruby.rb', line 562 def serialize_code_w_scope(buf, key, val) buf.put(CODE_W_SCOPE) self.class.serialize_key(buf, key) # Make a hole for the length len_pos = buf.position buf.put_int(0) buf.put_int(val.code.length + 1) self.class.serialize_cstr(buf, val.code) buf.put_array(@encoder.new.serialize(val.scope).to_a) end_pos = buf.position buf.put_int(end_pos - len_pos, len_pos) buf.position = end_pos end |
#serialize_date_element(buf, key, val) ⇒ Object
453 454 455 456 457 458 |
# File 'lib/bson/bson_ruby.rb', line 453 def serialize_date_element(buf, key, val) buf.put(DATE) self.class.serialize_key(buf, key) millisecs = (val.to_f * 1000).to_i buf.put_long(millisecs) end |
#serialize_dbref_element(buf, key, val) ⇒ Object
421 422 423 424 425 426 |
# File 'lib/bson/bson_ruby.rb', line 421 def serialize_dbref_element(buf, key, val) oh = BSON::OrderedHash.new oh['$ref'] = val.namespace oh['$id'] = val.object_id serialize_object_element(buf, key, oh, false) end |
#serialize_eoo_element(buf) ⇒ Object
412 413 414 |
# File 'lib/bson/bson_ruby.rb', line 412 def serialize_eoo_element(buf) buf.put(EOO) end |
#serialize_key_value(k, v, check_keys) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/bson/bson_ruby.rb', line 156 def serialize_key_value(k, v, check_keys) k = k.to_s if check_keys if k[0] == ?$ raise InvalidKeyName.new("key #{k} must not start with '$'") end if k.include? ?. raise InvalidKeyName.new("key #{k} must not contain '.'") end end type = bson_type(v) case type when STRING, SYMBOL serialize_string_element(@buf, k, v, type) when NUMBER, NUMBER_INT serialize_number_element(@buf, k, v, type) when OBJECT serialize_object_element(@buf, k, v, check_keys) when OID serialize_oid_element(@buf, k, v) when ARRAY serialize_array_element(@buf, k, v, check_keys) when REGEX serialize_regex_element(@buf, k, v) when BOOLEAN serialize_boolean_element(@buf, k, v) when DATE serialize_date_element(@buf, k, v) when NULL serialize_null_element(@buf, k) when REF serialize_dbref_element(@buf, k, v) when BINARY serialize_binary_element(@buf, k, v) when UNDEFINED serialize_null_element(@buf, k) when CODE_W_SCOPE serialize_code_w_scope(@buf, k, v) when MAXKEY serialize_max_key_element(@buf, k) when MINKEY serialize_min_key_element(@buf, k) when TIMESTAMP (@buf, k, v) else raise "unhandled type #{type}" end end |
#serialize_max_key_element(buf, key) ⇒ Object
517 518 519 520 |
# File 'lib/bson/bson_ruby.rb', line 517 def serialize_max_key_element(buf, key) buf.put(MAXKEY) self.class.serialize_key(buf, key) end |
#serialize_min_key_element(buf, key) ⇒ Object
522 523 524 525 |
# File 'lib/bson/bson_ruby.rb', line 522 def serialize_min_key_element(buf, key) buf.put(MINKEY) self.class.serialize_key(buf, key) end |
#serialize_null_element(buf, key) ⇒ Object
416 417 418 419 |
# File 'lib/bson/bson_ruby.rb', line 416 def serialize_null_element(buf, key) buf.put(NULL) self.class.serialize_key(buf, key) end |
#serialize_number_element(buf, key, val, type) ⇒ Object
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/bson/bson_ruby.rb', line 460 def serialize_number_element(buf, key, val, type) if type == NUMBER buf.put(type) self.class.serialize_key(buf, key) buf.put_double(val) else if val > 2**64 / 2 - 1 or val < -2**64 / 2 raise RangeError.new("MongoDB can only handle 8-byte ints") end if val > 2**32 / 2 - 1 or val < -2**32 / 2 buf.put(NUMBER_LONG) self.class.serialize_key(buf, key) buf.put_long(val) else buf.put(type) self.class.serialize_key(buf, key) buf.put_int(val) end end end |
#serialize_object_element(buf, key, val, check_keys, opcode = OBJECT) ⇒ Object
481 482 483 484 485 |
# File 'lib/bson/bson_ruby.rb', line 481 def serialize_object_element(buf, key, val, check_keys, opcode=OBJECT) buf.put(opcode) self.class.serialize_key(buf, key) buf.put_array(@encoder.new.serialize(val, check_keys).to_a) end |
#serialize_oid_element(buf, key, val) ⇒ Object
535 536 537 538 539 540 |
# File 'lib/bson/bson_ruby.rb', line 535 def serialize_oid_element(buf, key, val) buf.put(OID) self.class.serialize_key(buf, key) buf.put_array(val.to_a) end |
#serialize_regex_element(buf, key, val) ⇒ Object
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'lib/bson/bson_ruby.rb', line 495 def serialize_regex_element(buf, key, val) buf.put(REGEX) self.class.serialize_key(buf, key) str = val.source # We use serialize_key here since regex patterns aren't prefixed with # length (can't contain the NULL byte). self.class.serialize_key(buf, str) = val. = '' << 'i' if (( & Regexp::IGNORECASE) != 0) if (( & Regexp::MULTILINE) != 0) << 'm' << 's' end << 'x' if (( & Regexp::EXTENDED) != 0) << val. if val.respond_to?(:extra_options_str) # Must store option chars in alphabetical order self.class.serialize_cstr(buf, .split(//).sort.uniq.join) end |
#serialize_string_element(buf, key, val, type) ⇒ Object
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
# File 'lib/bson/bson_ruby.rb', line 542 def serialize_string_element(buf, key, val, type) buf.put(type) self.class.serialize_key(buf, key) # Make a hole for the length len_pos = buf.position buf.put_int(0) # Save the string start_pos = buf.position self.class.serialize_cstr(buf, val) end_pos = buf.position # Put the string size in front buf.put_int(end_pos - start_pos, len_pos) # Go back to where we were buf.position = end_pos end |
#serialize_timestamp_element(buf, key, val) ⇒ Object
527 528 529 530 531 532 533 |
# File 'lib/bson/bson_ruby.rb', line 527 def (buf, key, val) buf.put(TIMESTAMP) self.class.serialize_key(buf, key) buf.put_int(val.increment) buf.put_int(val.seconds) end |
#to_a ⇒ Object
100 101 102 |
# File 'lib/bson/bson_ruby.rb', line 100 def to_a @buf.to_a end |
#to_s ⇒ Object
104 105 106 |
# File 'lib/bson/bson_ruby.rb', line 104 def to_s @buf.to_s end |
#unpack(arg) ⇒ Object
Returns the array stored in the buffer. Implemented to ensure an API compatible with BSON extension.
152 153 154 |
# File 'lib/bson/bson_ruby.rb', line 152 def unpack(arg) @buf.to_a end |