bson 4.11.0-java → 4.13.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/README.md +4 -7
  4. data/lib/bson/active_support.rb +1 -0
  5. data/lib/bson/array.rb +2 -1
  6. data/lib/bson/binary.rb +8 -5
  7. data/lib/bson/boolean.rb +2 -1
  8. data/lib/bson/code.rb +2 -1
  9. data/lib/bson/code_with_scope.rb +2 -1
  10. data/lib/bson/config.rb +1 -0
  11. data/lib/bson/date.rb +1 -0
  12. data/lib/bson/date_time.rb +1 -0
  13. data/lib/bson/db_pointer.rb +2 -1
  14. data/lib/bson/dbref.rb +125 -0
  15. data/lib/bson/decimal128/builder.rb +18 -15
  16. data/lib/bson/decimal128.rb +10 -9
  17. data/lib/bson/document.rb +61 -18
  18. data/lib/bson/environment.rb +1 -0
  19. data/lib/bson/error.rb +7 -0
  20. data/lib/bson/ext_json.rb +24 -11
  21. data/lib/bson/false_class.rb +2 -1
  22. data/lib/bson/float.rb +20 -31
  23. data/lib/bson/hash.rb +15 -6
  24. data/lib/bson/int32.rb +3 -2
  25. data/lib/bson/int64.rb +3 -2
  26. data/lib/bson/integer.rb +3 -2
  27. data/lib/bson/json.rb +1 -0
  28. data/lib/bson/max_key.rb +3 -2
  29. data/lib/bson/min_key.rb +3 -2
  30. data/lib/bson/nil_class.rb +2 -1
  31. data/lib/bson/object.rb +1 -0
  32. data/lib/bson/object_id.rb +4 -3
  33. data/lib/bson/open_struct.rb +1 -0
  34. data/lib/bson/regexp.rb +17 -6
  35. data/lib/bson/registry.rb +1 -0
  36. data/lib/bson/specialized.rb +1 -0
  37. data/lib/bson/string.rb +3 -2
  38. data/lib/bson/symbol.rb +2 -1
  39. data/lib/bson/time.rb +4 -3
  40. data/lib/bson/time_with_zone.rb +1 -0
  41. data/lib/bson/timestamp.rb +3 -2
  42. data/lib/bson/true_class.rb +2 -1
  43. data/lib/bson/undefined.rb +2 -1
  44. data/lib/bson/version.rb +2 -1
  45. data/lib/bson-ruby.jar +0 -0
  46. data/lib/bson.rb +6 -4
  47. data/spec/README.md +14 -0
  48. data/spec/bson/binary_spec.rb +1 -1
  49. data/spec/bson/binary_uuid_spec.rb +12 -0
  50. data/spec/bson/byte_buffer_spec.rb +80 -1
  51. data/spec/bson/dbref_spec.rb +461 -0
  52. data/spec/bson/document_as_spec.rb +46 -0
  53. data/spec/bson/document_spec.rb +43 -1
  54. data/spec/bson/ext_json_parse_spec.rb +37 -0
  55. data/spec/bson/hash_as_spec.rb +57 -0
  56. data/spec/bson/int64_spec.rb +4 -24
  57. data/spec/bson/raw_spec.rb +7 -1
  58. data/spec/bson/regexp_spec.rb +52 -0
  59. data/spec/runners/common_driver.rb +1 -1
  60. data/spec/shared/LICENSE +20 -0
  61. data/spec/shared/bin/get-mongodb-download-url +17 -0
  62. data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
  63. data/spec/shared/lib/mrss/cluster_config.rb +221 -0
  64. data/spec/shared/lib/mrss/constraints.rb +346 -0
  65. data/spec/shared/lib/mrss/docker_runner.rb +265 -0
  66. data/spec/shared/lib/mrss/lite_constraints.rb +191 -0
  67. data/spec/shared/lib/mrss/server_version_registry.rb +115 -0
  68. data/spec/shared/lib/mrss/spec_organizer.rb +152 -0
  69. data/spec/shared/lib/mrss/utils.rb +15 -0
  70. data/spec/shared/share/Dockerfile.erb +231 -0
  71. data/spec/shared/shlib/distro.sh +73 -0
  72. data/spec/shared/shlib/server.sh +290 -0
  73. data/spec/shared/shlib/set_env.sh +128 -0
  74. data/spec/spec_helper.rb +13 -0
  75. data/spec/spec_tests/data/corpus/binary.json +33 -0
  76. data/spec/spec_tests/data/corpus/dbref.json +21 -1
  77. data/spec/spec_tests/data/corpus/document.json +4 -0
  78. data/spec/spec_tests/data/corpus/regex.json +2 -2
  79. data/spec/spec_tests/data/corpus/top.json +20 -9
  80. data/spec/support/spec_config.rb +2 -1
  81. data.tar.gz.sig +0 -0
  82. metadata +136 -100
  83. metadata.gz.sig +0 -0
  84. data/lib/bson_native.bundle +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7adb7902f9a5a688e440c021e8db54f373cd49930f1ca26e4be4242978c86dd8
