Sha256: 1dea07c6c0e8bbf4e4e26239d2fda632432dfe1c56978eb77a39a5367cd0b24c

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

module Moped
  module BSON
    module Extensions

      module String

        def __bson_dump__(io, key)
          io << Types::STRING
          io << key.to_bson_cstring
          data = to_utf8_binary
          io << [ data.bytesize + 1 ].pack(INT32_PACK)
          io << data
          io << NULL_BYTE
        end

        def to_bson_cstring
          if include? NULL_BYTE
            raise EncodingError, "#{inspect} cannot be converted to a BSON " \
              "cstring because it contains a null byte"
          end
          to_utf8_binary << NULL_BYTE
        end

        def to_utf8_binary
          encode(UTF8_ENCODING).force_encoding(BINARY_ENCODING)
        rescue EncodingError
          data = dup.force_encoding(UTF8_ENCODING)
          raise unless data.valid_encoding?
          data.force_encoding(BINARY_ENCODING)
        end

        def from_utf8_binary
          force_encoding(UTF8_ENCODING).encode!
        end

        module ClassMethods

          def __bson_load__(io)
            io.read(*io.read(4).unpack(INT32_PACK)).from_utf8_binary.chop!
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
moped-1.4.3 lib/moped/bson/extensions/string.rb
moped-1.4.2 lib/moped/bson/extensions/string.rb
moped-1.4.1 lib/moped/bson/extensions/string.rb
moped-1.4.0 lib/moped/bson/extensions/string.rb