bson 1.0 → 1.0.1

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.

@@ -1,7 +1,9 @@
1
+ # encoding: UTF-8
2
+
1
3
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
4
 
3
5
  module BSON
4
- VERSION = "1.0"
6
+ VERSION = "1.0.1"
5
7
  def self.serialize(obj, check_keys=false, move_id=false)
6
8
  BSON_CODER.serialize(obj, check_keys, move_id)
7
9
  end
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  # A thin wrapper for the CBson class
2
4
  module BSON
3
5
  class BSON_C
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  # --
2
4
  # Copyright (C) 2008-2010 10gen Inc.
3
5
  #
@@ -182,7 +184,7 @@ module BSON
182
184
  end
183
185
  @buf.rewind
184
186
  @buf.get_int # eat message size
185
- doc = OrderedHash.new
187
+ doc = BSON::OrderedHash.new
186
188
  while @buf.more?
187
189
  type = @buf.get
188
190
  case type
@@ -325,6 +327,16 @@ module BSON
325
327
  Regexp.new(str, options)
326
328
  end
327
329
 
330
+ def encoded_str(str)
331
+ if RUBY_VERSION >= '1.9'
332
+ str.force_encoding("utf-8")
333
+ if Encoding.default_internal
334
+ str.encode!(Encoding.default_internal)
335
+ end
336
+ end
337
+ str
338
+ end
339
+
328
340
  def deserialize_string_data(buf)
329
341
  len = buf.get_int
330
342
  bytes = buf.get(len)
@@ -332,10 +344,7 @@ module BSON
332
344
  if str.respond_to? "pack"
333
345
  str = str.pack("C*")
334
346
  end
335
- if RUBY_VERSION >= '1.9'
336
- str.force_encoding("utf-8")
337
- end
338
- str
347
+ encoded_str(str)
339
348
  end
340
349
 
341
350
  def deserialize_code_w_scope_data(buf)
@@ -345,15 +354,12 @@ module BSON
345
354
  if code.respond_to? "pack"
346
355
  code = code.pack("C*")
347
356
  end
348
- if RUBY_VERSION >= '1.9'
349
- code.force_encoding("utf-8")
350
- end
351
357
 
352
358
  scope_size = buf.get_int
353
359
  buf.position -= 4
354
360
  scope = BSON_CODER.new().deserialize(buf.get(scope_size))
355
361
 
356
- Code.new(code, scope)
362
+ Code.new(encoded_str(code), scope)
357
363
  end
358
364
 
359
365
  def deserialize_oid_data(buf)
@@ -383,7 +389,7 @@ module BSON
383
389
  end
384
390
 
385
391
  def serialize_dbref_element(buf, key, val)
386
- oh = OrderedHash.new
392
+ oh = BSON::OrderedHash.new
387
393
  oh['$ref'] = val.namespace
388
394
  oh['$id'] = val.object_id
389
395
  serialize_object_element(buf, key, oh, false)
@@ -450,7 +456,7 @@ module BSON
450
456
 
451
457
  def serialize_array_element(buf, key, val, check_keys)
452
458
  # Turn array into hash with integer indices as keys
453
- h = OrderedHash.new
459
+ h = BSON::OrderedHash.new
454
460
  i = 0
455
461
  val.each { |v| h[i] = v; i += 1 }
456
462
  serialize_object_element(buf, key, h, check_keys, ARRAY)
@@ -536,10 +542,7 @@ module BSON
536
542
  break if b == 0
537
543
  chars << b.chr
538
544
  end
539
- if RUBY_VERSION >= '1.9'
540
- chars.force_encoding("utf-8") # Mongo stores UTF-8
541
- end
542
- chars
545
+ encoded_str(chars)
543
546
  end
544
547
 
545
548
  def bson_type(o)
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  # --
2
4
  # Copyright (C) 2008-2010 10gen Inc.
3
5
  #
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  # --
2
4
  # Copyright (C) 2008-2010 10gen Inc.
3
5
  #
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  # --
2
4
  # Copyright (C) 2008-2010 10gen Inc.
3
5
  #
@@ -18,123 +20,125 @@
18
20
  #
19
21
  # Under Ruby 1.9 and greater, this class has no added methods because Ruby's
20
22
  # Hash already keeps its keys ordered by order of insertion.