4
- data.tar.gz: f4e42673b1d7becb9d069b5837026b171d3d1884d5548c7a04dc90fbc4e255b1
3
+ metadata.gz: ec2d89585c800c0c61b58f6902381b36273854a1c2a7f8064bfdc50b838cf123
4
+ data.tar.gz: be36daad51a87d3e7fd2d783c8a1e7b5d83ab2c4ea6fe515fd02012d5ad996ec
5
5
  SHA512:
6
- metadata.gz: 97b70aad52dc56049c3776b2348d5f8bf9cea1053fa7dbbad4b05a43677cff6ad0ea6be227601270cc260de21e0d082b3cf950211078b23f16f813779d53e2e0
7
- data.tar.gz: a4b983b9900bdf88d9fab2e188cad8819ba9738a4a8c0da937d42123455b703c3bd03317e248ec18254e407d28ab4c21aac3cf9fd0cfdea4e61d5aa00d165093
6
+ metadata.gz: 79b8c0aa172f5fe850d6b8de145b2e0439a5b07544cfa994a22df322c8f6c88da09b6ca9db7352d0c01fbe9db03546bd9c34a7736a8305d06be992cd68508d9d
7
+ data.tar.gz: 64d250ddbf130ccc8db0405dd251f23277666bab3900a1b685f574757cf05788c88437d8de02d501e117dec951abec5a0a9d457a64209dbff1e0d8c9ab3519a9
checksums.yaml.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  BSON
2
2
  [![Gem Version][rubygems-img]][rubygems-url]
3
- [![Build Status][travis-img]][travis-url]
4
- [![Build status Windows][appveyor-img]][appveyor-url]
3
+ [![Build Status][ghactions-img]][ghactions-url]
5
4
  [![Coverage Status][coveralls-img]][coveralls-url]
6
5
  [![Inline docs][inch-img]][inch-url]
7
6
  ====
@@ -11,7 +10,7 @@ An implementation of the BSON specification in Ruby.
11
10
  Compatibility
12
11
  -------------
13
12
 
14
- BSON is tested against MRI (2.3+) and JRuby (9.2+).
13
+ BSON is tested against MRI (2.5) and JRuby (9.2+).
15
14
 
16
15
  Documentation
17
16
  -------------
@@ -55,10 +54,8 @@ limitations under the License.
55
54
 
56
55
  [rubygems-img]: https://badge.fury.io/rb/bson.svg
57
56
  [rubygems-url]: http://badge.fury.io/rb/bson
58
- [travis-img]: https://secure.travis-ci.org/mongodb/bson-ruby.svg?branch=master
59
- [travis-url]: http://travis-ci.org/mongodb/bson-ruby
60
- [appveyor-img]: https://ci.appveyor.com/api/projects/status/p5aqko7umsx351nm?svg=true
61
- [appveyor-url]: https://ci.appveyor.com/project/p-mongo/bson-ruby/branch/master
57
+ [ghactions-img]: https://github.com/mongodb/bson-ruby/actions/workflows/bson-ruby.yml/badge.svg?query=branch%3Amaster
58
+ [ghactions-url]: https://github.com/mongodb/bson-ruby/actions/workflows/bson-ruby.yml?query=branch%3Amaster
62
59
  [coveralls-img]: https://coveralls.io/repos/mongodb/bson-ruby/badge.svg?branch=master
63
60
  [coveralls-url]: https://coveralls.io/r/mongodb/bson-ruby?branch=master
