-
Notifications
You must be signed in to change notification settings - Fork 105
/
int32.rb
46 lines (39 loc) · 983 Bytes
/
int32.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# encoding: utf-8
module BSON
# Represents a $maxKey type, which compares less than any other value in the
# specification.
#
# @see http://bsonspec.org/#/specification
#
# @since 2.0.0
class Int32
# A boolean is type 0x08 in the BSON spec.
#
# @since 2.0.0
BSON_TYPE = 16.chr.force_encoding(BINARY).freeze
# Constant for the int 32 pack directive.
#
# @since 2.0.0
PACK = Environment.ruby_18? ? "l".freeze : "l<".freeze
# Deserialize an Integer from BSON.
#
# @param [ BSON ] bson The encoded int32.
#
# @return [ Integer ] The decoded Integer.
#
# @see http://bsonspec.org/#/specification
#
# @since 2.0.0
def self.from_bson(bson)
from_bson_int32(bson.read(4))
end
private
def self.from_bson_int32(bytes)
bytes.unpack(PACK).first
end
# Register this type when the module is loaded.
#
# @since 2.0.0
Registry.register(BSON_TYPE, self)
end
end