Injects behaviour for encoding and decoding arrays to and from raw bytes as specified by the BSON spec.
@see bsonspec.org/#/specification
@since 2.0.0
Get the array as encoded BSON.
@example Get the array as encoded BSON.
[ 1, 2, 3 ].to_bson
@note Arrays are encoded as documents, where the index of the value in
the array is the actual key.
@return [ String ] The encoded string.
@see bsonspec.org/#/specification
@since 2.0.0
# File lib/bson/array.rb, line 44 def to_bson(encoded = ''.force_encoding(BINARY)) encode_with_placeholder_and_null(BSON_ADJUST, encoded) do |encoded| each_with_index do |value, index| encoded << value.bson_type index.to_bson_key(encoded) value.to_bson(encoded) end end end
Convert the array to an object id. This will only work for arrays of size 12 where the elements are all strings.
@example Convert the array to an object id.
array.to_bson_object_id
@note This is used for repairing legacy bson data.
@raise [ InvalidObjectId ] If the array is not 12 elements.
@return [ String ] The raw object id bytes.
@since 2.0.0
# File lib/bson/array.rb, line 67 def to_bson_object_id ObjectId.repair(self) { pack("C*") } end
Generated with the Darkfish Rdoc Generator 2.