64
61
  [inch-img]: http://inch-ci.org/github/mongodb/bson-ruby.svg?branch=master
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2018-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
data/lib/bson/array.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,7 +26,7 @@ module BSON
25
26
  # An array is type 0x04 in the BSON spec.
26
27
  #
27
28
  # @since 2.0.0
28
- BSON_TYPE = 4.chr.force_encoding(BINARY).freeze
29
+ BSON_TYPE = ::String.new(4.chr, encoding: BINARY).freeze
29
30
 
30
31
  # Get the array as encoded BSON.
31
32
  #
data/lib/bson/binary.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -27,7 +28,7 @@ module BSON
27
28
  # A binary is type 0x05 in the BSON spec.
28
29
  #
29
30
  # @since 2.0.0
30
- BSON_TYPE = 5.chr.force_encoding(BINARY).freeze
31
+ BSON_TYPE = ::String.new(5.chr, encoding: BINARY).freeze
31
32
 
32
33
  # The mappings of subtypes to their single byte identifiers.
33
34
  #
@@ -46,7 +47,8 @@ module BSON
46
47
  :uuid => 4.chr,
47
48
  :md5 => 5.chr,
48
49
  :ciphertext => 6.chr,
49
- :user => 128.chr
50
+ :column => 7.chr,
51
+ :user => 128.chr,
50
52
  }.freeze
51
53
 
52
54
  # The mappings of single byte subtypes to their symbol counterparts.
@@ -169,7 +171,7 @@ module BSON
169
171
  #
170
172
  # @since 2.3.0
171
173
  def inspect
172
- "<BSON::Binary:0x#{object_id} type=#{type} data=0x#{data[0, 8].unpack('H*').first}...>"
174
+ "<BSON::Binary:0x#{object_id} type=#{type} data=0x#{data[0, 8].unpack1('H*')}...>"
173
175
  end
174
176
 
175
177
  # Returns a string representation of the UUID stored in this Binary.
@@ -207,7 +209,8 @@ module BSON
207
209
  if representation && representation != :standard
208
210
  raise ArgumentError, "Binary of type :uuid can only be stringified to :standard representation, requested: #{representation.inspect}"
209
211
  end
210
- data.split('').map { |n| '%02x' % n.ord }.join.sub(/(.{8})(.{4})(.{4})(.{12})/, '\1-\2-\3-\4')
212
+
213
+ data.split('').map { |n| '%02x' % n.ord }.join.sub(/\A(.{8})(.{4})(.{4})(.{4})(.{12})\z/, '\1-\2-\3-\4-\5')
211
214
  when :uuid_old
212
215
  if representation.nil?
213
216
  raise ArgumentError, 'Representation must be specified for BSON::Binary objects of type :uuid_old'
@@ -229,7 +232,7 @@ module BSON
229
232
  hex
230
233
  else
231
234
  raise ArgumentError, "Invalid representation: #{representation}"
232
- end.sub(/(.{8})(.{4})(.{4})(.{12})/, '\1-\2-\3-\4')
235
+ end.sub(/\A(.{8})(.{4})(.{4})(.{4})(.{12})\z/, '\1-\2-\3-\4-\5')
233
236
  else
234
237
  raise TypeError, "The type of Binary must be :uuid or :uuid_old, this object is: #{type.inspect}"
235
238
  end
data/lib/bson/boolean.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,7 +26,7 @@ module BSON
25
26
  # A boolean is type 0x08 in the BSON spec.
26
27
  #
27
28
  # @since 2.0.0
28
- BSON_TYPE = 8.chr.force_encoding(BINARY).freeze
29
+ BSON_TYPE = ::String.new(8.chr, encoding: BINARY).freeze
29
30
 
30
31
  # Deserialize a boolean from BSON.
31
32
  #
data/lib/bson/code.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,7 +26,7 @@ module BSON
25
26
  # A code is type 0x0D in the BSON spec.
26
27
  #
27
28
  # @since 2.0.0
28
- BSON_TYPE = 13.chr.force_encoding(BINARY).freeze
29
+ BSON_TYPE = ::String.new(13.chr, encoding: BINARY).freeze
29
30
 
30
31
  # @!attribute javascript
31
32
  # @return [ String ] The javascript code.
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -26,7 +27,7 @@ module BSON
26
27
  # A code with scope is type 0x0F in the BSON spec.
27
28
  #
28
29
  # @since 2.0.0
