/
hash.rb
96 lines (87 loc) · 2.64 KB
/
hash.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Copyright (C) 2009-2014 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
module BSON
# Injects behaviour for encoding and decoding hashes to
# and from raw bytes as specified by the BSON spec.
#
# @see http://bsonspec.org/#/specification
#
# @since 2.0.0
module Hash
include Encodable
# An hash (embedded document) is type 0x03 in the BSON spec.
#
# @since 2.0.0
BSON_TYPE = 3.chr.force_encoding(BINARY).freeze
# Get the hash as encoded BSON.
#
# @example Get the hash as encoded BSON.
# { "field" => "value" }.to_bson
#
# @return [ String ] The encoded string.
#
# @see http://bsonspec.org/#/specification
#
# @since 2.0.0
def to_bson(encoded = ''.force_encoding(BINARY))
encode_with_placeholder_and_null(BSON_ADJUST, encoded) do |encoded|
each do |field, value|
encoded << value.bson_type
field.to_bson_key(encoded)
value.to_bson(encoded)
end
end
end
# Converts the hash to a normalized value in a BSON document.
#
# @example Convert the hash to a normalized value.
# hash.to_bson_normalized_value
#
# @return [ BSON::Document ] The normazlied hash.
#
# @since 3.0.0
def to_bson_normalized_value
Document.new(self)
end
module ClassMethods
# Deserialize the hash from BSON.
#
# @param [ IO ] bson The bson representing a hash.
#
# @return [ Array ] The decoded hash.
#
# @see http://bsonspec.org/#/specification
#
# @since 2.0.0
def from_bson(bson)
hash = allocate
bson.read(4) # Swallow the first four bytes.
while (type = bson.readbyte.chr) != NULL_BYTE
field = bson.gets(NULL_BYTE).from_bson_string.chop!
hash.store(field, BSON::Registry.get(type).from_bson(bson))
end
hash
end
end
# Register this type when the module is loaded.
#
# @since 2.0.0
Registry.register(BSON_TYPE, ::Hash)
end
# Enrich the core Hash class with this module.
#
# @since 2.0.0
::Hash.send(:include, Hash)
::Hash.send(:extend, Hash::ClassMethods)
end