21
- class OrderedHash < Hash
22
-
23
- def ==(other)
24
- begin
25
- !other.nil? &&
26
- keys == other.keys &&
27
- values == other.values
28
- rescue
29
- false
23
+ module BSON
24
+ class OrderedHash < Hash
25
+
26
+ def ==(other)
27
+ begin
28
+ !other.nil? &&
29
+ keys == other.keys &&
30
+ values == other.values
31
+ rescue
32
+ false
33
+ end
30
34
  end
31
- end
32
35
 
33
- # We only need the body of this class if the RUBY_VERSION is before 1.9
34
- if RUBY_VERSION < '1.9'
35
- attr_accessor :ordered_keys
36
-
37
- def self.[] *args
38
- oh = OrderedHash.new
39
- if Hash === args[0]
40
- oh.merge! args[0]
41
- elsif (args.size % 2) != 0
42
- raise ArgumentError, "odd number of elements for Hash"
43
- else
44
- 0.step(args.size - 1, 2) do |key|
45
- value = key + 1
46
- oh[args[key]] = args[value]
36
+ # We only need the body of this class if the RUBY_VERSION is before 1.9
37
+ if RUBY_VERSION < '1.9'
38
+ attr_accessor :ordered_keys
39
+
40
+ def self.[] *args
41
+ oh = BSON::OrderedHash.new
42
+ if Hash === args[0]
43
+ oh.merge! args[0]
44
+ elsif (args.size % 2) != 0
45
+ raise ArgumentError, "odd number of elements for Hash"
46
+ else
47
+ 0.step(args.size - 1, 2) do |key|
48
+ value = key + 1
49
+ oh[args[key]] = args[value]
50
+ end
47
51
  end
52
+ oh
48
53
  end
49
- oh
50
- end
51
54
 
52
- def initialize(*a, &b)
53
- super
54
- @ordered_keys = []
55
- end
55
+ def initialize(*a, &b)
56
+ super
57
+ @ordered_keys = []
58
+ end
56
59
 
57
- def keys
58
- @ordered_keys || []
59
- end
60
+ def keys
61
+ @ordered_keys || []
62
+ end
60
63
 
61
- def []=(key, value)
62
- @ordered_keys ||= []
63
- @ordered_keys << key unless @ordered_keys.include?(key)
64
- super(key, value)
65
- end
64
+ def []=(key, value)
65
+ @ordered_keys ||= []
66
+ @ordered_keys << key unless @ordered_keys.include?(key)
67
+ super(key, value)
68
+ end
66
69
 
67
- def each
68
- @ordered_keys ||= []
69
- @ordered_keys.each { |k| yield k, self[k] }
70
- self
71
- end
72
- alias :each_pair :each
70
+ def each
71
+ @ordered_keys ||= []
72
+ @ordered_keys.each { |k| yield k, self[k] }
73
+ self
74
+ end
75
+ alias :each_pair :each
73
76
 
74
- def to_a
75
- @ordered_keys ||= []
76
- @ordered_keys.map { |k| [k, self[k]] }
77
- end
77
+ def to_a
78
+ @ordered_keys ||= []
79
+ @ordered_keys.map { |k| [k, self[k]] }
80
+ end
78
81
 
79
- def values
80
- collect { |k, v| v }
81
- end
82
+ def values
83
+ collect { |k, v| v }
84
+ end
82
85
 
83
- def merge(other)
84
- oh = self.dup
85
- oh.merge!(other)
86
- oh
87
- end
86
+ def merge(other)
87
+ oh = self.dup
88
+ oh.merge!(other)
89
+ oh
90
+ end
88
91
 
89
- def merge!(other)
90
- @ordered_keys ||= []
91
- @ordered_keys += other.keys # unordered if not an OrderedHash
92
- @ordered_keys.uniq!
93
- super(other)
94
- end
92
+ def merge!(other)
93
+ @ordered_keys ||= []
94
+ @ordered_keys += other.keys # unordered if not an BSON::OrderedHash
95
+ @ordered_keys.uniq!
96
+ super(other)
97
+ end
95
98
 
96
- alias :update :merge!
99
+ alias :update :merge!
97
100
 
98
- def inspect
99
- str = '{'
100
- str << (@ordered_keys || []).collect { |k| "\"#{k}\"=>#{self.[](k).inspect}" }.join(", ")
101
- str << '}'
102
- end
101
+ def inspect
102
+ str = '{'
103
+ str << (@ordered_keys || []).collect { |k| "\"#{k}\"=>#{self.[](k).inspect}" }.join(", ")
104
+ str << '}'
105
+ end
103
106
 
