Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nesting temp #103

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/configatron/root_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class << self
def initialize
@locked = false
@cow = nil
@temp_level = 0

reset!
end

Expand Down Expand Up @@ -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?
Expand Down
11 changes: 11 additions & 0 deletions test/functional/configatron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down