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,86 @@
|
|
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
|
+
# Defines behaviour around objects that can be encoded.
|
18
|
+
#
|
19
|
+
# @since 2.0.0
|
20
|
+
module Encodable
|
21
|
+
|
22
|
+
# A 4 byte placeholder that would be replaced by a length at a later point.
|
23
|
+
#
|
24
|
+
# @since 2.0.0
|
25
|
+
PLACEHOLDER = 0.to_bson.freeze
|
26
|
+
|
27
|
+
# Adjustment value for total number of document bytes.
|
28
|
+
#
|
29
|
+
# @since 2.0.0
|
30
|
+
BSON_ADJUST = 0.freeze
|
31
|
+
|
32
|
+
# Adjustment value for total number of string bytes.
|
33
|
+
#
|
34
|
+
# @since 2.0.0
|
35
|
+
STRING_ADJUST = -4.freeze
|
36
|
+
|
37
|
+
# Encodes BSON to raw bytes, for types that require the length of the
|
38
|
+
# entire bytes to be present as the first word of the encoded string. This
|
39
|
+
# includes Hash, CodeWithScope.
|
40
|
+
#
|
41
|
+
# @example Encode the BSON with placeholder bytes.
|
42
|
+
# hash.encode_with_placeholder_and_null(BSON_ADJUST, encoded) do |encoded|
|
43
|
+
# each do |field, value|
|
44
|
+
# value.to_bson(encoded)
|
45
|
+
# end
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
# @param [ Integer ] adjust The number of bytes to adjust with.
|
49
|
+
# @param [ String ] encoded The string to encode.
|
50
|
+
#
|
51
|
+
# @return [ String ] The encoded string.
|
52
|
+
#
|
53
|
+
# @since 2.0.0
|
54
|
+
def encode_with_placeholder_and_null(adjust, encoded = ''.force_encoding(BINARY))
|
55
|
+
pos = encoded.bytesize
|
56
|
+
encoded << PLACEHOLDER
|
57
|
+
yield(encoded)
|
58
|
+
encoded << NULL_BYTE
|
59
|
+
encoded.set_int32(pos, encoded.bytesize - pos + adjust)
|
60
|
+
encoded
|
61
|
+
end
|
62
|
+
|
63
|
+
# Encodes binary data with a generic placeholder value to be written later
|
64
|
+
# once all bytes have been written.
|
65
|
+
#
|
66
|
+
# @example Encode the BSON with placeholder bytes.
|
67
|
+
# string.encode_binary_data_with_placeholder(encoded) do |encoded|
|
68
|
+
# each do |field, value|
|
69
|
+
# value.to_bson(encoded)
|
70
|
+
# end
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# @param [ String ] encoded The string to encode.
|
74
|
+
#
|
75
|
+
# @return [ String ] The encoded string.
|
76
|
+
#
|
77
|
+
# @since 2.0.0
|
78
|
+
def encode_binary_data_with_placeholder(encoded = ''.force_encoding(BINARY))
|
79
|
+
pos = encoded.bytesize
|
80
|
+
encoded << PLACEHOLDER
|
81
|
+
yield(encoded)
|
82
|
+
encoded.set_int32(pos, encoded.bytesize - pos - 5)
|
83
|
+
encoded
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,98 @@
|
|
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
|
+
# Provides static helper methods around determining what environment is
|
18
|
+
# running without polluting the global namespace.
|
19
|
+
#
|
20
|
+
# @since 2.0.0
|
21
|
+
module Environment
|
22
|
+
extend self
|
23
|
+
|
24
|
+
# Determine if we are using JRuby or not.
|
25
|
+
#
|
26
|
+
# @example Are we running with JRuby?
|
27
|
+
# Environment.jruby?
|
28
|
+
#
|
29
|
+
# @return [ true, false ] If JRuby is our vm.
|
30
|
+
#
|
31
|
+
# @since 2.0.0
|
32
|
+
def jruby?
|
33
|
+
defined?(JRUBY_VERSION)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Does the Ruby runtime we are using support ordered hashes?
|
37
|
+
#
|
38
|
+
# @example Does the runtime support ordered hashes?
|
39
|
+
# Environment.retaining_hash_order?
|
40
|
+
#
|
41
|
+
# @return [ true, false ] If the runtime has ordered hashes.
|
42
|
+
#
|
43
|
+
# @since 2.0.0
|
44
|
+
def retaining_hash_order?
|
45
|
+
jruby? || RUBY_VERSION > "1.9.1"
|
46
|
+
end
|
47
|
+
|
48
|
+
# Are we running in a ruby runtime that is version 1.8.x?
|
49
|
+
#
|
50
|
+
# @example Is the ruby runtime in 1.8 mode?
|
51
|
+
# Environment.ruby_18?
|
52
|
+
#
|
53
|
+
# @return [ true, false ] If we are running in 1.8.
|
54
|
+
#
|
55
|
+
# @since 2.0.0
|
56
|
+
def ruby_18?
|
57
|
+
RUBY_VERSION < "1.9"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# In the case where we don't have encoding, we need to monkey
|
63
|
+
# patch string to ignore the encoding directives.
|
64
|
+
#
|
65
|
+
# @since 2.0.0
|
66
|
+
if BSON::Environment.ruby_18?
|
67
|
+
|
68
|
+
# Making string in 1.8 respond like a 1.9 string, without any modifications.
|
69
|
+
#
|
70
|
+
# @since 2.0.0
|
71
|
+
class String
|
72
|
+
|
73
|
+
# Simply return the string when asking for it's character.
|
74
|
+
#
|
75
|
+
# @since 2.0.0
|
76
|
+
def chr; self; end
|
77
|
+
|
78
|
+
# Force the string to the provided encoding. NOOP.
|
79
|
+
#
|
80
|
+
# @since 2.0.0
|
81
|
+
def force_encoding(*); self; end
|
82
|
+
|
83
|
+
# Encode the string as the provided type. NOOP.
|
84
|
+
#
|
85
|
+
# @since 2.0.0
|
86
|
+
def encode(*); self; end
|
87
|
+
|
88
|
+
# Encode the string in place. NOOP.
|
89
|
+
#
|
90
|
+
# @since 2.0.0
|
91
|
+
def encode!(*); self; end
|
92
|
+
end
|
93
|
+
|
94
|
+
# No encoding error is defined in 1.8.
|
95
|
+
#
|
96
|
+
# @since 2.0.0
|
97
|
+
class EncodingError < RuntimeError; end
|
98
|
+
end
|
@@ -0,0 +1,61 @@
|
|
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 false values to and from
|
18
|
+
# raw bytes as specified by the BSON spec.
|
19
|
+
#
|
20
|
+
# @see http://bsonspec.org/#/specification
|
21
|
+
#
|
22
|
+
# @since 2.0.0
|
23
|
+
module FalseClass
|
24
|
+
|
25
|
+
# A false value in the BSON spec is 0x00.
|
26
|
+
#
|
27
|
+
# @since 2.0.0
|
28
|
+
FALSE_BYTE = 0.chr.force_encoding(BINARY).freeze
|
29
|
+
|
30
|
+
# The BSON type for false values is the general boolean type of 0x08.
|
31
|
+
#
|
32
|
+
# @example Get the bson type.
|
33
|
+
# false.bson_type
|
34
|
+
#
|
35
|
+
# @return [ String ] The character 0x08.
|
36
|
+
#
|
37
|
+
# @since 2.0.0
|
38
|
+
def bson_type
|
39
|
+
Boolean::BSON_TYPE
|
40
|
+
end
|
41
|
+
|
42
|
+
# Get the false boolean as encoded BSON.
|
43
|
+
#
|
44
|
+
# @example Get the false boolean as encoded BSON.
|
45
|
+
# false.to_bson
|
46
|
+
#
|
47
|
+
# @return [ String ] The encoded string.
|
48
|
+
#
|
49
|
+
# @see http://bsonspec.org/#/specification
|
50
|
+
#
|
51
|
+
# @since 2.0.0
|
52
|
+
def to_bson(encoded = ''.force_encoding(BINARY))
|
53
|
+
encoded << FALSE_BYTE
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Enrich the core FalseClass class with this module.
|
58
|
+
#
|
59
|
+
# @since 2.0.0
|
60
|
+
::FalseClass.send(:include, FalseClass)
|
61
|
+
end
|
data/lib/bson/float.rb
ADDED
@@ -0,0 +1,82 @@
|
|
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 floating point values
|
18
|
+
# to 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 Float
|
24
|
+
|
25
|
+
# A floating point is type 0x01 in the BSON spec.
|
26
|
+
#
|
27
|
+
# @since 2.0.0
|
28
|
+
BSON_TYPE = 1.chr.force_encoding(BINARY).freeze
|
29
|
+
|
30
|
+
# The pack directive is for 8 byte floating points.
|
31
|
+
#
|
32
|
+
# @since 2.0.0
|
33
|
+
PACK = "E".freeze
|
34
|
+
|
35
|
+
# Get the floating point as encoded BSON.
|
36
|
+
#
|
37
|
+
# @example Get the floating point as encoded BSON.
|
38
|
+
# 1.221311.to_bson
|
39
|
+
#
|
40
|
+
# @return [ String ] The encoded string.
|
41
|
+
#
|
42
|
+
# @see http://bsonspec.org/#/specification
|
43
|
+
#
|
44
|
+
# @since 2.0.0
|
45
|
+
def to_bson(encoded = ''.force_encoding(BINARY))
|
46
|
+
encoded << [ self ].pack(PACK)
|
47
|
+
end
|
48
|
+
|
49
|
+
module ClassMethods
|
50
|
+
|
51
|
+
# Deserialize an instance of a Float from a BSON double.
|
52
|
+
#
|
53
|
+
# @param [ BSON ] bson The encoded double.
|
54
|
+
#
|
55
|
+
# @return [ Float ] The decoded Float.
|
56
|
+
#
|
57
|
+
# @see http://bsonspec.org/#/specification
|
58
|
+
#
|
59
|
+
# @since 2.0.0
|
60
|
+
def from_bson(bson)
|
61
|
+
from_bson_double(bson.read(8))
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def from_bson_double(double)
|
67
|
+
double.unpack(PACK).first
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Register this type when the module is loaded.
|
72
|
+
#
|
73
|
+
# @since 2.0.0
|
74
|
+
Registry.register(BSON_TYPE, ::Float)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Enrich the core Float class with this module.
|
78
|
+
#
|
79
|
+
# @since 2.0.0
|
80
|
+
::Float.send(:include, Float)
|
81
|
+
::Float.send(:extend, Float::ClassMethods)
|
82
|
+
end
|
data/lib/bson/hash.rb
ADDED
@@ -0,0 +1,84 @@
|
|
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 hashes 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 Hash
|
24
|
+
include Encodable
|
25
|
+
|
26
|
+
# An hash (embedded document) is type 0x03 in the BSON spec.
|
27
|
+
#
|
28
|
+
# @since 2.0.0
|
29
|
+
BSON_TYPE = 3.chr.force_encoding(BINARY).freeze
|
30
|
+
|
31
|
+
# Get the hash as encoded BSON.
|
32
|
+
#
|
33
|
+
# @example Get the hash as encoded BSON.
|
34
|
+
# { "field" => "value" }.to_bson
|
35
|
+
#
|
36
|
+
# @return [ String ] The encoded string.
|
37
|
+
#
|
38
|
+
# @see http://bsonspec.org/#/specification
|
39
|
+
#
|
40
|
+
# @since 2.0.0
|
41
|
+
def to_bson(encoded = ''.force_encoding(BINARY))
|
42
|
+
encode_with_placeholder_and_null(BSON_ADJUST, encoded) do |encoded|
|
43
|
+
each do |field, value|
|
44
|
+
encoded << value.bson_type
|
45
|
+
field.to_bson_key(encoded)
|
46
|
+
value.to_bson(encoded)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
module ClassMethods
|
52
|
+
|
53
|
+
# Deserialize the hash from BSON.
|
54
|
+
#
|
55
|
+
# @param [ IO ] bson The bson representing a hash.
|
56
|
+
#
|
57
|
+
# @return [ Array ] The decoded hash.
|
58
|
+
#
|
59
|
+
# @see http://bsonspec.org/#/specification
|
60
|
+
#
|
61
|
+
# @since 2.0.0
|
62
|
+
def from_bson(bson)
|
63
|
+
hash = new
|
64
|
+
bson.read(4) # Swallow the first four bytes.
|
65
|
+
while (type = bson.readbyte.chr) != NULL_BYTE
|
66
|
+
field = bson.gets(NULL_BYTE).from_bson_string.chop!
|
67
|
+
hash[field] = BSON::Registry.get(type).from_bson(bson)
|
68
|
+
end
|
69
|
+
hash
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Register this type when the module is loaded.
|
74
|
+
#
|
75
|
+
# @since 2.0.0
|
76
|
+
Registry.register(BSON_TYPE, ::Hash)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Enrich the core Hash class with this module.
|
80
|
+
#
|
81
|
+
# @since 2.0.0
|
82
|
+
::Hash.send(:include, Hash)
|
83
|
+
::Hash.send(:extend, Hash::ClassMethods)
|
84
|
+
end
|
data/lib/bson/int32.rb
ADDED
@@ -0,0 +1,59 @@
|
|
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
|
+
# Represents a $maxKey type, which compares less than any other value in the
|
18
|
+
# specification.
|
19
|
+
#
|
20
|
+
# @see http://bsonspec.org/#/specification
|
21
|
+
#
|
22
|
+
# @since 2.0.0
|
23
|
+
class Int32
|
24
|
+
|
25
|
+
# A boolean is type 0x08 in the BSON spec.
|
26
|
+
#
|
27
|
+
# @since 2.0.0
|
28
|
+
BSON_TYPE = 16.chr.force_encoding(BINARY).freeze
|
29
|
+
|
30
|
+
# Constant for the int 32 pack directive.
|
31
|
+
#
|
32
|
+
# @since 2.0.0
|
33
|
+
PACK = Environment.ruby_18? ? "l".freeze : "l<".freeze
|
34
|
+
|
35
|
+
# Deserialize an Integer from BSON.
|
36
|
+
#
|
37
|
+
# @param [ BSON ] bson The encoded int32.
|
38
|
+
#
|
39
|
+
# @return [ Integer ] The decoded Integer.
|
40
|
+
#
|
41
|
+
# @see http://bsonspec.org/#/specification
|
42
|
+
#
|
43
|
+
# @since 2.0.0
|
44
|
+
def self.from_bson(bson)
|
45
|
+
from_bson_int32(bson.read(4))
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def self.from_bson_int32(bytes)
|
51
|
+
bytes.unpack(PACK).first
|
52
|
+
end
|
53
|
+
|
54
|
+
# Register this type when the module is loaded.
|
55
|
+
#
|
56
|
+
# @since 2.0.0
|
57
|
+
Registry.register(BSON_TYPE, self)
|
58
|
+
end
|
59
|
+
end
|