Class: Klay::Rlp::Sedes::Binary

Inherits:
Object
  • Object
show all
Defined in:
lib/klay/rlp/sedes/binary.rb

Overview

A sedes type for binary values.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min_length: 0, max_length: Constant::INFINITY, allow_empty: false) ⇒ Binary

Create a serializable bianry of variable size.

Parameters:

  • min_length (Integer) (defaults to: 0)

    the minimum size of the binary.

  • max_length (Integer) (defaults to: Constant::INFINITY)

    the maximum size of the binary.

  • allow_empty (Boolean) (defaults to: false)

    indicator wether empty binaries should be allowed.



55
56
57
58
59
# File 'lib/klay/rlp/sedes/binary.rb', line 55

def initialize(min_length: 0, max_length: Constant::INFINITY, allow_empty: false)
  @min_length = min_length
  @max_length = max_length
  @allow_empty = allow_empty
end

Class Method Details

.fixed_length(l, allow_empty: false) ⇒ Klay::Rlp::Sedes::Binary

Create a serializable bianry of fixed size.

Parameters:

  • l (Integer)

    the fixed size of the binary.

  • allow_empty (Boolean) (defaults to: false)

    indicator wether empty binaries should be allowed.

Returns:



37
38
39
# File 'lib/klay/rlp/sedes/binary.rb', line 37

def fixed_length(l, allow_empty: false)
  new(min_length: l, max_length: l, allow_empty: allow_empty)
end

.valid_type?(obj) ⇒ Boolean

Checks wether the given object is of a valid binary type.

Parameters:

  • obj (Object)

    the supposed binary item to check.

Returns:

  • (Boolean)

    true if valid.



45
46
47
# File 'lib/klay/rlp/sedes/binary.rb', line 45

def valid_type?(obj)
  obj.instance_of? String
end

Instance Method Details

#deserialize(serial) ⇒ String

Deserializes a binary.

Parameters:

  • serial (Object)

    the serialized binary.

Returns:

  • (String)

    a deserialized binary.

Raises:



80
81
82
83
84
# File 'lib/klay/rlp/sedes/binary.rb', line 80

def deserialize(serial)
  raise DeserializationError, "Objects of type #{serial.class} cannot be deserialized" unless Util.is_primitive? serial
  raise DeserializationError, "#{serial.class} has invalid length" unless valid_length? serial.size
  serial
end

#serialize(obj) ⇒ Object

Serializes a binary.

Parameters:

  • obj (String)

    the binary to serialize.

Returns:

  • (Object)

    a serialized binary.

Raises:



67
68
69
70
71
72
# File 'lib/klay/rlp/sedes/binary.rb', line 67

def serialize(obj)
  raise SerializationError, "Object is not a serializable (#{obj.class})" unless self.class.valid_type? obj
  serial = Util.str_to_bytes obj
  raise SerializationError, "Object has invalid length" unless valid_length? serial.size
  serial
end

#valid_length?(length) ⇒ Boolean

Checks wether the given length fits the defined size boundaries of the binary type.

Parameters:

  • length (Integer)

    the supposed length of the binary item.

Returns:

  • (Boolean)

    true if valid.



91
92
93
# File 'lib/klay/rlp/sedes/binary.rb', line 91

def valid_length?(length)
  (@min_length <= length && length <= @max_length) || (@allow_empty && length == 0)
end