diff options
Diffstat (limited to 'activejob/lib/active_job')
-rw-r--r-- | activejob/lib/active_job/serializers/base_serializer.rb | 6 | ||||
-rw-r--r-- | activejob/lib/active_job/serializers/object_serializer.rb | 19 |
2 files changed, 25 insertions, 0 deletions
diff --git a/activejob/lib/active_job/serializers/base_serializer.rb b/activejob/lib/active_job/serializers/base_serializer.rb index 2e510781cf..155eeb29c3 100644 --- a/activejob/lib/active_job/serializers/base_serializer.rb +++ b/activejob/lib/active_job/serializers/base_serializer.rb @@ -2,6 +2,7 @@ module ActiveJob module Serializers + # Implement the basic interface for Active Job arguments serializers. class BaseSerializer include Singleton @@ -9,24 +10,29 @@ module ActiveJob delegate :serialize?, :deserialize?, :serialize, :deserialize, to: :instance end + # Determines if an argument should be serialized by a serializer. def serialize?(argument) argument.is_a?(klass) end + # Determines if an argument should be deserialized by a serializer. def deserialize?(_argument) raise NotImplementedError end + # Serializes an argument to a JSON primitive type. def serialize(_argument) raise NotImplementedError end + # Deserilizes an argument form a JSON primiteve type. def deserialize(_argument) raise NotImplementedError end protected + # The class of the object that will be serialized. def klass raise NotImplementedError end diff --git a/activejob/lib/active_job/serializers/object_serializer.rb b/activejob/lib/active_job/serializers/object_serializer.rb index 318eabebdf..940b6ff95d 100644 --- a/activejob/lib/active_job/serializers/object_serializer.rb +++ b/activejob/lib/active_job/serializers/object_serializer.rb @@ -2,6 +2,25 @@ module ActiveJob module Serializers + # Base class for serializing and deserializing custom times. + # + # Example + # + # class MoneySerializer < ActiveJob::Serializers::ObjectSerializer + # def serialize(money) + # super("cents" => money.cents, "currency" => money.currency) + # end + # + # def deserialize(hash) + # Money.new(hash["cents"], hash["currency"]) + # end + # + # private + # + # def klass + # Money + # end + # end class ObjectSerializer < BaseSerializer def serialize(hash) { OBJECT_SERIALIZER_KEY => self.class.name }.merge!(hash) |