blob: be69ddf90cb0b9c77ac87f7167928d7f85dba3a9 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# frozen_string_literal: true
module ActiveJob
module Serializers
# Base class for serializing and deserializing custom objects.
#
# Example:
#
# class MoneySerializer < ActiveJob::Serializers::ObjectSerializer
# def serialize(money)
# super("amount" => money.amount, "currency" => money.currency)
# end
#
# def deserialize(hash)
# Money.new(hash["amount"], hash["currency"])
# end
#
# private
#
# def klass
# Money
# end
# end
class ObjectSerializer
include Singleton
class << self
delegate :serialize?, :serialize, :deserialize, to: :instance
end
# Determines if an argument should be serialized by a serializer.
def serialize?(argument)
argument.is_a?(klass)
end
# Serializes an argument to a JSON primitive type.
def serialize(hash)
{ Arguments::OBJECT_SERIALIZER_KEY => self.class.name }.merge!(hash)
end
# Deserializes an argument from a JSON primitive type.
def deserialize(_argument)
raise NotImplementedError
end
private
# The class of the object that will be serialized.
def klass # :doc:
raise NotImplementedError
end
end
end
end
|