Sha256: 9a2329aea84e0924505a34d7032a3160ec03a1a9a6e50d0ad53bcdc01b8b32a1

Contents?: true

Size: 899 Bytes

Versions: 3

Compression:

Stored size: 899 Bytes

Contents

module Moped
  module BSON
    # @private
    module Extensions
      module Integer
        INT32_MIN = (-(1 << 31)+1)
        INT32_MAX = ((1<<31)-1)

        INT64_MIN = (-2**64 / 2)
        INT64_MAX = (2**64 / 2 - 1)

        module ClassMethods
          def __bson_load__(io, bignum=false)
            io.read(4).unpack(INT32_PACK)[0]
          end
        end

        def __bson_dump__(io, key)
          if self >= INT32_MIN && self <= INT32_MAX
            io << Types::INT32
            io << key
            io << NULL_BYTE
            io << [self].pack(INT32_PACK)
          elsif self >= INT64_MIN && self <= INT64_MAX
            io << Types::INT64
            io << key
            io << NULL_BYTE
            io << [self].pack(INT64_PACK)
          else
            raise RangeError.new("MongoDB can only handle 8-byte ints")
          end
        end

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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