29
- BSON_TYPE = 15.chr.force_encoding(BINARY).freeze
30
+ BSON_TYPE = ::String.new(15.chr, encoding: BINARY).freeze
30
31
 
31
32
  # @!attribute javascript
32
33
  # @return [ String ] The javascript code.
data/lib/bson/config.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2016-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
data/lib/bson/date.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,7 +23,7 @@ module BSON
22
23
  include JSON
23
24
 
24
25
  # A DBPointer is type 0x0C in the BSON spec.
25
- BSON_TYPE = 0x0C.chr.force_encoding(BINARY).freeze
26
+ BSON_TYPE = ::String.new(0x0C.chr, encoding: BINARY).freeze
26
27
 
27
28
  # Create a new DBPointer object.
28
29
  #
data/lib/bson/dbref.rb ADDED
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+ # encoding: utf-8
3
+
4
+ # Copyright (C) 2015-2021 MongoDB Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the 'License');
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an 'AS IS' BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ module BSON
19
+
20
+ # Represents a DBRef document in the database.
21
+ class DBRef < Document
22
+ include JSON
23
+
24
+ # The constant for the collection reference field.
25
+ #
26
+ # @deprecated
27
+ COLLECTION = '$ref'.freeze
28
+
29
+ # The constant for the id field.
30
+ #
31
+ # @deprecated
32
+ ID = '$id'.freeze
33
+
34
+ # The constant for the database field.
35
+ #
36
+ # @deprecated
37
+ DATABASE = '$db'.freeze
38
+
39
+ # @return [ String ] collection The collection name.
40
+ def collection
41
+ self['$ref']
42
+ end
43
+
44
+ # @return [ BSON::ObjectId ] id The referenced document id.
45
+ def id
46
+ self['$id']
47
+ end
48
+
49
+ # @return [ String ] database The database name.
50
+ def database
51
+ self['$db']
52
+ end
53
+
54
+ # Get the DBRef as a JSON document
55
+ #
56
+ # @example Get the DBRef as a JSON hash.
57
+ # dbref.as_json
58
+ #
59
+ # @return [ Hash ] The max key as a JSON hash.
60
+ def as_json(*args)
61
+ {}.update(self)
62
+ end
63
+
64
+ # Instantiate a new DBRef.
65
+ #
66
+ # @example Create the DBRef.
67
+ # BSON::DBRef.new({'$ref' => 'users', '$id' => id, '$db' => 'database'})
68
+ #
69
+ # @param [ Hash ] hash the DBRef hash. It must contain $ref and $id.
70
+ def initialize(hash)
71
+ hash = reorder_fields(hash)
72
+ %w($ref $id).each do |key|
73
+ unless hash[key]
74
+ raise ArgumentError, "DBRef must have #{key}: #{hash}"
75
+ end
76
+ end
77
+
78
+ unless hash['$ref'].is_a?(String)
79
+ raise ArgumentError, "The value for key $ref must be a string, got: #{hash['$ref']}"
80
+ end
81
+
82
+ if db = hash['$db']
83
+ unless db.is_a?(String)
84
+ raise ArgumentError, "The value for key $db must be a string, got: #{hash['$db']}"
85
+ end
86
+ end
87
+
88
+ super
89
+ end
90
+
91
+ # Converts the DBRef to raw BSON.
92
+ #
93
+ # @example Convert the DBRef to raw BSON.
94
+ # dbref.to_bson
95
+ #
96
+ # @param [ BSON::ByteBuffer ] buffer The encoded BSON buffer to append to.
97
+ # @param [ true, false ] validating_keys Whether keys should be validated when serializing.
98
+ #
99
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
100
+ def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
101
+ as_json.to_bson(buffer, validating_keys)
102
+ end
103
+
104
+ private
105
+
106
+ # Reorder the fields of the given Hash to have $ref first, $id second,
107
+ # and $db third. The rest of the fields in the hash can come in any
108
+ # order after that.
109
+ #
110
+ # @param [ Hash ] hash The input hash. Must be a valid dbref.
111
+ #
112
+ # @return [ Hash ] The hash with it's fields reordered.
113
+ def reorder_fields(hash)
114
+ hash = BSON::Document.new(hash)
115
+ reordered = {}
116
+ reordered['$ref'] = hash.delete('$ref')
117
+ reordered['$id'] = hash.delete('$id')
118
+ if db = hash.delete('$db')
119
+ reordered['$db'] = db
120
+ end
121
+
122
+ reordered.update(hash)
123
+ end
124
+ end
125
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2016-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -26,27 +27,27 @@ module BSON
26
27
  # Infinity mask.
