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



31
32
33
# File 'lib/sorbet-schema/t/struct.rb', line 31

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
20
21
22
23
24
25
26
27
28
29
# 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:)
  when :csv
    Typed::CSVSerializer.new(schema:)
  when :yml
    Typed::YMLSerializer.new(schema:)
  when :msgpack
    Typed::MessagePackSerializer.new(schema:)
  when :activerecord
    raise ArgumentError, "activerecord gem is required for ActiveRecord serialization" unless defined?(ActiveRecord)

    Typed::ActiveRecordSerializer.new(**T.unsafe({schema:, **options}))
  else
    raise ArgumentError, "unknown serializer for #{type}"
  end
end

Instance Method Details

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



36
37
38
# File 'lib/sorbet-schema/t/struct.rb', line 36

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