Class: BSON::Binary
- Inherits:
-
Object
- Object
- BSON::Binary
- Defined in:
- lib/bson/binary.rb
Overview
Represents binary data.
Defined Under Namespace
Classes: InvalidType
Constant Summary collapse
- BSON_TYPE =
A binary is type 0x05 in the BSON spec.
5.chr.force_encoding(BINARY).freeze
- SUBTYPES =
The mappings of subtypes to their single byte identifiers.
{ :generic => 0.chr, :function => 1.chr, :old => 2.chr, :uuid_old => 3.chr, :uuid => 4.chr, :md5 => 5.chr, :user => 128.chr }.freeze
- TYPES =
The mappings of single byte subtypes to their symbol counterparts.
SUBTYPES.invert.freeze
Constants included from Encodable
Encodable::BSON_ADJUST, Encodable::PLACEHOLDER, Encodable::STRING_ADJUST
Instance Attribute Summary collapse
-
#data ⇒ Object
The raw binary data.
- #type ⇒ Object
Class Method Summary collapse
-
.from_bson(bson) ⇒ Binary
Deserialize the binary data from BSON.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Determine if this binary object is equal to another object.
-
#as_json(*args) ⇒ Hash
Get the binary as JSON hash data.
-
#initialize(data = "", type = :generic) ⇒ Binary
constructor
Instantiate the new binary object.
-
#to_bson(encoded = ''.force_encoding(BINARY)) ⇒ String
Encode the binary type.
Methods included from Encodable
#encode_binary_data_with_placeholder, #encode_with_placeholder_and_null
Methods included from JSON
Constructor Details
#initialize(data = "", type = :generic) ⇒ Binary
Instantiate the new binary object.
93 94 95 96 97 |
# File 'lib/bson/binary.rb', line 93 def initialize(data = "", type = :generic) validate_type!(type) @data = data @type = type end |
Instance Attribute Details
#data ⇒ Object
Returns The raw binary data.
55 56 57 |
# File 'lib/bson/binary.rb', line 55 def data @data end |
#type ⇒ Object
55 |
# File 'lib/bson/binary.rb', line 55 attr_reader :data, :type |
Class Method Details
.from_bson(bson) ⇒ Binary
Deserialize the binary data from BSON.
126 127 128 129 130 131 132 |
# File 'lib/bson/binary.rb', line 126 def self.from_bson(bson) length = Int32.from_bson(bson) type = TYPES[bson.read(1)] length = Int32.from_bson(bson) if type == :old data = bson.read(length) new(data, type) end |
Instance Method Details
#==(other) ⇒ true, false
Determine if this binary object is equal to another object.
67 68 69 70 |
# File 'lib/bson/binary.rb', line 67 def ==(other) return false unless other.is_a?(Binary) type == other.type && data == other.data end |
#as_json(*args) ⇒ Hash
Get the binary as JSON hash data.
80 81 82 |
# File 'lib/bson/binary.rb', line 80 def as_json(*args) { "$binary" => data, "$type" => type } end |
#to_bson(encoded = ''.force_encoding(BINARY)) ⇒ String
Encode the binary type
109 110 111 112 113 114 115 |
# File 'lib/bson/binary.rb', line 109 def to_bson(encoded = ''.force_encoding(BINARY)) encode_binary_data_with_placeholder(encoded) do |encoded| encoded << SUBTYPES.fetch(type) encoded << data.bytesize.to_bson if type == :old encoded << data end end |