Class: BSON::Binary
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
Instance Attribute Summary collapse
-
#data ⇒ Object
The raw binary data.
- #type ⇒ Object
Class Method Summary collapse
-
.from_bson(buffer) ⇒ Binary
Deserialize the binary data from BSON.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
(also: #eql?)
Determine if this binary object is equal to another object.
-
#as_json(*args) ⇒ Hash
Get the binary as JSON hash data.
-
#hash ⇒ Fixnum
Generates a Fixnum hash value for this object.
-
#initialize(data = "", type = :generic) ⇒ Binary
constructor
Instantiate the new binary object.
-
#inspect ⇒ String
Get a nice string for use with object inspection.
-
#to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) ⇒ String
Encode the binary type.
Methods included from JSON
Constructor Details
#initialize(data = "", type = :generic) ⇒ Binary
Instantiate the new binary object.
104 105 106 107 108 |
# File 'lib/bson/binary.rb', line 104 def initialize(data = "", type = :generic) validate_type!(type) @data = data @type = type end |
Instance Attribute Details
#data ⇒ Object
Returns The raw binary data.
54 55 56 |
# File 'lib/bson/binary.rb', line 54 def data @data end |
Class Method Details
.from_bson(buffer) ⇒ Binary
Deserialize the binary data from BSON.
150 151 152 153 154 155 156 |
# File 'lib/bson/binary.rb', line 150 def self.from_bson(buffer) length = buffer.get_int32 type = TYPES[buffer.get_byte] length = buffer.get_int32 if type == :old data = buffer.get_bytes(length) new(data, type) end |
Instance Method Details
#==(other) ⇒ true, false Also known as: eql?
Determine if this binary object is equal to another object.
66 67 68 69 |
# File 'lib/bson/binary.rb', line 66 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.
91 92 93 |
# File 'lib/bson/binary.rb', line 91 def as_json(*args) { "$binary" => data, "$type" => type } end |
#hash ⇒ Fixnum
Generates a Fixnum hash value for this object.
Allows using Binary as hash keys.
79 80 81 |
# File 'lib/bson/binary.rb', line 79 def hash data.hash + type.hash end |
#inspect ⇒ String
Get a nice string for use with object inspection.
118 119 120 |
# File 'lib/bson/binary.rb', line 118 def inspect "<BSON::Binary:0x#{object_id} type=#{type} data=0x#{data[0, 8].unpack('H*').first}...>" end |
#to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) ⇒ String
Encode the binary type
132 133 134 135 136 137 138 139 |
# File 'lib/bson/binary.rb', line 132 def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) position = buffer.length buffer.put_int32(0) buffer.put_byte(SUBTYPES[type]) buffer.put_int32(data.bytesize) if type == :old buffer.put_bytes(data.force_encoding(BINARY)) buffer.replace_int32(position, buffer.length - position - 5) end |