104
- def delete(key, &block)
105
- @ordered_keys.delete(key) if @ordered_keys
106
- super
107
- end
107
+ def delete(key, &block)
108
+ @ordered_keys.delete(key) if @ordered_keys
109
+ super
110
+ end
108
111
 
109
- def delete_if(&block)
110
- self.each { |k,v|
111
- if yield k, v
112
- delete(k)
113
- end
114
- }
115
- end
112
+ def delete_if(&block)
113
+ self.each { |k,v|
114
+ if yield k, v
115
+ delete(k)
116
+ end
117
+ }
118
+ end
116
119
 
117
- def clear
118
- super
119
- @ordered_keys = []
120
- end
120
+ def clear
121
+ super
122
+ @ordered_keys = []
123
+ end
121
124
 
122
- def hash
123
- code = 17
124
- each_pair do |key, value|
125
- code = 37 * code + key.hash
126
- code = 37 * code + value.hash
125
+ def hash
126
+ code = 17
127
+ each_pair do |key, value|
128
+ code = 37 * code + key.hash
129
+ code = 37 * code + value.hash
130
+ end
131
+ code & 0x7fffffff
127
132
  end
128
- code & 0x7fffffff
129
- end
130
133
 
131
- def eql?(o)
132
- if o.instance_of? OrderedHash
133
- self.hash == o.hash
134
- else
135
- false
134
+ def eql?(o)
135
+ if o.instance_of? BSON::OrderedHash
136
+ self.hash == o.hash
137
+ else
138
+ false
139
+ end
136
140
  end
137
- end
138
141
 
142
+ end
139
143
  end
140
144
  end
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  # --
2
4
  # Copyright (C) 2008-2010 10gen Inc.
3
5
  #
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  # --
2
4
  # Copyright (C) 2008-2010 10gen Inc.
3
5
  #
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  # --
2
4
  # Copyright (C) 2008-2010 10gen Inc.
3
5
  #
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  # --
2
4
  # Copyright (C) 2008-2010 10gen Inc.
3
5
  #
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  # --
2
4
  # Copyright (C) 2008-2010 10gen Inc.
3
5
  #
@@ -90,6 +90,23 @@ class BSONTest < Test::Unit::TestCase
90
90
  bson = BSON::BSON_CODER.serialize({'aé'.encode('iso-8859-1') => 'hello'})
91
91
  assert_equal 'hello', BSON::BSON_CODER.deserialize(bson)['aé']
92
92
  end
93
+
94
+ # Based on a test from sqlite3-ruby
95
+ def test_default_internal_is_honored
96
+ before_enc = Encoding.default_internal
97
+
98
+ str = "壁に耳あり、障子に目あり"
99
+ bson = BSON::BSON_CODER.serialize("x" => str)
100
+
101
+ Encoding.default_internal = 'EUC-JP'
102
+ out = BSON::BSON_CODER.deserialize(bson)["x"]
103
+
104
+ assert_equal Encoding.default_internal, out.encoding
105
+ assert_equal str.encode('EUC-JP'), out
106
+ assert_equal str, out.encode(str.encoding)
107
+ ensure
108
+ Encoding.default_internal = before_enc
109
+ end
93
110
  end
94
111
 
95
112
  def test_code
@@ -98,6 +115,12 @@ class BSONTest < Test::Unit::TestCase
98
115
  assert_equal doc, BSON::BSON_CODER.deserialize(bson)
99
116
  end
100
117
 
118
+ def test_code_with_scope
119
+ doc = {'$where' => Code.new('this.a.b < this.b', {'foo' => 1})}
120
+ bson = BSON::BSON_CODER.serialize(doc)
121
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
122
+ end
123
+
101
124
  def test_number
102
125
  doc = {'doc' => 41.99}
103
126
  bson = BSON::BSON_CODER.serialize(doc)
@@ -123,7 +146,7 @@ class BSONTest < Test::Unit::TestCase
123
146
  end
124
147
 
125
148
  def test_ordered_hash
126
- doc = OrderedHash.new
149
+ doc = BSON::OrderedHash.new
127
150
  doc["b"] = 1
128
151
  doc["a"] = 2