27
28
  #
28
29
  # @since 4.2.0
29
- INFINITY_MASK = 0x7800000000000000.freeze
30
+ INFINITY_MASK = 0x7800000000000000
30
31
 
31
32
  # NaN mask.
32
33
  #
33
34
  # @since 4.2.0
34
- NAN_MASK = 0x7c00000000000000.freeze
35
+ NAN_MASK = 0x7c00000000000000
35
36
 
36
37
  # SNaN mask.
37
38
  #
38
39
  # @since 4.2.0
39
- SNAN_MASK = (1 << 57).freeze
40
+ SNAN_MASK = (1 << 57)
40
41
 
41
42
  # Signed bit mask.
42
43
  #
43
44
  # @since 4.2.0
44
- SIGN_BIT_MASK = (1 << 63).freeze
45
+ SIGN_BIT_MASK = (1 << 63)
45
46
 
46
47
  # The two highest bits of the 64 high order bits.
47
48
  #
48
49
  # @since 4.2.0
49
- TWO_HIGHEST_BITS_SET = (3 << 61).freeze
50
+ TWO_HIGHEST_BITS_SET = (3 << 61)
50
51
 
51
52
  extend self
52
53
 
@@ -109,14 +110,14 @@ module BSON
109
110
  # @return [ Regex ] A regex matching a NaN string.
110
111
  #
111
112
  # @since 4.2.0
112
- NAN_REGEX = /^(\-)?(S)?NaN$/i.freeze
113
+ NAN_REGEX = /^(\-)?(S)?NaN$/i
113
114
 
114
115
  # Regex matching a string representing positive or negative Infinity.
115
116
  #
116
117
  # @return [ Regex ] A regex matching a positive or negative Infinity string.
117
118
  #
118
119
  # @since 4.2.0
119
- INFINITY_REGEX = /^(\+|\-)?Inf(inity)?$/i.freeze
120
+ INFINITY_REGEX = /^(\+|\-)?Inf(inity)?$/i
120
121
 
121
122
  # Regex for the fraction, including leading zeros.
122
123
  #
@@ -124,33 +125,33 @@ module BSON
124
125
  # including leading zeros.
125
126
  #
126
127
  # @since 4.2.0
127
- SIGNIFICAND_WITH_LEADING_ZEROS_REGEX = /(0*)(\d+)/.freeze
128
+ SIGNIFICAND_WITH_LEADING_ZEROS_REGEX = /(0*)(\d+)/
128
129
 
129
130
  # Regex for separating a negative sign from the significands.
130
131
  #
131
132
  # @return [ Regex ] The regex for separating a sign from significands.
132
133
  #
133
134
  # @since 4.2.0
134
- SIGN_AND_DIGITS_REGEX = /^(\-)?(\S+)/.freeze
135
+ SIGN_AND_DIGITS_REGEX = /^(\-)?(\S+)/
135
136
 
136
137
  # Regex matching a scientific exponent.
137
138
  #
138
139
  # @return [ Regex ] A regex matching E, e, E+, e+.
139
140
  #
140
141
  # @since 4.2.0
141
- SCIENTIFIC_EXPONENT_REGEX = /E\+?/i.freeze
142
+ SCIENTIFIC_EXPONENT_REGEX = /E\+?/i
142
143
 
143
144
  # Regex for capturing trailing zeros.
144
145
  #
145
146
  # @since 4.2.0
146
- TRAILING_ZEROS_REGEX = /[1-9]*(0+)$/.freeze
147
+ TRAILING_ZEROS_REGEX = /[1-9]*(0+)$/
147
148
 
148
149
  # Regex for a valid decimal128 string format.
149
150
  #
150
151
  # @return [ Regex ] The regex for a valid decimal128 string.
151
152
  #
152
153
  # @since 4.2.0
153
- VALID_DECIMAL128_STRING_REGEX = /^[\-\+]?(\d+(\.\d*)?|\.\d+)(E[\-\+]?\d+)?$/i.freeze
154
+ VALID_DECIMAL128_STRING_REGEX = /^[\-\+]?(\d+(\.\d*)?|\.\d+)(E[\-\+]?\d+)?$/i
154
155
 
