From 33da531269ed5186892dc31cd673b04cff58cc1d Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Tue, 24 Nov 2009 17:22:45 -0500 Subject: [PATCH] Fix inheritable attributes mutable by subclasses child classes should be able to reconfigure inherited attributes without stomping each other --- lib/httparty/module_inheritable_attributes.rb | 6 +++--- spec/httparty_spec.rb | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/httparty/module_inheritable_attributes.rb b/lib/httparty/module_inheritable_attributes.rb index 0afe608..ebb4bb2 100644 --- a/lib/httparty/module_inheritable_attributes.rb +++ b/lib/httparty/module_inheritable_attributes.rb @@ -16,10 +16,10 @@ module HTTParty def inherited(subclass) @mattr_inheritable_attrs.each do |inheritable_attribute| - instance_var = "@#{inheritable_attribute}" - subclass.instance_variable_set(instance_var, instance_variable_get(instance_var)) + instance_var = "@#{inheritable_attribute}" + subclass.instance_variable_set(instance_var, instance_variable_get(instance_var).clone) end end end end -end \ No newline at end of file +end diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index 140e137..718c2c6 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -308,6 +308,27 @@ describe HTTParty do end end + describe "two child classes inheriting from one parent" do + before(:each) do + @parent = Class.new do + include HTTParty + end + + @child1 = Class.new(@parent) + @child2 = Class.new(@parent) + end + + it "does not modify each others inherited attributes" do + @child1.default_params :joe => "alive" + @child2.default_params :joe => "dead" + + @child1.default_options.should == { :default_params => {:joe => "alive"} } + @child2.default_options.should == { :default_params => {:joe => "dead"} } + + @parent.default_options.should == { } + end + end + describe "#get" do it "should be able to get html" do stub_http_response_with('google.html')