bson 2.0.0.rc-java → 2.1.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.

Files changed (73) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/CHANGELOG.md +9 -0
  5. data/LICENSE +1 -1
  6. data/NOTICE +1 -1
  7. data/README.md +7 -1
  8. data/Rakefile +25 -8
  9. data/lib/bson-ruby.jar +0 -0
  10. data/lib/bson.rb +3 -1
  11. data/lib/bson/array.rb +18 -1
  12. data/lib/bson/binary.rb +56 -1
  13. data/lib/bson/boolean.rb +1 -1
  14. data/lib/bson/code.rb +1 -1
  15. data/lib/bson/code_with_scope.rb +7 -5
  16. data/lib/bson/date.rb +44 -0
  17. data/lib/bson/date_time.rb +65 -0
  18. data/lib/bson/document.rb +1 -1
  19. data/lib/bson/encodable.rb +1 -1
  20. data/lib/bson/environment.rb +13 -1
  21. data/lib/bson/false_class.rb +1 -1
  22. data/lib/bson/float.rb +1 -1
  23. data/lib/bson/hash.rb +3 -3
  24. data/lib/bson/int32.rb +1 -1
  25. data/lib/bson/int64.rb +1 -1
  26. data/lib/bson/integer.rb +1 -1
  27. data/lib/bson/json.rb +1 -3
  28. data/lib/bson/max_key.rb +1 -1
  29. data/lib/bson/min_key.rb +1 -1
  30. data/lib/bson/nil_class.rb +1 -1
  31. data/lib/bson/object_id.rb +79 -6
  32. data/lib/bson/regexp.rb +1 -1
  33. data/lib/bson/registry.rb +1 -1
  34. data/lib/bson/specialized.rb +1 -1
  35. data/lib/bson/string.rb +20 -3
  36. data/lib/bson/symbol.rb +2 -2
  37. data/lib/bson/time.rb +1 -1
  38. data/lib/bson/timestamp.rb +1 -1
  39. data/lib/bson/true_class.rb +1 -1
  40. data/lib/bson/undefined.rb +1 -1
  41. data/lib/bson/version.rb +2 -2
  42. data/spec/bson/array_spec.rb +28 -1
  43. data/spec/bson/binary_spec.rb +16 -1
  44. data/spec/bson/boolean_spec.rb +1 -1
  45. data/spec/bson/code_spec.rb +1 -1
  46. data/spec/bson/code_with_scope_spec.rb +34 -7
  47. data/spec/bson/date_spec.rb +39 -0
  48. data/spec/bson/date_time_spec.rb +37 -0
  49. data/spec/bson/document_spec.rb +1 -1
  50. data/spec/bson/false_class_spec.rb +1 -1
  51. data/spec/bson/float_spec.rb +1 -1
  52. data/spec/bson/hash_spec.rb +1 -1
  53. data/spec/bson/int32_spec.rb +1 -1
  54. data/spec/bson/int64_spec.rb +1 -1
  55. data/spec/bson/integer_spec.rb +1 -1
  56. data/spec/bson/json_spec.rb +1 -1
  57. data/spec/bson/max_key_spec.rb +1 -1
  58. data/spec/bson/min_key_spec.rb +1 -1
  59. data/spec/bson/nil_class_spec.rb +1 -1
  60. data/spec/bson/object_id_spec.rb +110 -1
  61. data/spec/bson/regexp_spec.rb +1 -1
  62. data/spec/bson/registry_spec.rb +1 -1
  63. data/spec/bson/string_spec.rb +29 -2
  64. data/spec/bson/symbol_spec.rb +11 -1
  65. data/spec/bson/time_spec.rb +1 -1
  66. data/spec/bson/timestamp_spec.rb +1 -1
  67. data/spec/bson/true_class_spec.rb +1 -1
  68. data/spec/bson/undefined_spec.rb +1 -1
  69. data/spec/bson_spec.rb +1 -1
  70. data/spec/spec_helper.rb +3 -2
  71. data/spec/support/shared_examples.rb +1 -1
  72. metadata +16 -12
  73. metadata.gz.sig +0 -0
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -31,7 +31,7 @@ module BSON
31
31
  # Get the hash as encoded BSON.
