diff --git a/lib/configatron/root_store.rb b/lib/configatron/root_store.rb index dbdc3a4..4dcfb05 100644 --- a/lib/configatron/root_store.rb +++ b/lib/configatron/root_store.rb @@ -20,6 +20,8 @@ class << self def initialize @locked = false @cow = nil + @temp_level = 0 + reset! end @@ -71,17 +73,20 @@ def temp(&block) def temp_start @temp_locked = @locked @temp_cow = @cow + @temp_level += 1 # Just need to have a unique Copy-on-Write generation ID @cow = @@cow += 1 - @temp = @store + ::Kernel.instance_variable_set("@temp_#{@temp_level}", @store) end def temp_end @locked = @temp_locked @cow = @temp_cow - @store = @temp + @store = ::Kernel.instance_variable_get("@temp_#{@temp_level}") + + @temp_level -= 1 end def locked? diff --git a/test/functional/configatron.rb b/test/functional/configatron.rb index af6791a..8aa93ff 100644 --- a/test/functional/configatron.rb +++ b/test/functional/configatron.rb @@ -35,6 +35,17 @@ class Critic::Functional::ConfigatronTest < Critic::Functional::Test assert_equal('original', @kernel.foo.bar) end + it 'handles nested temps' do + @kernel.temp do + @kernel.a = 'Z' + @kernel.temp do + @kernel.a = 'Y' + end + assert_equal('Z', @kernel.a) + end + assert_equal('A', @kernel.a) + end + it 'cleans up after an exception' do @kernel.foo.bar = 'original'