Sha256: 74c64697750b6eccde9ae273932fc002adafd2bd3c76d27fbcc725e13e7b6197

Contents?: true

Size: 1.4 KB

Versions: 21

Compression:

Stored size: 1.4 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.bytesize + 4].pack(INT32_PACK)
          io << SUBTYPE_MAP[type]
          io << [data.bytesize].pack(INT32_PACK)
          io << data
        else
          io << [data.bytesize].pack(INT32_PACK)
          io << SUBTYPE_MAP[type]
          io << data
        end
      end

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

      def to_s
        data.to_s
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/bson/binary.rb
moped-1.3.2 lib/moped/bson/binary.rb
moped-1.3.1 lib/moped/bson/binary.rb
moped-1.3.0 lib/moped/bson/binary.rb
moped-1.2.9 lib/moped/bson/binary.rb
moped-1.2.8 lib/moped/bson/binary.rb
moped-1.2.7 lib/moped/bson/binary.rb
moped-1.2.6 lib/moped/bson/binary.rb
moped-1.2.5 lib/moped/bson/binary.rb
moped-1.2.4 lib/moped/bson/binary.rb
moped-1.2.2 lib/moped/bson/binary.rb
moped-1.2.1 lib/moped/bson/binary.rb
moped-1.2.0 lib/moped/bson/binary.rb
moped-1.1.6 lib/moped/bson/binary.rb
moped-1.1.5 lib/moped/bson/binary.rb
moped-1.1.4 lib/moped/bson/binary.rb
moped-1.1.3 lib/moped/bson/binary.rb
moped-1.1.2 lib/moped/bson/binary.rb
moped-1.0.1 lib/moped/bson/binary.rb
moped-1.1.1 lib/moped/bson/binary.rb