From f74227d30f9389b4b23a888c9af49fb9b8248e1f Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Sat, 7 Jul 2012 02:04:45 -0400 Subject: [PATCH] deep clone the hash instead of marshalling so some object types (IO, etc) do not cause subclassing to blow up --- lib/httparty/module_inheritable_attributes.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/httparty/module_inheritable_attributes.rb b/lib/httparty/module_inheritable_attributes.rb index 9f4584ad..879ca259 100644 --- a/lib/httparty/module_inheritable_attributes.rb +++ b/lib/httparty/module_inheritable_attributes.rb @@ -4,6 +4,16 @@ def self.included(base) base.extend(ClassMethods) end + # borrowed from Rails 3.2 ActiveSupport + def self.hash_deep_dup(h) + duplicate = h.dup + duplicate.each_pair do |k,v| + tv = duplicate[k] + duplicate[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? hash_deep_dup(tv) : v + end + duplicate + end + module ClassMethods #:nodoc: def mattr_inheritable(*args) @mattr_inheritable_attrs ||= [:mattr_inheritable_attrs] @@ -22,7 +32,7 @@ def inherited(subclass) if instance_variable_get(ivar).respond_to?(:merge) method = <<-EOM def self.#{inheritable_attribute} - #{ivar} = superclass.#{inheritable_attribute}.merge Marshal.load(Marshal.dump(#{ivar})) + #{ivar} = superclass.#{inheritable_attribute}.merge ModuleInheritableAttributes.hash_deep_dup(#{ivar}) end EOM subclass.class_eval method