Class: Yes::Core::ActiveJobSerializers::DryStructSerializer

Inherits:
ActiveJob::Serializers::ObjectSerializer
  • Object
show all
Defined in:
lib/yes/core/active_job_serializers/dry_struct_serializer.rb

Overview

ActiveJob serializer for Dry::Struct objects (including Commands).

Instance Method Summary collapse

Instance Method Details

#deserialize(hash) ⇒ Dry::Struct

Returns the deserialized object.

Parameters:

  • hash (Hash)

    the serialized representation

Returns:

  • (Dry::Struct)

    the deserialized object



22
23
24
25
26
27
28
29
30
31
# File 'lib/yes/core/active_job_serializers/dry_struct_serializer.rb', line 22

def deserialize(hash)
  symbolized_hash = hash.deep_symbolize_keys
  object = Object.const_get(symbolized_hash[:_type])

  if object < Yes::Core::Command
    deserialize_command(symbolized_hash, object)
  else
    object.new(symbolized_hash.except(:_aj_serialized, :_type))
  end
end

#serialize(dry_struct) ⇒ Hash

Returns the serialized representation.

Parameters:

  • dry_struct (Dry::Struct)

    the dry struct to serialize

Returns:

  • (Hash)

    the serialized representation



16
17
18
# File 'lib/yes/core/active_job_serializers/dry_struct_serializer.rb', line 16

def serialize(dry_struct)
  super(dry_struct.attributes.merge(_type: dry_struct.class.name).as_json)
end

#serialize?(argument) ⇒ Boolean

Returns true if the argument can be serialized.

Parameters:

  • argument (Object)

    the argument to check

Returns:

  • (Boolean)

    true if the argument can be serialized



10
11
12
# File 'lib/yes/core/active_job_serializers/dry_struct_serializer.rb', line 10

def serialize?(argument)
  argument.is_a? Dry::Struct
end