mongodb-mongo 0.2.7 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -79,7 +79,17 @@ class BSON
79
79
  # put in a placeholder for the total size
80
80
  @buf.put_int(0)
81
81
 
82
- obj.each {|k, v|
82
+ # Write key/value pairs. Always write _id first if it exists.
83
+ oid = obj.delete('_id') || obj.delete(:_id)
84
+ serialize_key_value('_id', oid) if oid
85
+ obj.each {|k, v| serialize_key_value(k, v) }
86
+
87
+ serialize_eoo_element(@buf)
88
+ @buf.put_int(@buf.size, 0)
89
+ self
90
+ end
91
+
92
+ def serialize_key_value(k, v)
83
93
  type = bson_type(v, k)
84
94
  case type
85
95
  when STRING, CODE, SYMBOL
@@ -112,10 +122,6 @@ class BSON
112
122
  else
113
123
  raise "unhandled type #{type}"
114
124
  end
115
- }
116
- serialize_eoo_element(@buf)
117
- @buf.put_int(@buf.size, 0)
118
- self
119
125
  end
120
126
 
121
127
  def deserialize(buf=nil, parent=nil)
@@ -338,20 +344,7 @@ class BSON
338
344
  def serialize_object_element(buf, key, val, opcode=OBJECT)
339
345
  buf.put(opcode)
340
346
  self.class.serialize_cstr(buf, key)
341
- buf.put_array(BSON.new.serialize(put_id_first(val)).to_a)
342
- end
343
-
344
- # For internal use only. Looks for '_id' or :_id key of val. If there is
345
- # one, returns an OrderedHash where '_id' is first. If not, returns val.
346
- def put_id_first(val)
347
- oid = val.delete('_id') || val.delete(:_id)
348
- if oid
349
- h = OrderedHash.new
350
- h['_id'] = oid
351
- val.each { |k, v| h[k] = v }
352
- val = h
353
- end
354
- val
347
+ buf.put_array(BSON.new.serialize(val).to_a)
355
348
  end
356
349
 
357
350
  def serialize_array_element(buf, key, val)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'mongo'
3
- s.version = '0.2.7'
3
+ s.version = '0.3.0'
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.summary = 'Simple pure-Ruby driver for the 10gen Mongo DB'
6
6
  s.description = 'A pure-Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'
data/tests/test_bson.rb CHANGED
@@ -95,9 +95,6 @@ class BSONTest < Test::Unit::TestCase
95
95
  assert_equal doc['date'].to_i, doc2['date'].to_i
96
96
  end
97
97
 
98
- def test_null
99
- end
100
-
101
98
  def test_dbref
102
99
  oid = ObjectID.new
103
100
  doc = {}
@@ -125,17 +122,17 @@ class BSONTest < Test::Unit::TestCase
125
122
  assert_equal 'binstring', doc2['bin']
126
123
  end
127
124
 
128
- # def test_binary_byte_buffer
129
- # bb = ByteBuffer.new
130
- # 10.times { |i| bb.put(i) }
131
- # doc = {'bin' => bb}
132
- # @b.serialize(doc)
133
- # doc2 = @b.deserialize
125
+ def test_binary_byte_buffer
126
+ bb = ByteBuffer.new
127
+ 10.times { |i| bb.put(i) }
128
+ doc = {'bin' => bb}
129
+ @b.serialize(doc)
130
+ doc2 = @b.deserialize
134
131
 
135
- # doc2_bytes = []
136
- # doc2['bin'].each_byte { |b| doc2_bytes << b }
137
- # assert_equal bb.to_a, doc2_bytes
138
- # end
132
+ doc2_bytes = []
133
+ doc2['bin'].each_byte { |b| doc2_bytes << b }
134
+ assert_equal bb.to_a, doc2_bytes
135
+ end
139
136
 
140
137
  def test_undefined
141
138
  doc = {'undef' => Undefined.new}
@@ -145,19 +142,17 @@ class BSONTest < Test::Unit::TestCase
145
142
  end
146
143
 
147
144
  def test_put_id_first
148
- val = {'a' => 'foo'}
149
- assert_same val, @b.put_id_first(val)
150
-
151
145
  val = OrderedHash.new
152
146
  val['not_id'] = 1
153
147
  val['_id'] = 2
154
- id_first = @b.put_id_first(val)
155
- assert_equal ['_id', 'not_id'], id_first.keys
148
+ roundtrip = @b.deserialize(@b.serialize(val).to_a)
149
+ assert_kind_of OrderedHash, roundtrip
150
+ assert_equal '_id', roundtrip.keys.first
156
151
 
157
152
  val = {'a' => 'foo', 'b' => 'bar', :_id => 42, 'z' => 'hello'}
158
- id_first = @b.put_id_first(val)
159
- assert id_first.keys.include?('_id')
160
- assert !id_first.keys.include?(:_id)
153
+ roundtrip = @b.deserialize(@b.serialize(val).to_a)
154
+ assert_kind_of OrderedHash, roundtrip
155
+ assert_equal '_id', roundtrip.keys.first
161
156
  end
162
157
 
163
158
  end
data/tests/test_db.rb CHANGED
@@ -64,11 +64,15 @@ class DBTest < Test::Unit::TestCase
64
64
 
65
65
  obj = coll.insert('name' => 'Fred', 'age' => 42)
66
66
  row = coll.find({'name' => 'Fred'}, :limit => 1).next_object
67
+ oid = row.delete('_id')
68
+ assert_not_nil oid
67
69
  assert_equal obj, row
68
70
 
69
71
  oid = XGen::Mongo::Driver::ObjectID.new
70
72
  obj = coll.insert('_id' => oid, 'name' => 'Barney', 'age' => 41)
71
73
  row = coll.find({'name' => 'Barney'}, :limit => 1).next_object
74
+ db_oid = row.delete('_id')
75
+ assert_equal oid, db_oid
72
76
  assert_equal obj, row
73
77
 
74
78
  coll.clear
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongodb-mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Menard