32
32
  #
33
33
  # @example Get the hash as encoded BSON.
34
- # { field: "value" }.to_bson
34
+ # { "field" => "value" }.to_bson
35
35
  #
36
36
  # @return [ String ] The encoded string.
37
37
  #
@@ -52,7 +52,7 @@ module BSON
52
52
 
53
53
  # Deserialize the hash from BSON.
54
54
  #
55
- # @param [ String ] bson The bson representing a hash.
55
+ # @param [ IO ] bson The bson representing a hash.
56
56
  #
57
57
  # @return [ Array ] The decoded hash.
58
58
  #
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -12,8 +12,6 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require "json"
16
-
17
15
  module BSON
18
16
 
19
17
  # Provides common behaviour for JSON serialization of objects.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@ module BSON
46
46
  return false unless other.is_a?(ObjectId)
47
47
  to_bson == other.to_bson
48
48
  end
49
+ alias :eql? :==
49
50
 
50
51
  # Check case equality on the object id.
51
52
  #
@@ -102,12 +103,62 @@ module BSON
102
103
  ::Time.at(to_bson.unpack("N")[0]).utc
103
104
  end
104
105
 
106
+ # Get the hash value for the object id.
107
+ #
108
+ # @example Get the hash value.
109
+ # object_id.hash
110
+ #
111
+ # @return [ Integer ] The hash value.
112
+ #
113
+ # @since 2.0.0
114
+ def hash
115
+ to_bson.hash
116
+ end
117
+
118
+ # Get a nice string for use with object inspection.
119
+ #
120
+ # @example Inspect the object id.
121
+ # obhect_id.inspect
122
+ #
123
+ # @return [ String ] The object id in form BSON::ObjectId('id')
124
+ #
125
+ # @since 2.0.0
126
+ def inspect
127
+ "BSON::ObjectId('#{to_s}')"
128
+ end
129
+
130
+ # Dump the raw bson when calling Marshal.dump.
131
+ #
132
+ # @example Dump the raw bson.
133
+ # Marshal.dump(object_id)
134
+ #
135
+ # @return [ String ] The raw bson bytes.
136
+ #
137
+ # @since 2.0.0
138
+ def marshal_dump
139
+ to_bson
140
+ end
141
+
142
+ # Unmarshal the data into an object id.
143
+ #
144
+ # @example Unmarshal the data.
145
+ # Marshal.load(data)
146
+ #
147
+ # @param [ String ] data The raw bson bytes.
148
+ #
149
+ # @return [ String ] The raw bson bytes.
150
+ #
151
+ # @since 2.0.0
152
+ def marshal_load(data)
153
+ @raw_data = data
154
+ end
155
+
105
156
  # Get the object id as it's raw BSON data.
106
157
  #
107
158
  # @example Get the raw bson bytes.
108
159
  # object_id.to_bson
109
160
  #
110
- # @note Since Moped's BSON and 10gen BSON before 2.0.0 have different
161
+ # @note Since Moped's BSON and MongoDB BSON before 2.0.0 have different
111
162
  # internal representations, we will attempt to repair the data for cases
112
163
  # where the object was instantiated in a non-standard way. (Like a
113
164
  # Marshal.load)
@@ -118,7 +169,7 @@ module BSON
118
169
  #
119
170
  # @since 2.0.0
120
171
  def to_bson(encoded = ''.force_encoding(BINARY))
121
- repair!(@data) if defined?(@data)
172
+ repair if defined?(@data)
122
173
  @raw_data ||= @@generator.next
123
174
  encoded << @raw_data
124
175
  end
@@ -143,8 +194,9 @@ module BSON
143
194
 
144
195
  private
145
196
 
