lib/bson.rb in bson-1.0.1 vs lib/bson.rb in bson-1.0.2

- old
+ new

@@ -1,25 +1,47 @@ # encoding: UTF-8 $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) +MINIMUM_BSON_EXT_VERSION = "1.0.1" + module BSON - VERSION = "1.0.1" + VERSION = "1.0.2" def self.serialize(obj, check_keys=false, move_id=false) BSON_CODER.serialize(obj, check_keys, move_id) end def self.deserialize(buf=nil) BSON_CODER.deserialize(buf) end + + # Reads a single BSON document from an IO object. + # This method is used in the executable b2json, bundled with + # the bson gem, for reading a file of bson documents. + # + # @param [IO] io an io object containing a bson object. + # + # @return [ByteBuffer] + def self.read_bson_document(io) + bytebuf = BSON::ByteBuffer.new + sz = io.read(4).unpack("V")[0] + bytebuf.put_int(sz) + bytebuf.put_array(io.read(sz-4).unpack("C*")) + bytebuf.rewind + return BSON.deserialize(bytebuf) + end end begin # Need this for running test with and without c ext in Ruby 1.9. raise LoadError if ENV['TEST_MODE'] && !ENV['C_EXT'] require 'bson_ext/cbson' - raise LoadError unless defined?(CBson::VERSION) && CBson::VERSION == BSON::VERSION + raise LoadError unless defined?(CBson::VERSION) + if CBson::VERSION < MINIMUM_BSON_EXT_VERSION + puts "Able to load bson_ext version #{CBson::VERSION}, but >= #{MINIMUM_BSON_EXT_VERSION} is required." + raise LoadError + end require 'bson/bson_c' module BSON BSON_CODER = BSON_C end rescue LoadError