Sha256: 33584f46a23f7f0857d1788dc931a28633155a17ecc55b0d31cc3a0891f0a576

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

module Moped
  module BSON
    # @private
    module Extensions
      module Hash

        module ClassMethods
          def __bson_load__(io, doc = new)
            # Swallow the first four (length) bytes
            io.read 4

            while (buf = io.readbyte) != 0
              key = io.gets(NULL_BYTE).chop!.force_encoding('utf-8')

              if native_class = Types::MAP[buf]
                doc[key] = native_class.__bson_load__(io)
              end
            end

            doc
          end
        end

        def __bson_dump__(io = "", key = nil)
          if key
            io << Types::HASH
            io << key
            io << NULL_BYTE
          end

          start = io.length

          # write dummy length
          io << START_LENGTH

          each do |k, v|
            v.__bson_dump__(io, k.to_s.encode('utf-8').force_encoding('binary'))
          end
          io << EOD

          stop = io.length
          io[start, 4] = [stop - start].pack INT32_PACK

          io
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
moped-1.0.0.beta lib/moped/bson/extensions/hash.rb
moped-1.0.0.alpha lib/moped/bson/extensions/hash.rb