146
- def data=(data)
147
- @raw_data = data
197
+ def repair
198
+ @raw_data = @data.to_bson_object_id
199
+ remove_instance_variable(:@data)
148
200
  end
149
201
 
150
202
  class << self
@@ -175,7 +227,7 @@ module BSON
175
227
  # @since 2.0.0
176
228
  def from_data(data)
177
229
  object_id = allocate
178
- object_id.send(:data=, data)
230
+ object_id.instance_variable_set(:@raw_data, data)
179
231
  object_id
180
232
  end
181
233
 
@@ -232,6 +284,27 @@ module BSON
232
284
  def legal?(string)
233
285
  string.to_s =~ /^[0-9a-f]{24}$/i ? true : false
234
286
  end
287
+
288
+ # Executes the provided block only if the size of the provided object is
289
+ # 12. Used in legacy id repairs.
290
+ #
291
+ # @example Execute in a repairing block.
292
+ # BSON::ObjectId.repair("test") { obj }
293
+ #
294
+ # @param [ String, Array ] object The object to repair.
295
+ #
296
+ # @raise [ Invalid ] If the array is not 12 elements.
297
+ #
298
+ # @return [ String ] The result of the block.
299
+ #
300
+ # @since 2.0.0
301
+ def repair(object)
302
+ if object.size == 12
303
+ block_given? ? yield(object) : object
304
+ else
305
+ raise Invalid.new("#{object.inspect} is not a valid object id.")
306
+ end
307
+ end
235
308
  end
236
309
 
237
310
  # Inner class that encapsulates the behaviour of actually generating each
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,5 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
4
5
  # you may not use this file except in compliance with the License.
@@ -59,7 +60,6 @@ module BSON
59
60
  #
60
61
  # @since 2.0.0
61
62
  def to_bson_key(encoded = ''.force_encoding(BINARY))
62
- check_for_illegal_characters!
63
63
  to_bson_cstring(encoded)
64
64
  end
65
65
 
@@ -80,6 +80,23 @@ module BSON
80
80
  to_bson_string(encoded) << NULL_BYTE
81
81
  end
82
82
 
83
+ # Convert the string to an object id. This will only work for strings of size
84
+ # 12.
85
+ #
86
+ # @example Convert the string to an object id.
87
+ # string.to_bson_object_id
88
+ #
89
+ # @note This is used for repairing legacy bson data.
90
+ #
91
+ # @raise [ InvalidObjectId ] If the string is not 12 elements.
92
+ #
93
+ # @return [ String ] The raw object id bytes.
94
+ #
95
+ # @since 2.0.0
96
+ def to_bson_object_id
97
+ ObjectId.repair(self)
98
+ end
99
+
83
100
  # Convert the string to a UTF-8 string then force to binary. This is so
84
101
  # we get errors for strings that are not UTF-8 encoded.
85
102
  #
@@ -168,7 +185,7 @@ module BSON
168
185
 
169
186
  def check_for_illegal_characters!
170
187
  if include?(NULL_BYTE)
171
- raise RuntimeError.new("Illegal C-String '#{self}' contains a null byte.")
188
+ raise(ArgumentError, "Illegal C-String '#{self}' contains a null byte.")
172
189
  end
173
190
  end
174
191
 
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -55,7 +55,7 @@ module BSON
55
55
  #
56
56
  # @since 2.0.0
57
57
  def to_bson_key(encoded = ''.force_encoding(BINARY))
58
- to_s.to_bson_cstring(encoded)
58
+ to_s.to_bson_key(encoded)
59
59
  end
60
60
 
61
61
  module ClassMethods
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -13,5 +13,5 @@
13
13
  # limitations under the License.
14
14
 
15
15
  module BSON
16
- VERSION = "2.0.0.rc"
16
+ VERSION = "2.1.0"
17
17
  end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -28,4 +28,31 @@ describe Array do
28
28
  it_behaves_like "a serializable bson element"
29
29
  it_behaves_like "a deserializable bson element"
30
30
  end
