Class: Typed::HashSerializer

Inherits:
Serializer show all
Defined in:
lib/typed/hash_serializer.rb

Constant Summary collapse

InputHash =
T.type_alias { T::Hash[T.any(Symbol, String), T.untyped] }
Input =
type_member { {fixed: InputHash} }
Output =
type_member { {fixed: Params} }

Constants inherited from Serializer

Serializer::DeserializeResult, Serializer::Params

Instance Attribute Summary collapse

Attributes inherited from Serializer

#schema

Instance Method Summary collapse

Constructor Details

#initialize(schema:, should_serialize_values: false) ⇒ HashSerializer

Returns a new instance of HashSerializer.



13
14
15
16
17
# File 'lib/typed/hash_serializer.rb', line 13

def initialize(schema:, should_serialize_values: false)
  @should_serialize_values = should_serialize_values

  super(schema: schema)
end

Instance Attribute Details

#should_serialize_valuesObject (readonly)

Returns the value of attribute should_serialize_values.



10
11
12
# File 'lib/typed/hash_serializer.rb', line 10

def should_serialize_values
  @should_serialize_values
end

Instance Method Details

#deserialize(source) ⇒ Object



20
21
22
# File 'lib/typed/hash_serializer.rb', line 20

def deserialize(source)
  deserialize_from_creation_params(HashTransformer.symbolize_keys(source))
end

#serialize(struct) ⇒ Object



25
26
27
28
29
# File 'lib/typed/hash_serializer.rb', line 25

def serialize(struct)
  return Failure.new(SerializeError.new("'#{struct.class}' cannot be serialized to target type of '#{schema.target}'.")) if struct.class != schema.target

  Success.new(serialize_from_struct(struct:, should_serialize_values:))
end