Skip to content

Commit

Permalink
Marshaling a time object added an instance variable to the object whi…
Browse files Browse the repository at this point in the history
…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
jrafanie authored and wycats committed Mar 27, 2010
1 parent 157c180 commit c1b2200
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions activesupport/lib/active_support/core_ext/time.rb
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions activesupport/test/core_ext/time_ext_test.rb
Expand Up @@ -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.