Permalink
Browse files

Marshaling a time object added an instance variable to the object whi…

…ch 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.

Signed-off-by: wycats <wycats@gmail.com>
  • Loading branch information...
1 parent 157c180 commit c1b22000856412eb0f5966e9e2fbcc4d91b60208 @jrafanie jrafanie committed with wycats Jan 21, 2010
Showing with 17 additions and 2 deletions.
  1. +2 −2 activesupport/lib/active_support/core_ext/time.rb
  2. +15 −0 activesupport/test/core_ext/time_ext_test.rb
@@ -15,14 +15,14 @@ class << self
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
@@ -745,4 +745,19 @@ def test_marshaling_with_frozen_local_instance
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

0 comments on commit c1b2200

Please sign in to comment.