Skip to content

Commit

Permalink
deep clone the hash instead of marshalling so some object types (IO, …
Browse files Browse the repository at this point in the history
…etc) do not cause subclassing to blow up
  • Loading branch information
joshgoebel committed Jul 7, 2012
1 parent 9c08f47 commit f74227d
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/httparty/module_inheritable_attributes.rb
Expand Up @@ -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]
Expand All @@ -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
Expand Down

0 comments on commit f74227d

Please sign in to comment.