1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activejob/lib/active_job/serializers/object_serializer.rb
Ryuta Kamizono c81af6ae72 Enable Layout/EmptyLinesAroundAccessModifier cop
We sometimes say "✂️ newline after `private`" in a code review (e.g.
https://github.com/rails/rails/pull/18546#discussion_r23188776,
https://github.com/rails/rails/pull/34832#discussion_r244847195).

Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style
`EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059).

That cop and enforced style will reduce the our code review cost.
2019-06-13 12:00:45 +09:00

53 lines
1.3 KiB
Ruby

# 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