module BSON::Integer

def append_bson_int32(encoded)

def append_bson_int32(encoded)
  encoded << (self & 255)
  encoded << ((self >> 8) & 255)
  encoded << ((self >> 16) & 255)
  encoded << ((self >> 24) & 255)
end

def as_extended_json(**options)

Returns:
  • (Hash | Integer) - The extended json representation.

Options Hash: (**opts)
  • :mode (nil | :relaxed | :legacy) -- Serialization mode
def as_extended_json(**options)
  # The behavior of native integers' serialization to extended json is
  # not specified. Following our bson serialization logic in this file,
  # produce explicit $numberInt or $numberLong, choosing $numberInt if
  # the integer fits in 32 bits. In Ruby integers can be arbitrarily
  # big; integers that do not fit into 64 bits raise an error as we do not
  # want to silently perform an effective type conversion of integer ->
  # decimal.
  unless bson_int64?
    raise RangeError, "Integer #{self} is too big to be represented as a MongoDB integer"
  end
  if options[:mode] == :relaxed || options[:mode] == :legacy
    self
  elsif bson_int32?
    {'$numberInt' => to_s}
  else
    {'$numberLong' => to_s}
  end
end

def bson_int32?

Other tags:
    Since: - 2.0.0

Returns:
  • (true, false) - If the integer is 32 bit.

Other tags:
    Example: Is the integer a valid 32 bit value? -
def bson_int32?
  (MIN_32BIT <= self) && (self <= MAX_32BIT)
end

def bson_int64?

Other tags:
    Since: - 2.0.0

Returns:
  • (true, false) - If the integer is 64 bit.

Other tags:
    Example: Is the integer a valid 64 bit value? -
def bson_int64?
  (MIN_64BIT <= self) && (self <= MAX_64BIT)
end

def bson_type

Other tags:
    Since: - 2.0.0

Other tags:
    See: http://bsonspec.org/#/specification -

Returns:
  • (String) - The single byte BSON type.

Other tags:
    Example: Get the BSON type for the integer. -
def bson_type
  bson_int32? ? Int32::BSON_TYPE : (bson_int64? ? Int64::BSON_TYPE : out_of_range!)
end

def out_of_range!

def out_of_range!
  raise RangeError.new("#{self} is not a valid 8 byte integer value.")
end

def to_bson(buffer = ByteBuffer.new)

Other tags:
    Since: - 2.0.0

Other tags:
    See: http://bsonspec.org/#/specification -

Returns:
  • (BSON::ByteBuffer) - The buffer with the encoded object.

Other tags:
    Example: Get the integer as encoded BSON. -
def to_bson(buffer = ByteBuffer.new)
  if bson_int32?
    buffer.put_int32(self)
  elsif bson_int64?
    buffer.put_int64(self)
  else
    out_of_range!
  end
end

def to_bson_int32(encoded)

Other tags:
    Since: - 2.0.0

Returns:
  • (String) - The encoded string.

Parameters:
  • encoded (String) -- The string to encode to.

Other tags:
    Example: Convert the integer to it's 32 bit bytes. -
def to_bson_int32(encoded)
  append_bson_int32(encoded)
end

def to_bson_int64(encoded)

Other tags:
    Since: - 2.0.0

Returns:
  • (String) - The encoded string.

Parameters:
  • encoded (String) -- The string to encode to.

Other tags:
    Example: Convert the integer to it's 64 bit bytes. -
def to_bson_int64(encoded)
  append_bson_int32(encoded)
  encoded << ((self >> 32) & 255)
  encoded << ((self >> 40) & 255)
  encoded << ((self >> 48) & 255)
  encoded << ((self >> 56) & 255)
end

def to_bson_key

Other tags:
    Since: - 2.0.0

Returns:
  • (String) - The string key.

Other tags:
    Example: Convert the integer to a BSON key string. -
def to_bson_key
  self
end