129
152
  doc["c"] = 3
@@ -284,16 +307,16 @@ class BSONTest < Test::Unit::TestCase
284
307
  end
285
308
 
286
309
  def test_put_id_first
287
- val = OrderedHash.new
310
+ val = BSON::OrderedHash.new
288
311
  val['not_id'] = 1
289
312
  val['_id'] = 2
290
313
  roundtrip = BSON::BSON_CODER.deserialize(BSON::BSON_CODER.serialize(val, false, true).to_a)
291
- assert_kind_of OrderedHash, roundtrip
314
+ assert_kind_of BSON::OrderedHash, roundtrip
292
315
  assert_equal '_id', roundtrip.keys.first
293
316
 
294
317
  val = {'a' => 'foo', 'b' => 'bar', :_id => 42, 'z' => 'hello'}
295
318
  roundtrip = BSON::BSON_CODER.deserialize(BSON::BSON_CODER.serialize(val, false, true).to_a)
296
- assert_kind_of OrderedHash, roundtrip
319
+ assert_kind_of BSON::OrderedHash, roundtrip
297
320
  assert_equal '_id', roundtrip.keys.first
298
321
  end
299
322
 
@@ -354,7 +377,7 @@ class BSONTest < Test::Unit::TestCase
354
377
  end
355
378
 
356
379
  def test_do_not_change_original_object
357
- val = OrderedHash.new
380
+ val = BSON::OrderedHash.new
358
381
  val['not_id'] = 1
359
382
  val['_id'] = 2
360
383
  assert val.keys.include?('_id')
@@ -369,7 +392,7 @@ class BSONTest < Test::Unit::TestCase
369
392
 
370
393
  # note we only test for _id here because in the general case we will
371
394
  # write duplicates for :key and "key". _id is a special case because
372
- # we call has_key? to check for it's existance rather than just iterating
395
+ # we call has_key? to check for it's existence rather than just iterating
373
396
  # over it like we do for the rest of the keys. thus, things like
374
397
  # HashWithIndifferentAccess can cause problems for _id but not for other
375
398
  # keys. rather than require rails to test with HWIA directly, we do this
@@ -426,7 +449,7 @@ class BSONTest < Test::Unit::TestCase
426
449
  end
427
450
 
428
451
  def test_move_id
429
- a = OrderedHash.new
452
+ a = BSON::OrderedHash.new
430
453
  a['text'] = 'abc'
431
454
  a['key'] = 'abc'
432
455
  a['_id'] = 1
@@ -441,10 +464,10 @@ class BSONTest < Test::Unit::TestCase
441
464
  end
442
465
 
443
466
  def test_move_id_with_nested_doc
444
- b = OrderedHash.new
467
+ b = BSON::OrderedHash.new
445
468
  b['text'] = 'abc'
446
469
  b['_id'] = 2
447
- c = OrderedHash.new
470
+ c = BSON::OrderedHash.new
448
471
  c['text'] = 'abc'
449
472
  c['hash'] = b
450
473
  c['_id'] = 3
@@ -3,7 +3,7 @@ require 'test/test_helper'
3
3
  class OrderedHashTest < Test::Unit::TestCase
4
4
 
5
5
  def setup
6
- @oh = OrderedHash.new
6
+ @oh = BSON::OrderedHash.new
7
7
  @oh['c'] = 1
8
8
  @oh['a'] = 2
9
9
  @oh['z'] = 3
@@ -11,31 +11,31 @@ class OrderedHashTest < Test::Unit::TestCase
11
11
  end
12
12
 
13
13
  def test_initialize
14
- a = OrderedHash.new
14
+ a = BSON::OrderedHash.new
15
15
  a['x'] = 1
16
16
  a['y'] = 2
17
17
 
18
- b = OrderedHash['x' => 1, 'y' => 2]
18
+ b = BSON::OrderedHash['x' => 1, 'y' => 2]
19
19
  assert_equal a, b
20
20
  end
21
21
 
22
22
  def test_hash_code
23
- o = OrderedHash.new
23
+ o = BSON::OrderedHash.new
24
24
  o['number'] = 50
25
25
  assert o.hash
26
26
  end
27
27
 
28
28
  def test_empty
29
- assert_equal [], OrderedHash.new.keys
29
+ assert_equal [], BSON::OrderedHash.new.keys
30
30
  end
31
31
 