155
156
  # Initialize the FromString Builder object.
156
157
  #
@@ -337,14 +338,14 @@ module BSON
337
338
  # @return [ String ] The string representing NaN.
338
339
  #
339
340
  # @since 4.2.0
340
- NAN_STRING = 'NaN'.freeze
341
+ NAN_STRING = 'NaN'
341
342
 
342
343
  # String representing an Infinity value.
343
344
  #
344
345
  # @return [ String ] The string representing Infinity.
345
346
  #
346
347
  # @since 4.2.0
347
- INFINITY_STRING = 'Infinity'.freeze
348
+ INFINITY_STRING = 'Infinity'
348
349
 
349
350
  # Initialize the FromBigDecimal Builder object.
350
351
  #
@@ -366,11 +367,13 @@ module BSON
366
367
  #
367
368
  # @return [ String ] The string representing the decimal128 object.
368
369
  #
370
+ # @note The returned string may be frozen.
371
+ #
369
372
  # @since 4.2.0
370
373
  def string
371
374
  return NAN_STRING if nan?
372
375
  str = infinity? ? INFINITY_STRING : create_string
373
- negative? ? '-' << str : str
376
+ negative? ? "-#{str}" : str
374
377
  end
375
378
 
376
379
  private
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2016-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -23,32 +24,32 @@ module BSON
23
24
  # A Decimal128 is type 0x13 in the BSON spec.
24
25
  #
25
26
  # @since 4.2.0
26
- BSON_TYPE = 19.chr.force_encoding(BINARY).freeze
27
+ BSON_TYPE = ::String.new(19.chr, encoding: BINARY).freeze
27
28
 
28
29
  # Exponent offset.
29
30
  #
30
31
  # @since 4.2.0
31
- EXPONENT_OFFSET = 6176.freeze
32
+ EXPONENT_OFFSET = 6176
32
33
 
33
34
  # Minimum exponent.
34
35
  #
35
36
  # @since 4.2.0
36
- MIN_EXPONENT = -6176.freeze
37
+ MIN_EXPONENT = -6176
37
38
 
38
39
  # Maximum exponent.
39
40
  #
40
41
  # @since 4.2.0
41
- MAX_EXPONENT = 6111.freeze
42
+ MAX_EXPONENT = 6111
42
43
 
43
44
  # Maximum digits of precision.
44
45
  #
45
46
  # @since 4.2.0
46
- MAX_DIGITS_OF_PRECISION = 34.freeze
47
+ MAX_DIGITS_OF_PRECISION = 34
47
48
 
48
49
  # Key for this type when converted to extended json.
49
50
  #
50
51
  # @since 4.2.0
51
- EXTENDED_JSON_KEY = "$numberDecimal".freeze
52
+ EXTENDED_JSON_KEY = "$numberDecimal"
52
53
 
53
54
  # The native type to which this object can be converted.
54
55
  #
@@ -262,7 +263,7 @@ module BSON
262
263
  # The custom error message for this error.
263
264
  #
264
265
  # @since 4.2.0
265
- MESSAGE = 'A Decimal128 can only be created from a String or BigDecimal.'.freeze
266
+ MESSAGE = 'A Decimal128 can only be created from a String or BigDecimal.'
266
267
 
267
268
  # Get the custom error message for the exception.
268
269
  #
@@ -288,7 +289,7 @@ module BSON
288
289
  # The custom error message for this error.
289
290
  #
290
291
  # @since 4.2.0
291
- MESSAGE = 'Invalid string format for creating a Decimal128 object.'.freeze
292
+ MESSAGE = 'Invalid string format for creating a Decimal128 object.'
292
293
 
293
294
  # Get the custom error message for the exception.
294
295
  #
@@ -313,7 +314,7 @@ module BSON
313
314
  # The custom error message for this error.
314
315
  #
315
316
  # @since 4.2.0
316
- MESSAGE = 'Value out of range for Decimal128 representation.'.freeze
317
+ MESSAGE = 'Value out of range for Decimal128 representation.'
317
318
 
318
319
  # Get the custom error message for the exception.
319
320
  #
data/lib/bson/document.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -269,27 +270,69 @@ module BSON
269
270
  end
270
271
  end
271
272
 
