diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2018-02-09 16:50:57 -0500 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2018-02-14 13:10:08 -0500 |
commit | d9a5c7011f62dd771a2fa430090e068b1f9785f3 (patch) | |
tree | 28907c8837dd84fc02272cac1b6b94e4f69be76c /activejob | |
parent | d2d98d69468bf34a39794496beb8f9f7b69088c4 (diff) | |
download | rails-d9a5c7011f62dd771a2fa430090e068b1f9785f3.tar.gz rails-d9a5c7011f62dd771a2fa430090e068b1f9785f3.tar.bz2 rails-d9a5c7011f62dd771a2fa430090e068b1f9785f3.zip |
Add serializers for Time, Date and DateTime
Diffstat (limited to 'activejob')
7 files changed, 77 insertions, 2 deletions
diff --git a/activejob/lib/active_job/serializers.rb b/activejob/lib/active_job/serializers.rb index 9e3fcda28d..dfd654175d 100644 --- a/activejob/lib/active_job/serializers.rb +++ b/activejob/lib/active_job/serializers.rb @@ -36,6 +36,9 @@ module ActiveJob autoload :StandardTypeSerializer autoload :SymbolSerializer autoload :DurationSerializer + autoload :DateSerializer + autoload :TimeSerializer + autoload :DateTimeSerializer mattr_accessor :_additional_serializers self._additional_serializers = Set.new @@ -98,6 +101,9 @@ module ActiveJob HashSerializer, ArraySerializer, SymbolSerializer, - DurationSerializer + DurationSerializer, + DateTimeSerializer, + DateSerializer, + TimeSerializer end end diff --git a/activejob/lib/active_job/serializers/date_serializer.rb b/activejob/lib/active_job/serializers/date_serializer.rb new file mode 100644 index 0000000000..e995d30faa --- /dev/null +++ b/activejob/lib/active_job/serializers/date_serializer.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module ActiveJob + module Serializers + class DateSerializer < ObjectSerializer # :nodoc: + def serialize(date) + super("value" => date.iso8601) + end + + def deserialize(hash) + Date.iso8601(hash["value"]) + end + + private + + def klass + Date + end + end + end +end diff --git a/activejob/lib/active_job/serializers/date_time_serializer.rb b/activejob/lib/active_job/serializers/date_time_serializer.rb new file mode 100644 index 0000000000..fe780a1978 --- /dev/null +++ b/activejob/lib/active_job/serializers/date_time_serializer.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module ActiveJob + module Serializers + class DateTimeSerializer < ObjectSerializer # :nodoc: + def serialize(time) + super("value" => time.iso8601) + end + + def deserialize(hash) + DateTime.iso8601(hash["value"]) + end + + private + + def klass + DateTime + end + end + end +end diff --git a/activejob/lib/active_job/serializers/duration_serializer.rb b/activejob/lib/active_job/serializers/duration_serializer.rb index 94b0d0407a..a3c4c5d1c2 100644 --- a/activejob/lib/active_job/serializers/duration_serializer.rb +++ b/activejob/lib/active_job/serializers/duration_serializer.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ActiveJob module Serializers class DurationSerializer < ObjectSerializer # :nodoc: diff --git a/activejob/lib/active_job/serializers/symbol_serializer.rb b/activejob/lib/active_job/serializers/symbol_serializer.rb index c8900de9d6..7e1f9553a2 100644 --- a/activejob/lib/active_job/serializers/symbol_serializer.rb +++ b/activejob/lib/active_job/serializers/symbol_serializer.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ActiveJob module Serializers class SymbolSerializer < ObjectSerializer # :nodoc: diff --git a/activejob/lib/active_job/serializers/time_serializer.rb b/activejob/lib/active_job/serializers/time_serializer.rb new file mode 100644 index 0000000000..fe20772f35 --- /dev/null +++ b/activejob/lib/active_job/serializers/time_serializer.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module ActiveJob + module Serializers + class TimeSerializer < ObjectSerializer # :nodoc: + def serialize(time) + super("value" => time.iso8601) + end + + def deserialize(hash) + Time.iso8601(hash["value"]) + end + + private + + def klass + Time + end + end + end +end diff --git a/activejob/test/cases/argument_serialization_test.rb b/activejob/test/cases/argument_serialization_test.rb index 442384f2c3..8f51a2d238 100644 --- a/activejob/test/cases/argument_serialization_test.rb +++ b/activejob/test/cases/argument_serialization_test.rb @@ -13,7 +13,9 @@ 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, + :a, 1.day, Date.new(2001, 2, 3), Time.new(2002, 10, 31, 2, 2, 2, "+02:00"), + DateTime.new(2001, 2, 3, 4, 5, 6, '+03:00'), + ActiveSupport::TimeWithZone.new(Time.utc(1999, 12, 31, 23, 59, 59), ActiveSupport::TimeZone["UTC"]), [ 1, "a" ], { "a" => 1 } ].each do |arg| |