32
32
  def test_uniq
33
33
  list = []
34
- doc = OrderedHash.new
34
+ doc = BSON::OrderedHash.new
35
35
  doc['_id'] = 'ab12'
36
36
  doc['name'] = 'test'
37
37
 
38
- same_doc = OrderedHash.new
38
+ same_doc = BSON::OrderedHash.new
39
39
  same_doc['_id'] = 'ab12'
40
40
  same_doc['name'] = 'test'
41
41
  list << doc
@@ -46,23 +46,23 @@ class OrderedHashTest < Test::Unit::TestCase
46
46
  end
47
47
 
48
48
  def test_equality
49
- a = OrderedHash.new
49
+ a = BSON::OrderedHash.new
50
50
  a['x'] = 1
51
51
  a['y'] = 2
52
52
 
53
- b = OrderedHash.new
53
+ b = BSON::OrderedHash.new
54
54
  b['y'] = 2
55
55
  b['x'] = 1
56
56
 
57
- c = OrderedHash.new
57
+ c = BSON::OrderedHash.new
58
58
  c['x'] = 1
59
59
  c['y'] = 2
60
60
 
61
- d = OrderedHash.new
61
+ d = BSON::OrderedHash.new
62
62
  d['x'] = 2
63
63
  d['y'] = 3
64
64
 
65
- e = OrderedHash.new
65
+ e = BSON::OrderedHash.new
66
66
  e['z'] = 1
67
67
  e['y'] = 2
68
68
 
@@ -105,7 +105,7 @@ class OrderedHashTest < Test::Unit::TestCase
105
105
  end
106
106
 
107
107
  def test_merge
108
- other = OrderedHash.new
108
+ other = BSON::OrderedHash.new
109
109
  other['f'] = 'foo'
110
110
  noob = @oh.merge(other)
111
111
  assert_equal @ordered_keys + ['f'], noob.keys
@@ -113,7 +113,7 @@ class OrderedHashTest < Test::Unit::TestCase
113
113
  end
114
114
 
115
115
  def test_merge_bang
116
- other = OrderedHash.new
116
+ other = BSON::OrderedHash.new
117
117
  other['f'] = 'foo'
118
118
  @oh.merge!(other)
119
119
  assert_equal @ordered_keys + ['f'], @oh.keys
@@ -121,7 +121,7 @@ class OrderedHashTest < Test::Unit::TestCase
121
121
  end
122
122
 
123
123
  def test_merge_bang_with_overlap
124
- other = OrderedHash.new
124
+ other = BSON::OrderedHash.new
125
125
  other['a'] = 'apple'
126
126
  other['c'] = 'crab'
127
127
  other['f'] = 'foo'
@@ -141,7 +141,7 @@ class OrderedHashTest < Test::Unit::TestCase
141
141
  end
142
142
 
143
143
  def test_update
144
- other = OrderedHash.new
144
+ other = BSON::OrderedHash.new
145
145
  other['f'] = 'foo'
146
146
  noob = @oh.update(other)
147
147
  assert_equal @ordered_keys + ['f'], noob.keys
metadata CHANGED
@@ -1,11 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bson
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 0
8
- version: "1.0"
4
+ version: 1.0.1
9
5
  platform: ruby
10
6
  authors:
11
7
  - Jim Menard
@@ -15,7 +11,7 @@ autorequire:
15
11
  bindir: bin
16
12
  cert_chain: []
17
13
 
18
- date: 2010-04-29 00:00:00 -04:00
14
+ date: 2010-05-07 00:00:00 -04:00
19
15
  default_executable:
20
16
  dependencies: []
21
17
 
@@ -55,20 +51,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
55
51
  requirements:
56
52
  - - ">="
57
53
  - !ruby/object:Gem::Version
58
- segments:
59
- - 0
60
54
  version: "0"
55
+ version:
61
56
  required_rubygems_version: !ruby/object:Gem::Requirement
62
57
  requirements:
63
58
  - - ">="
64
59
  - !ruby/object:Gem::Version
65
- segments:
66
- - 0
67
60
  version: "0"
61
+ version:
68
62
  requirements: []
69
63
 
70
64
  rubyforge_project:
71
- rubygems_version: 1.3.6
65
+ rubygems_version: 1.3.5
72
66
  signing_key:
73
67
  specification_version: 3
74
68
  summary: Ruby implementation of BSON