Add symbol and duration serializers
This commit is contained in:
parent
ea61533245
commit
b098584f63
|
@ -34,6 +34,8 @@ module ActiveJob
|
|||
autoload :HashSerializer
|
||||
autoload :ObjectSerializer
|
||||
autoload :StandardTypeSerializer
|
||||
autoload :SymbolSerializer
|
||||
autoload :DurationSerializer
|
||||
|
||||
mattr_accessor :_additional_serializers
|
||||
self._additional_serializers = Set.new
|
||||
|
@ -94,6 +96,8 @@ module ActiveJob
|
|||
StandardTypeSerializer,
|
||||
HashWithIndifferentAccessSerializer,
|
||||
HashSerializer,
|
||||
ArraySerializer
|
||||
ArraySerializer,
|
||||
SymbolSerializer,
|
||||
DurationSerializer
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
module ActiveJob
|
||||
module Serializers
|
||||
class DurationSerializer < ObjectSerializer
|
||||
class << self
|
||||
def serialize(duration)
|
||||
super("value" => duration.value, "parts" => Serializers.serialize(duration.parts))
|
||||
end
|
||||
|
||||
def deserialize(hash)
|
||||
value = hash["value"]
|
||||
parts = Serializers.deserialize(hash["parts"])
|
||||
|
||||
klass.new(value, parts)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def klass
|
||||
ActiveSupport::Duration
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,21 @@
|
|||
module ActiveJob
|
||||
module Serializers
|
||||
class SymbolSerializer < ObjectSerializer
|
||||
class << self
|
||||
def serialize(argument)
|
||||
super("value" => argument.to_s)
|
||||
end
|
||||
|
||||
def deserialize(argument)
|
||||
argument["value"].to_sym
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def klass
|
||||
Symbol
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -13,6 +13,7 @@ class ArgumentSerializationTest < ActiveSupport::TestCase
|
|||
|
||||
[ nil, 1, 1.0, 1_000_000_000_000_000_000_000,
|
||||
"a", true, false, BigDecimal.new(5),
|
||||
:a, 1.day,
|
||||
[ 1, "a" ],
|
||||
{ "a" => 1 }
|
||||
].each do |arg|
|
||||
|
@ -21,7 +22,7 @@ class ArgumentSerializationTest < ActiveSupport::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
[ :a, Object.new, self, Person.find("5").to_gid ].each do |arg|
|
||||
[ Object.new, self, Person.find("5").to_gid ].each do |arg|
|
||||
test "does not serialize #{arg.class}" do
|
||||
assert_raises ActiveJob::SerializationError do
|
||||
ActiveJob::Arguments.serialize [ arg ]
|
||||
|
|
Loading…
Reference in New Issue