272
- if instance_methods.include?(:slice)
273
- # Slices a document to include only the given keys.
274
- # Will normalize symbol keys into strings.
275
- # (this method is backported from ActiveSupport::Hash)
276
- #
277
- # @example Get a document/hash with only the `name` and `age` fields present
278
- # document # => { _id: <ObjectId>, :name => 'John', :age => 30, :location => 'Earth' }
279
- # document.slice(:name, 'age') # => { name: 'John', age: 30 }
280
- # document.slice('name') # => { name: 'John' }
281
- # document.slice(:foo) # => nil
282
- #
283
- # @param [ Array<String, Symbol> ] *keys Keys, that will be kept in the resulting document
284
- #
285
- # @return [ BSON::Document ] The document with only the selected keys
286
- #
287
- # @since 4.3.1
288
- def slice(*keys)
289
- super(*keys.map{|key| convert_key(key)})
273
+ # Slices a document to include only the given keys.
274
+ # Will normalize symbol keys into strings.
275
+ # (this method is backported from ActiveSupport::Hash)
276
+ #
277
+ # @example Get a document/hash with only the `name` and `age` fields present
278
+ # document # => { _id: <ObjectId>, :name => "John", :age => 30, :location => "Earth" }
279
+ # document.slice(:name, 'age') # => { "name": "John", "age" => 30 }
280
+ # document.slice('name') # => { "name" => "John" }
281
+ # document.slice(:foo) # => {}
282
+ #
283
+ # @param [ Array<String, Symbol> ] *keys Keys, that will be kept in the resulting document
284
+ #
285
+ # @return [ BSON::Document ] The document with only the selected keys
286
+ #
287
+ # @since 4.3.1
288
+ def slice(*keys)
289
+ keys.each_with_object(self.class.new) do |key, hash|
290
+ if key?(key)
291
+ hash[key] = self[key]
292
+ end
290
293
  end
291
294
  end
292
295
 
296
+ # Returns a new document consisting of the current document minus the
297
+ # specified keys.
298
+ #
299
+ # The keys to be removed can be specified as either strings or symbols.
300
+ #
301
+ # @example Get a document/hash with only the `name` and `age` fields removed
302
+ # document # => { _id: <ObjectId>, :name => 'John', :age => 30, :location => 'Earth' }
303
+ # document.except(:name, 'age') # => { _id: <ObjectId>, location: 'Earth' }
304
+ #
305
+ # @param [ Array<String, Symbol> ] *keys Keys, that will be removed in the resulting document
306
+ #
307
+ # @return [ BSON::Document ] The document with the specified keys removed.
308
+ #
309
+ # @note This method is always defined, even if Hash already contains a
310
+ # definition of #except, because ActiveSupport unconditionally defines
311
+ # its version of #except which doesn't work for BSON::Document which
312
+ # causes problems if ActiveSupport is loaded after bson-ruby is.
313
+ def except(*keys)
314
+ copy = dup
315
+ keys.each {|key| copy.delete(key)}
316
+ copy
317
+ end
318
+
319
+ def symbolize_keys!
320
+ raise ArgumentError, 'symbolize_keys! is not supported on BSON::Document instances. Please convert the document to hash first (using #to_h), then call #symbolize_keys! on the Hash instance'
321
+ end
322
+
323
+ # Override the Hash implementation of to_bson_normalized_value.
324
+ #
325
+ # BSON::Document is already of the correct type and already provides
326
+ # indifferent access to keys, hence no further conversions are necessary.
327
+ #
328
+ # Attempting to perform Hash's conversion on Document instances converts
329
+ # DBRefs to Documents which is wrong.
330
+ #
331
+ # @return [ BSON::Document ] The normalized hash.
332
+ def to_bson_normalized_value
333
+ self
334
+ end
335
+
293
336
  private
294
337
 
295
338
  def convert_key(key)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
data/lib/bson/error.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module BSON
2
3
  # Base exception class for all BSON-related errors.
3
4
  #
@@ -23,5 +24,11 @@ module BSON
23
24
  # itself to BSON.
24
25
  class UnserializableClass < Error
25
26
  end
27
+
28
+ # Exception raised when there is an invalid argument passed into the
29
+ # constructor of regexp object. This includes when the argument contains
30
+ # a null byte.
31
+ class InvalidRegexpPattern < Error
32
+ end
26
33
  end
27
34
  end