From 14ad00c50edb7c6eb5f08e78e67913681023f163 Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Thu, 21 Jan 2010 16:56:04 -0500 Subject: [PATCH] Marshaling a time object added an instance variable to the object which affected the quoting of serialized attributes because the to_yaml of the original object did not match the to_yaml of the marshaled one. Also, Marshal.dump was modifying the source object which the client may not be aware of. --- activesupport/lib/active_support/core_ext/time.rb | 4 ++-- activesupport/test/core_ext/time_ext_test.rb | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/time.rb b/activesupport/lib/active_support/core_ext/time.rb index 78bbfc9..a8feb1a 100644 --- a/activesupport/lib/active_support/core_ext/time.rb +++ b/activesupport/lib/active_support/core_ext/time.rb @@ -15,14 +15,14 @@ class Time alias_method :_original_load, :_load def _load(marshaled_time) time = _original_load(marshaled_time) - utc = time.instance_variable_get('@marshal_with_utc_coercion') + utc = time.send(:remove_instance_variable, '@marshal_with_utc_coercion') if time.instance_variable_defined?('@marshal_with_utc_coercion') utc ? time.utc : time end end alias_method :_original_dump, :_dump def _dump(*args) - obj = self.frozen? ? self.dup : self + obj = self.dup obj.instance_variable_set('@marshal_with_utc_coercion', utc?) obj._original_dump(*args) end diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 7b8173d..50dbc00 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -741,4 +741,19 @@ class TimeExtMarshalingTest < Test::Unit::TestCase assert_equal t, unmarshaled assert_equal t.zone, unmarshaled.zone end + + def test_marshaling_does_not_modify_source_object + t = Time.local(2000) + Marshal.dump t + assert_equal false, t.instance_variable_defined?('@marshal_with_utc_coercion') + end + + def test_marshaling_does_not_affect_yaml_dump + t = Time.local(2000) + t2 = t.dup + marshaled = Marshal.dump t2 + unmarshaled = Marshal.load marshaled + assert_equal t.to_yaml, unmarshaled.to_yaml + end + end -- 1.6.5