Sha256: 7572e998fcea013c42a159e8af285ffe442118a74b2ea5f0561ff725e74ed6a2

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

module Moped
  module BSON
    class Binary

      SUBTYPE_MAP = {
        generic:  "\x00",
        function: "\x01",
        old:      "\x02",
        uuid:     "\x03",
        md5:      "\x05",
        user:     "\x80"
      }

      attr_reader :data, :type

      def initialize(type, data)
        @type = type
        @data = data
      end

      class << self
        def __bson_load__(io)
          length, = io.read(4).unpack(INT32_PACK)
          type = SUBTYPE_MAP.invert[io.read(1)]

          if type == :old
            length -= 4
            io.read(4)
          end

          data = io.read length
          new(type, data)
        end
      end

      def ==(other)
        BSON::Binary === other && data == other.data && type == other.type
      end
      alias eql? ==

      def hash
        [data, type].hash
      end

      def __bson_dump__(io, key)
        io << Types::BINARY
        io << key
        io << NULL_BYTE

        if type == :old
          io << [data.length + 4].pack(INT32_PACK)
          io << SUBTYPE_MAP[type]
          io << [data.length].pack(INT32_PACK)
          io << data
        else
          io << [data.length].pack(INT32_PACK)
          io << SUBTYPE_MAP[type]
          io << data
        end
      end

      def inspect
        "#<#{self.class.name} type=#{type.inspect} length=#{data.length}>"
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
moped-1.0.0.rc lib/moped/bson/binary.rb
moped-1.0.0.beta lib/moped/bson/binary.rb
moped-1.0.0.alpha lib/moped/bson/binary.rb