Class: T::Struct

Inherits:
Object
  • Object
show all
Defined in:
lib/sorbet-schema/t/struct.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deserialize_from(serializer_type, source, options: {}) ⇒ Object



21
22
23
# File 'lib/sorbet-schema/t/struct.rb', line 21

def deserialize_from(serializer_type, source, options: {})
  T.unsafe(serializer(serializer_type, options:).deserialize(source))
end

.schemaObject



6
7
8
# File 'lib/sorbet-schema/t/struct.rb', line 6

def schema
  Typed::Schema.from_struct(self)
end

.serializer(type, options: {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/sorbet-schema/t/struct.rb', line 10

def serializer(type, options: {})
  case type
  when :hash
    Typed::HashSerializer.new(**T.unsafe({schema:, **options}))
  when :json
    Typed::JSONSerializer.new(schema:)
  else
    raise ArgumentError, "unknown serializer for #{type}"
  end
end

Instance Method Details

#serialize_to(serializer_type, options: {}) ⇒ Object



26
27
28
# File 'lib/sorbet-schema/t/struct.rb', line 26

def serialize_to(serializer_type, options: {})
  self.class.serializer(serializer_type, options:).serialize(self)
end