31
+
32
+ describe "#to_bson_object_id" do
33
+
34
+ context "when the array has 12 elements" do
35
+
36
+ let(:array) do
37
+ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]
38
+ end
39
+
40
+ let(:converted) do
41
+ array.to_bson_object_id
42
+ end
43
+
44
+ it "returns the array as a string" do
45
+ expect(converted).to eq(array.pack("C*"))
46
+ end
47
+ end
48
+
49
+ context "when the array does not have 12 elements" do
50
+
51
+ it "raises an exception" do
52
+ expect {
53
+ [ 1 ].to_bson_object_id
54
+ }.to raise_error(BSON::ObjectId::Invalid)
55
+ end
56
+ end
57
+ end
31
58
  end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -31,6 +31,21 @@ describe BSON::Binary do
31
31
  it_behaves_like "a JSON serializable object"
32
32
  end
33
33
 
34
+ describe "#initialize" do
35
+
36
+ context "when he type is invalid" do
37
+
38
+ it "raises an error" do
39
+ expect {
40
+ described_class.new("testing", :error)
41
+ }.to raise_error { |error|
42
+ error.should be_a(BSON::Binary::InvalidType)
43
+ error.message.should match /is not a valid binary type/
44
+ }
45
+ end
46
+ end
47
+ end
48
+
34
49
  describe "#to_bson/#from_bson" do
35
50
 
36
51
  let(:type) { 5.chr }
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -16,6 +16,29 @@ require "spec_helper"
16
16
 
17
17
  describe BSON::CodeWithScope do
18
18
 
19
+ describe "#==" do
20
+
21
+ let(:object) do
22
+ BSON::CodeWithScope.new("this.value = val", "test")
23
+ end
24
+
25
+ context "when the objects are equal" do
26
+ let(:other) { described_class.new("this.value = val", "test") }
27
+
28
+ it "returns true" do
29
+ expect(object).to eq(other)
30
+ end
31
+ end
32
+
33
+ context "when the other object is not equal" do
34
+ let(:other) { described_class.new("this.value = otherVal", "test") }
35
+
36
+ it "returns false" do
37
+ expect(object).to_not eq(other)
38
+ end
39
+ end
40
+ end
41
+
19
42
  describe "#as_json" do
20
43
 
21
44
  let(:object) do
@@ -40,8 +63,8 @@ describe BSON::CodeWithScope do
40
63
  end
41
64
  let(:obj) { described_class.new(code, scope) }
42
65
  let(:bson) do
43
- "#{48.to_bson}#{(code.length + 1).to_bson}#{code}#{BSON::NULL_BYTE}" +
44
- "#{scope.to_bson}#{BSON::NULL_BYTE}"
66
+ "#{47.to_bson}#{(code.length + 1).to_bson}#{code}#{BSON::NULL_BYTE}" +
67
+ "#{scope.to_bson}"
45
68
  end
46
69
 
47
70
  it_behaves_like "a bson element"
@@ -53,18 +76,22 @@ describe BSON::CodeWithScope do
53
76
  let(:type) { 15.chr }
54
77
  let(:code) { "this.value == name" }
55
78
  let(:scope) do
56
- { :name => "test" }
79
+ { "name" => "test" }
57
80
  end
58
81
  let(:obj) { described_class.new(code, scope) }
59
82
  let(:bson) { StringIO.new(obj.to_bson) }
60
- let(:deserialized) { described_class.from_bson(bson) }
83
+ let!(:deserialized) { described_class.from_bson(bson) }
61
84
 
62
85
  it "deserializes the javascript" do
63
86
  expect(deserialized.javascript).to eq(code)
64
87
  end
65
88
 
66
- it "does not deserialize a scope" do
67
- expect(deserialized.scope).to be_empty
89
+ it "deserializes the scope" do
90
+ expect(deserialized.scope).to eq(scope)
91
+ end
92
+
93
+ it "does not leave any extra bytes" do
94
+ expect(bson.read(1)).to be_nil
68
95
  end
69
96
  end
70
97
  end