From 6d911f4e9d1e619ddc2ace581a09253b33d197d3 Mon Sep 17 00:00:00 2001 From: Darin Peshev Date: Thu, 5 Jan 2017 16:46:29 -0800 Subject: [PATCH] dup the klass config on retrieval so it does not clear between calls --- lib/table_print/config.rb | 9 ++++++++- spec/config_spec.rb | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/table_print/config.rb b/lib/table_print/config.rb index b9f385e..79e1068 100644 --- a/lib/table_print/config.rb +++ b/lib/table_print/config.rb @@ -25,7 +25,14 @@ def self.set(klass, val) end def self.for(klass) - @@klasses.fetch(klass) {} + config = @@klasses.fetch(klass) {} + if config.is_a?(Array) + config.map { |c| c.is_a?(Hash) ? c.dup : c } + elsif config.is_a?(Hash) + config.dup + else + config + end end def self.clear(klass) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 5a09932..b3550b4 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -37,6 +37,12 @@ TablePrint::Config.set(Sandbox::Blog, [:title, :author]) TablePrint::Config.for(Sandbox::Blog).should == [:title, :author] end + + it "makes a copy on retrieval" do |variable| + TablePrint::Config.set(Sandbox::Blog, [:title, :author]) + TablePrint::Config.for(Sandbox::Blog).clear + TablePrint::Config.for(Sandbox::Blog).should == [:title, :author] + end end describe "clearing" do