Class: Moped::BSON::Binary
Overview
Represents binary data in the BSON specification.
Constant Summary collapse
- SUBTYPE_MAP =
{ generic: 0.chr, function: 1.chr, old: 2.chr, uuid: 3.chr, md5: 5.chr, user: 128.chr }.freeze
- SUBTYPE_TYPES =
SUBTYPE_MAP.invert.freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.__bson_load__(io) ⇒ Binary
Load the BSON from the raw data to a binary.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
(also: #eql?)
Check equality on the object.
-
#__bson_dump__(io, key) ⇒ Object
Dump the binary into it’s raw bytes.
-
#hash ⇒ Fixnum
Gets the hash code for the object.
-
#initialize(type, data) ⇒ Binary
constructor
Create the new binary type.
-
#inspect ⇒ String
Gets the string inspection for the object.
-
#to_s ⇒ String
Get the string representation of the object.
Constructor Details
#initialize(type, data) ⇒ Binary
Create the new binary type.
83 84 85 86 |
# File 'lib/moped/bson/binary.rb', line 83 def initialize(type, data) @type = type @data = data end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
18 19 20 |
# File 'lib/moped/bson/binary.rb', line 18 def data @data end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
18 19 20 |
# File 'lib/moped/bson/binary.rb', line 18 def type @type end |
Class Method Details
.__bson_load__(io) ⇒ Binary
Load the BSON from the raw data to a binary.
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/moped/bson/binary.rb', line 124 def __bson_load__(io) length, = io.read(4).unpack(INT32_PACK) type = SUBTYPE_TYPES[io.read(1)] if type == :old length -= 4 io.read(4) end data = io.read(length) new(type, data) end |
Instance Method Details
#==(other) ⇒ true, false Also known as: eql?
Check equality on the object.
56 57 58 |
# File 'lib/moped/bson/binary.rb', line 56 def ==(other) BSON::Binary === other && data == other.data && type == other.type end |
#__bson_dump__(io, key) ⇒ Object
Dump the binary into it’s raw bytes.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/moped/bson/binary.rb', line 29 def __bson_dump__(io, key) io << Types::BINARY io << key io << NULL_BYTE if type == :old io << [data.bytesize + 4].pack(INT32_PACK) io << SUBTYPE_MAP[type] io << [data.bytesize].pack(INT32_PACK) io << data else io << [data.bytesize].pack(INT32_PACK) io << SUBTYPE_MAP[type] io << data end end |
#hash ⇒ Fixnum
Gets the hash code for the object.
69 70 71 |
# File 'lib/moped/bson/binary.rb', line 69 def hash [data, type].hash end |
#inspect ⇒ String
Gets the string inspection for the object.
96 97 98 |
# File 'lib/moped/bson/binary.rb', line 96 def inspect "#<#{self.class.name} type=#{type.inspect} length=#{data.bytesize}>" end |
#to_s ⇒ String
Get the string representation of the object.
108 109 110 |
# File 'lib/moped/bson/binary.rb', line 108 def to_s data.to_s end |