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
data/lib/bson/int64.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 Int64
|
24
|
+
|
25
|
+
# A boolean is type 0x08 in the BSON spec.
|
26
|
+
#
|
27
|
+
# @since 2.0.0
|
28
|
+
BSON_TYPE = 18.chr.force_encoding(BINARY).freeze
|
29
|
+
|
30
|
+
# Constant for the int 64 pack directive.
|
31
|
+
#
|
32
|
+
# @since 2.0.0
|
33
|
+
PACK = Environment.ruby_18? ? "q".freeze : "q<".freeze
|
34
|
+
|
35
|
+
# Deserialize an Integer from BSON.
|
36
|
+
#
|
37
|
+
# @param [ BSON ] bson The encoded int64.
|
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_int64(bson.read(8))
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def self.from_bson_int64(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
|
data/lib/bson/integer.rb
ADDED
@@ -0,0 +1,185 @@
|
|
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 integer 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 Integer
|
24
|
+
|
25
|
+
# A 32bit integer is type 0x10 in the BSON spec.
|
26
|
+
#
|
27
|
+
# @since 2.0.0
|
28
|
+
INT32_TYPE = 16.chr.force_encoding(BINARY).freeze
|
29
|
+
|
30
|
+
# A 64bit integer is type 0x12 in the BSON spec.
|
31
|
+
#
|
32
|
+
# @since 2.0.0
|
33
|
+
INT64_TYPE = 18.chr.force_encoding(BINARY).freeze
|
34
|
+
|
35
|
+
# The maximum 32 bit integer value.
|
36
|
+
#
|
37
|
+
# @since 2.0.0
|
38
|
+
MAX_32BIT = (1 << 31) - 1
|
39
|
+
|
40
|
+
# The maximum 64 bit integer value.
|
41
|
+
#
|
42
|
+
# @since 2.0.0
|
43
|
+
MAX_64BIT = (1 << 63) - 1
|
44
|
+
|
45
|
+
# The minimum 32 bit integer value.
|
46
|
+
#
|
47
|
+
# @since 2.0.0
|
48
|
+
MIN_32BIT = -(1 << 31)
|
49
|
+
|
50
|
+
# The minimum 64 bit integer value.
|
51
|
+
#
|
52
|
+
# @since 2.0.0
|
53
|
+
MIN_64BIT = -(1 << 63)
|
54
|
+
|
55
|
+
# The BSON index size.
|
56
|
+
#
|
57
|
+
# @since 2.0.0
|
58
|
+
BSON_INDEX_SIZE = 1024.freeze
|
59
|
+
|
60
|
+
# A hash of index values for array optimization.
|
61
|
+
#
|
62
|
+
# @since 2.0.0
|
63
|
+
BSON_ARRAY_INDEXES = ::Array.new(BSON_INDEX_SIZE) do |i|
|
64
|
+
(i.to_s.force_encoding(BINARY) << NULL_BYTE).freeze
|
65
|
+
end.freeze
|
66
|
+
|
67
|
+
# Is this integer a valid BSON 32 bit value?
|
68
|
+
#
|
69
|
+
# @example Is the integer a valid 32 bit value?
|
70
|
+
# 1024.bson_int32?
|
71
|
+
#
|
72
|
+
# @return [ true, false ] If the integer is 32 bit.
|
73
|
+
#
|
74
|
+
# @since 2.0.0
|
75
|
+
def bson_int32?
|
76
|
+
(MIN_32BIT <= self) && (self <= MAX_32BIT)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Is this integer a valid BSON 64 bit value?
|
80
|
+
#
|
81
|
+
# @example Is the integer a valid 64 bit value?
|
82
|
+
# 1024.bson_int64?
|
83
|
+
#
|
84
|
+
# @return [ true, false ] If the integer is 64 bit.
|
85
|
+
#
|
86
|
+
# @since 2.0.0
|
87
|
+
def bson_int64?
|
88
|
+
(MIN_64BIT <= self) && (self <= MAX_64BIT)
|
89
|
+
end
|
90
|
+
|
91
|
+
# Get the BSON type for this integer. Will depend on whether the integer
|
92
|
+
# is 32 bit or 64 bit.
|
93
|
+
#
|
94
|
+
# @example Get the BSON type for the integer.
|
95
|
+
# 1024.bson_type
|
96
|
+
#
|
97
|
+
# @return [ String ] The single byte BSON type.
|
98
|
+
#
|
99
|
+
# @see http://bsonspec.org/#/specification
|
100
|
+
#
|
101
|
+
# @since 2.0.0
|
102
|
+
def bson_type
|
103
|
+
bson_int32? ? INT32_TYPE : (bson_int64? ? INT64_TYPE : out_of_range!)
|
104
|
+
end
|
105
|
+
|
106
|
+
# Get the integer as encoded BSON.
|
107
|
+
#
|
108
|
+
# @example Get the integer as encoded BSON.
|
109
|
+
# 1024.to_bson
|
110
|
+
#
|
111
|
+
# @return [ String ] The encoded string.
|
112
|
+
#
|
113
|
+
# @see http://bsonspec.org/#/specification
|
114
|
+
#
|
115
|
+
# @since 2.0.0
|
116
|
+
def to_bson(encoded = ''.force_encoding(BINARY))
|
117
|
+
if bson_int32?
|
118
|
+
to_bson_int32(encoded)
|
119
|
+
elsif bson_int64?
|
120
|
+
to_bson_int64(encoded)
|
121
|
+
else
|
122
|
+
out_of_range!
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# Convert the integer to a 32 bit (4 bytes) raw bytes string.
|
127
|
+
#
|
128
|
+
# @example Convert the integer to it's 32 bit bytes.
|
129
|
+
# 1024.to_bson_int32
|
130
|
+
#
|
131
|
+
# @param [ String ] encoded The string to encode to.
|
132
|
+
#
|
133
|
+
# @return [ String ] The encoded string.
|
134
|
+
#
|
135
|
+
# @since 2.0.0
|
136
|
+
def to_bson_int32(encoded)
|
137
|
+
append_bson_int32(encoded)
|
138
|
+
end
|
139
|
+
|
140
|
+
# Convert the integer to a 64 bit (8 bytes) raw bytes string.
|
141
|
+
#
|
142
|
+
# @example Convert the integer to it's 64 bit bytes.
|
143
|
+
# 1024.to_bson_int64
|
144
|
+
#
|
145
|
+
# @param [ String ] encoded The string to encode to.
|
146
|
+
#
|
147
|
+
# @return [ String ] The encoded string.
|
148
|
+
#
|
149
|
+
# @since 2.0.0
|
150
|
+
def to_bson_int64(encoded)
|
151
|
+
append_bson_int32(encoded)
|
152
|
+
encoded << ((self >> 32) & 255)
|
153
|
+
encoded << ((self >> 40) & 255)
|
154
|
+
encoded << ((self >> 48) & 255)
|
155
|
+
encoded << ((self >> 56) & 255)
|
156
|
+
end
|
157
|
+
|
158
|
+
def to_bson_key(encoded = ''.force_encoding(BINARY))
|
159
|
+
if self < BSON_INDEX_SIZE
|
160
|
+
encoded << BSON_ARRAY_INDEXES[self]
|
161
|
+
else
|
162
|
+
self.to_s.to_bson_cstring(encoded)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
private
|
167
|
+
|
168
|
+
def append_bson_int32(encoded)
|
169
|
+
encoded << (self & 255)
|
170
|
+
encoded << ((self >> 8) & 255)
|
171
|
+
encoded << ((self >> 16) & 255)
|
172
|
+
encoded << ((self >> 24) & 255)
|
173
|
+
end
|
174
|
+
|
175
|
+
def out_of_range!
|
176
|
+
raise RangeError.new("#{self} is not a valid 8 byte integer value.")
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
# Enrich the core Integer class with this module.
|
182
|
+
#
|
183
|
+
# @since 2.0.0
|
184
|
+
::Integer.send(:include, Integer)
|
185
|
+
end
|
data/lib/bson/json.rb
ADDED
@@ -0,0 +1,37 @@
|
|
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 common behaviour for JSON serialization of objects.
|
18
|
+
#
|
19
|
+
# @since 2.0.0
|
20
|
+
module JSON
|
21
|
+
|
22
|
+
# Converting an object to JSON simply gets it's hash representation via
|
23
|
+
# as_json, then converts it to a string.
|
24
|
+
#
|
25
|
+
# @example Convert the object to JSON
|
26
|
+
# object.to_json
|
27
|
+
#
|
28
|
+
# @note All types must implement as_json.
|
29
|
+
#
|
30
|
+
# @return [ String ] The object as JSON.
|
31
|
+
#
|
32
|
+
# @since 2.0.0
|
33
|
+
def to_json(*args)
|
34
|
+
as_json.to_json(*args)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/bson/max_key.rb
ADDED
@@ -0,0 +1,70 @@
|
|
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 MaxKey
|
24
|
+
include Comparable
|
25
|
+
include JSON
|
26
|
+
include Specialized
|
27
|
+
|
28
|
+
# A $maxKey is type 0x7F in the BSON spec.
|
29
|
+
#
|
30
|
+
# @since 2.0.0
|
31
|
+
BSON_TYPE = 127.chr.force_encoding(BINARY).freeze
|
32
|
+
|
33
|
+
# Constant for always evaluating greater in a comparison.
|
34
|
+
#
|
35
|
+
# @since 2.0.0
|
36
|
+
GREATER = 1.freeze
|
37
|
+
|
38
|
+
# When comparing a max key with any other object, the max key will always
|
39
|
+
# be greater.
|
40
|
+
#
|
41
|
+
# @example Compare with another object.
|
42
|
+
# max_key <=> 1000
|
43
|
+
#
|
44
|
+
# @param [ Object ] The object to compare against.
|
45
|
+
#
|
46
|
+
# @return [ Integer ] Always 1.
|
47
|
+
#
|
48
|
+
# @since 2.0.0
|
49
|
+
def <=>(other)
|
50
|
+
GREATER
|
51
|
+
end
|
52
|
+
|
53
|
+
# Get the max key as JSON hash data.
|
54
|
+
#
|
55
|
+
# @example Get the max key as a JSON hash.
|
56
|
+
# max_key.as_json
|
57
|
+
#
|
58
|
+
# @return [ Hash ] The max key as a JSON hash.
|
59
|
+
#
|
60
|
+
# @since 2.0.0
|
61
|
+
def as_json(*args)
|
62
|
+
{ "$maxKey" => 1 }
|
63
|
+
end
|
64
|
+
|
65
|
+
# Register this type when the module is loaded.
|
66
|
+
#
|
67
|
+
# @since 2.0.0
|
68
|
+
Registry.register(BSON_TYPE, self)
|
69
|
+
end
|
70
|
+
end
|
data/lib/bson/min_key.rb
ADDED
@@ -0,0 +1,70 @@
|
|
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 $minKey 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 MinKey
|
24
|
+
include Comparable
|
25
|
+
include JSON
|
26
|
+
include Specialized
|
27
|
+
|
28
|
+
# A $minKey is type 0xFF in the BSON spec.
|
29
|
+
#
|
30
|
+
# @since 2.0.0
|
31
|
+
BSON_TYPE = 255.chr.force_encoding(BINARY).freeze
|
32
|
+
|
33
|
+
# Constant for always evaluating lesser in a comparison.
|
34
|
+
#
|
35
|
+
# @since 2.0.0
|
36
|
+
LESSER = -1.freeze
|
37
|
+
|
38
|
+
# When comparing a min key with any other object, the min key will always
|
39
|
+
# be lesser.
|
40
|
+
#
|
41
|
+
# @example Compare with another object.
|
42
|
+
# min_key <=> 1000
|
43
|
+
#
|
44
|
+
# @param [ Object ] The object to compare against.
|
45
|
+
#
|
46
|
+
# @return [ Integer ] Always -1.
|
47
|
+
#
|
48
|
+
# @since 2.0.0
|
49
|
+
def <=>(other)
|
50
|
+
LESSER
|
51
|
+
end
|
52
|
+
|
53
|
+
# Get the min key as JSON hash data.
|
54
|
+
#
|
55
|
+
# @example Get the min key as a JSON hash.
|
56
|
+
# min_key.as_json
|
57
|
+
#
|
58
|
+
# @return [ Hash ] The min key as a JSON hash.
|
59
|
+
#
|
60
|
+
# @since 2.0.0
|
61
|
+
def as_json(*args)
|
62
|
+
{ "$minKey" => 1 }
|
63
|
+
end
|
64
|
+
|
65
|
+
# Register this type when the module is loaded.
|
66
|
+
#
|
67
|
+
# @since 2.0.0
|
68
|
+
Registry.register(BSON_TYPE, self)
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,70 @@
|
|
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 nil 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 NilClass
|
24
|
+
|
25
|
+
# A nil is type 0x0A in the BSON spec.
|
26
|
+
#
|
27
|
+
# @since 2.0.0
|
28
|
+
BSON_TYPE = 10.chr.force_encoding(BINARY).freeze
|
29
|
+
|
30
|
+
# Get the nil as encoded BSON.
|
31
|
+
#
|
32
|
+
# @example Get the nil as encoded BSON.
|
33
|
+
# nil.to_bson
|
34
|
+
#
|
35
|
+
# @return [ String ] An empty string.
|
36
|
+
#
|
37
|
+
# @see http://bsonspec.org/#/specification
|
38
|
+
#
|
39
|
+
# @since 2.0.0
|
40
|
+
def to_bson(encoded = ''.force_encoding(BINARY))
|
41
|
+
encoded
|
42
|
+
end
|
43
|
+
|
44
|
+
module ClassMethods
|
45
|
+
# Deserialize NilClass from BSON.
|
46
|
+
#
|
47
|
+
# @param [ BSON ] bson The encoded Null value.
|
48
|
+
#
|
49
|
+
# @return [ nil ] The decoded nil value.
|
50
|
+
#
|
51
|
+
# @see http://bsonspec.org/#/specification
|
52
|
+
#
|
53
|
+
# @since 2.0.0
|
54
|
+
def from_bson(bson)
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Register this type when the module is loaded.
|
60
|
+
#
|
61
|
+
# @since 2.0.0
|
62
|
+
Registry.register(BSON_TYPE, ::NilClass)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Enrich the core NilClass class with this module.
|
66
|
+
#
|
67
|
+
# @since 2.0.0
|
68
|
+
::NilClass.send(:include, NilClass)
|
69
|
+
::NilClass.send(:extend, NilClass::ClassMethods)
|
70
|
+
end
|