Module: ActiveJob::Serializers

Defined in:
lib/rails_6_polyfills/activejob/serializers.rb

Overview

The ActiveJob::Serializers module is used to store a list of known serializers and to add new ones. It also has helpers to serialize/deserialize objects.

Defined Under Namespace

Classes: DurationSerializer, ObjectSerializer, SymbolSerializer

Class Method Summary collapse

Class Method Details

.add_serializers(*new_serializers) ⇒ Object

Adds new serializers to a list of known serializers.



179
180
181
# File 'lib/rails_6_polyfills/activejob/serializers.rb', line 179

def add_serializers(*new_serializers)
  self._additional_serializers += new_serializers.flatten
end

.deserialize(argument) ⇒ Object

Returns deserialized object. Will look up through all known serializers. If no serializer found will raise ArgumentError.

Raises:

  • (ArgumentError)


163
164
165
166
167
168
169
170
171
# File 'lib/rails_6_polyfills/activejob/serializers.rb', line 163

def deserialize(argument)
  serializer_name = argument[Arguments::OBJECT_SERIALIZER_KEY]
  raise ArgumentError, "Serializer name is not present in the argument: #{argument.inspect}" unless serializer_name

  serializer = serializer_name.safe_constantize
  raise ArgumentError, "Serializer #{serializer_name} is not known" unless serializer

  serializer.deserialize(argument)
end

.serialize(argument) ⇒ Object

Returns serialized representative of the passed object. Will look up through all known serializers. Raises ActiveJob::SerializationError if it can’t find a proper serializer.

Raises:

  • (SerializationError)


154
155
156
157
158
# File 'lib/rails_6_polyfills/activejob/serializers.rb', line 154

def serialize(argument)
  serializer = serializers.detect { |s| s.serialize?(argument) }
  raise SerializationError.new("Unsupported argument type: #{argument.class.name}") unless serializer
  serializer.serialize(argument)
end

.serializersObject

Returns list of known serializers.



174
175
176
# File 'lib/rails_6_polyfills/activejob/serializers.rb', line 174

def serializers
  self._additional_serializers # standard:disable Style/RedundantSelf
end