Module: Paquito::TypedStruct

Extended by:
T::Sig
Includes:
T::Props::Plugin
Defined in:
lib/paquito/typed_struct.rb

Overview

To make a T::Struct class serializable, include Paquito::TypedStruct:

class MyStruct < T::Struct
  include Paquito::TypedStruct

  prop :foo, String
  prop :bar, Integer
end

my_struct = MyStruct.new(foo: "foo", bar: 1)
my_struct.as_pack
=> [26450, "foo", 1]

MyStruct.from_pack([26450, "foo", 1])
=> <MyStruct bar=1, foo="foo">

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#as_packObject



28
29
30
31
32
33
# File 'lib/paquito/typed_struct.rb', line 28

def as_pack
  decorator = self.class.decorator
  props = decorator.props.keys
  values = props.map { |prop| decorator.get(self, prop) }
  [self.class.pack_digest, *values]
end