Module: BSON::Array
- Includes:
- Encodable
- Defined in:
- lib/bson/array.rb
Overview
Injects behaviour for encoding and decoding arrays to and from raw bytes as specified by the BSON spec.
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- BSON_TYPE =
An array is type 0x04 in the BSON spec.
4.chr.force_encoding(BINARY).freeze
Constants included from Encodable
Encodable::BSON_ADJUST, Encodable::PLACEHOLDER, Encodable::STRING_ADJUST
Instance Method Summary collapse
-
#to_bson(encoded = ''.force_encoding(BINARY)) ⇒ String
Get the array as encoded BSON.
-
#to_bson_normalized_value ⇒ Array
Converts the array to a normalized value in a BSON document.
-
#to_bson_object_id ⇒ String
Convert the array to an object id.
Methods included from Encodable
#encode_binary_data_with_placeholder, #encode_with_placeholder_and_null
Instance Method Details
#to_bson(encoded = ''.force_encoding(BINARY)) ⇒ String
Note:
Arrays are encoded as documents, where the index of the value in the array is the actual key.
Get the array as encoded BSON.
44 45 46 47 48 49 50 51 52 |
# 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 |
#to_bson_normalized_value ⇒ Array
Converts the array to a normalized value in a BSON document.
79 80 81 |
# File 'lib/bson/array.rb', line 79 def to_bson_normalized_value map!{ |value| value.to_bson_normalized_value } end |