BSON::Encodable

Defines behaviour around objects that can be encoded.

@since 2.0.0

Constants

BSON_ADJUST

Adjustment value for total number of document bytes.

@since 2.0.0

PLACEHOLDER

A 4 byte placeholder that would be replaced by a length at a later point.

@since 2.0.0

STRING_ADJUST

Adjustment value for total number of string bytes.

@since 2.0.0

Public Instance Methods

encode_binary_data_with_placeholder(encoded = ''.force_encoding(BINARY)) click to toggle source

Encodes binary data with a generic placeholder value to be written later once all bytes have been written.

@example Encode the BSON with placeholder bytes.

string.encode_binary_data_with_placeholder(encoded) do |encoded|
  each do |field, value|
    value.to_bson(encoded)
  end
end

@param [ String ] encoded The string to encode.

@return [ String ] The encoded string.

@since 2.0.0

# File lib/bson/encodable.rb, line 78
def encode_binary_data_with_placeholder(encoded = ''.force_encoding(BINARY))
  pos = encoded.bytesize
  encoded << PLACEHOLDER
  yield(encoded)
  encoded.set_int32(pos, encoded.bytesize - pos - 5)
  encoded
end
encode_with_placeholder_and_null(adjust, encoded = ''.force_encoding(BINARY)) click to toggle source

Encodes BSON to raw bytes, for types that require the length of the entire bytes to be present as the first word of the encoded string. This includes Hash, CodeWithScope.

@example Encode the BSON with placeholder bytes.

hash.encode_with_placeholder_and_null(BSON_ADJUST, encoded) do |encoded|
  each do |field, value|
    value.to_bson(encoded)
  end
end

@param [ Integer ] adjust The number of bytes to adjust with. @param [ String ] encoded The string to encode.

@return [ String ] The encoded string.

@since 2.0.0

# File lib/bson/encodable.rb, line 54
def encode_with_placeholder_and_null(adjust, encoded = ''.force_encoding(BINARY))
  pos = encoded.bytesize
  encoded << PLACEHOLDER
  yield(encoded)
  encoded << NULL_BYTE
  encoded.set_int32(pos, encoded.bytesize - pos + adjust)
  encoded
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.