diff --git a/lib/mongo/util/bson.rb b/lib/mongo/util/bson.rb index 545fa1b..49859fa 100644 --- a/lib/mongo/util/bson.rb +++ b/lib/mongo/util/bson.rb @@ -287,12 +287,17 @@ class BSON buf.put(BINARY) self.class.serialize_cstr(buf, key) - bytes = if RUBY_VERSION >= '1.9' - val.bytes.to_a + bytes = case val + when ByteBuffer + val.to_a else - a = [] - val.each_byte { |byte| a << byte } - a + if RUBY_VERSION >= '1.9' + val.bytes.to_a + else + a = [] + val.each_byte { |byte| a << byte } + a + end end num_bytes = bytes.length @@ -422,7 +427,7 @@ class BSON NUMBER_INT when Numeric NUMBER - when XGen::Mongo::Driver::Binary # must be before String + when XGen::Mongo::Driver::Binary, ByteBuffer # must be before String BINARY when String # magic awful stuff - the DB requires that a where clause is sent as CODE diff --git a/mongo-ruby-driver.gemspec b/mongo-ruby-driver.gemspec index d17c529..be656f0 100644 --- a/mongo-ruby-driver.gemspec +++ b/mongo-ruby-driver.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'mongo' - s.version = '0.2.6' + s.version = '0.2.7' s.platform = Gem::Platform::RUBY s.summary = 'Simple pure-Ruby driver for the 10gen Mongo DB' s.description = 'A pure-Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.' diff --git a/tests/test_bson.rb b/tests/test_bson.rb index 349007e..b4b9bbe 100644 --- a/tests/test_bson.rb +++ b/tests/test_bson.rb @@ -125,6 +125,18 @@ class BSONTest < Test::Unit::TestCase assert_equal 'binstring', doc2['bin'] end +# def test_binary_byte_buffer +# bb = ByteBuffer.new +# 10.times { |i| bb.put(i) } +# doc = {'bin' => bb} +# @b.serialize(doc) +# doc2 = @b.deserialize + +# doc2_bytes = [] +# doc2['bin'].each_byte { |b| doc2_bytes << b } +# assert_equal bb.to_a, doc2_bytes +# end + def test_undefined doc = {'undef' => Undefined.new} @b.serialize(doc)