aboutsummaryrefslogtreecommitdiffstats
path: root/activejob/lib/active_job/serializers/object_serializer.rb
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2018-02-12 14:16:41 -0500
committerRafael Mendonça França <rafaelmfranca@gmail.com>2018-02-14 13:10:08 -0500
commit69645cba727dfa1c18c666d2a2f1c0dedffde938 (patch)
treedc5fda6749d63a795d12459a8dc2d8994c5bfea6 /activejob/lib/active_job/serializers/object_serializer.rb
parent71721dc1c9b769d3c06317122dc88cad4a346580 (diff)
downloadrails-69645cba727dfa1c18c666d2a2f1c0dedffde938.tar.gz
rails-69645cba727dfa1c18c666d2a2f1c0dedffde938.tar.bz2
rails-69645cba727dfa1c18c666d2a2f1c0dedffde938.zip
Simplify the implementation of custom argument serializers
We can speed up things for the supported types by keeping the code in the way it was. We can also avoid to loop trough all serializers in the deserialization by trying to access the class already in the Hash. We could also speed up the custom serialization if we define the class that is going to be serialized when registering the serializers, but that will remove the possibility of defining a serialzer for a superclass and have the subclass serialized using it.
Diffstat (limited to 'activejob/lib/active_job/serializers/object_serializer.rb')
-rw-r--r--activejob/lib/active_job/serializers/object_serializer.rb28
1 files changed, 24 insertions, 4 deletions
diff --git a/activejob/lib/active_job/serializers/object_serializer.rb b/activejob/lib/active_job/serializers/object_serializer.rb
index 940b6ff95d..9f59e8236f 100644
--- a/activejob/lib/active_job/serializers/object_serializer.rb
+++ b/activejob/lib/active_job/serializers/object_serializer.rb
@@ -21,14 +21,34 @@ module ActiveJob
# Money
# end
# end
- class ObjectSerializer < BaseSerializer
+ 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)
- { OBJECT_SERIALIZER_KEY => self.class.name }.merge!(hash)
+ { Arguments::OBJECT_SERIALIZER_KEY => self.class.name }.merge!(hash)
end
- def deserialize?(argument)
- argument.is_a?(Hash) && argument[OBJECT_SERIALIZER_KEY] == self.class.name
+ # 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
end
end
end