Class: Klay::Rlp::Sedes::Binary
- Inherits:
-
Object
- Object
- Klay::Rlp::Sedes::Binary
- Defined in:
- lib/klay/rlp/sedes/binary.rb
Overview
A sedes type for binary values.
Class Method Summary collapse
-
.fixed_length(l, allow_empty: false) ⇒ Klay::Rlp::Sedes::Binary
Create a serializable bianry of fixed size.
-
.valid_type?(obj) ⇒ Boolean
Checks wether the given object is of a valid binary type.
Instance Method Summary collapse
-
#deserialize(serial) ⇒ String
Deserializes a binary.
-
#initialize(min_length: 0, max_length: Constant::INFINITY, allow_empty: false) ⇒ Binary
constructor
Create a serializable bianry of variable size.
-
#serialize(obj) ⇒ Object
Serializes a binary.
-
#valid_length?(length) ⇒ Boolean
Checks wether the given length fits the defined size boundaries of the binary type.
Constructor Details
#initialize(min_length: 0, max_length: Constant::INFINITY, allow_empty: false) ⇒ Binary
Create a serializable bianry of variable size.
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.
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.
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.
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.
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.
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 |