module BSON::Hash::ClassMethods

def from_bson(buffer, **options)

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

Returns:
  • (Hash) - The decoded hash.

Options Hash: (**options)
  • :mode (nil | :bson) -- Decoding mode to use.

Parameters:
  • buffer (ByteBuffer) -- The byte buffer.

Other tags:
    Note: - If the argument cannot be parsed, an exception will be raised
def from_bson(buffer, **options)
  if buffer.respond_to?(:get_hash)
    buffer.get_hash(**options)
  else
    hash = parse_hash_from_buffer(buffer, **options)
    maybe_dbref(hash)
  end
end

def maybe_dbref(hash)

Returns:
  • (DBRef | Hash) - the result of decoding the hash

Parameters:
  • hash (Hash) -- the hash to try and decode
def maybe_dbref(hash)
  return DBRef.new(hash) if hash['$ref'] && hash['$id']
  hash
rescue Error::InvalidDBRefArgument
  hash
end

def parse_hash_contents(hash, buffer, **options)

Parameters:
  • options (Hash) -- the keyword arguments
  • buffer (ByteBuf) -- the buffer to read data from
  • hash (Hash) -- the hash to populate
def parse_hash_contents(hash, buffer, **options)
  while (type = buffer.get_byte) != NULL_BYTE
    field = buffer.get_cstring
    cls = BSON::Registry.get(type, field)
    value = if options.empty?
              # Compatibility with the older Ruby driver versions which define
              # a DBRef class with from_bson accepting a single argument.
              cls.from_bson(buffer)
            else
              cls.from_bson(buffer, **options)
            end
    hash.store(field, value)
  end
end

def parse_hash_from_buffer(buffer, **options)

Returns:
  • (Hash) - the hash parsed from the buffer

Parameters:
  • options (Hash) -- the keyword arguments
  • buffer (ByteBuf) -- the buffer to read data from
def parse_hash_from_buffer(buffer, **options)
  hash = Document.allocate
  start_position = buffer.read_position
  expected_byte_size = buffer.get_int32
  parse_hash_contents(hash, buffer, **options)
  actual_byte_size = buffer.read_position - start_position
  return hash unless actual_byte_size != expected_byte_size
  raise Error::BSONDecodeError,
        "Expected hash to take #{expected_byte_size} bytes but it took #{actual_byte_size} bytes"
end