Class: BinaryCodec::Blob

Inherits:
SerializedType show all
Defined in:
lib/binary-codec/types/blob.rb

Instance Attribute Summary

Attributes inherited from SerializedType

#bytes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SerializedType

from_bytes, from_hex, from_json, get_type_by_name, #to_byte_sink, #to_bytes, #to_hex, #to_json, #to_s

Constructor Details

#initialize(byte_buf = nil) ⇒ Blob



8
9
10
# File 'lib/binary-codec/types/blob.rb', line 8

def initialize(byte_buf = nil)
  @bytes = byte_buf || Array.new(0)
end

Class Method Details

.from(value) ⇒ Object

Raises:

  • (StandardError)


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/binary-codec/types/blob.rb', line 12

def self.from(value)
  return value if value.is_a?(Blob)

  if value.is_a?(String)
    if value !~ /^[A-F0-9]*$/i
      raise StandardError, 'Cannot construct Blob from a non-hex string'
    end
    return Blob.new(hex_to_bytes(value))
  end

  raise StandardError, 'Cannot construct Blob from value given'
end

.from_parser(parser, hint = nil) ⇒ Object



25
26
27
# File 'lib/binary-codec/types/blob.rb', line 25

def self.from_parser(parser, hint = nil)
  Blob.new(parser.read(hint))
end