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, #value_of

Constructor Details

#initialize(byte_buf = nil) ⇒ Blob

Returns a new instance of Blob.



6
7
8
# File 'lib/binary-codec/types/blob.rb', line 6

def initialize(byte_buf = nil)
  super(byte_buf || [])
end

Class Method Details

.from(value) ⇒ Blob

Creates a new Blob instance from a value.

Parameters:

  • value (Blob, String, Array<Integer>)

    The value to convert.

Returns:

  • (Blob)

    The created instance.

Raises:

  • (StandardError)


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

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

  if value.is_a?(String)
    unless valid_hex?(value)
      raise StandardError, 'Cannot construct Blob from a non-hex string'
    end
    return Blob.new(hex_to_bytes(value))
  end

  if value.is_a?(Array)
    return Blob.new(value)
  end

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

.from_parser(parser, hint = nil) ⇒ Blob

Creates a Blob instance from a parser.

Parameters:

  • parser (BinaryParser)

    The parser to read from.

  • hint (Integer, nil) (defaults to: nil)

    Optional width hint.

Returns:

  • (Blob)

    The created instance.



34
35
36
# File 'lib/binary-codec/types/blob.rb', line 34

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