Commit 07e7dd1b authored by Samuel E. Giddins's avatar Samuel E. Giddins

[Config] Add #with_changes

parent 1bf21c70
...@@ -24,6 +24,21 @@ module Pod ...@@ -24,6 +24,21 @@ module Pod
:cache_root => Pathname.new(Dir.home) + 'Library/Caches/CocoaPods', :cache_root => Pathname.new(Dir.home) + 'Library/Caches/CocoaPods',
} }
# Applies the given changes to the config for the duration of the given
# block.
#
def with_changes(changes)
old = {}
changes.keys.each do |key|
key = key.to_sym
old[key] = send(key) if respond_to?(key)
end
configure_with(changes)
yield if block_given?
ensure
configure_with(old)
end
public public
#-------------------------------------------------------------------------# #-------------------------------------------------------------------------#
......
...@@ -45,6 +45,48 @@ module Pod ...@@ -45,6 +45,48 @@ module Pod
#-------------------------------------------------------------------------# #-------------------------------------------------------------------------#
describe '#with_changes' do
it 'doesnt raise when using an unknown key' do
should.not.raise { @config.with_changes(:foo_bar => false) }
end
it 'uses the new value inside the block' do
@config.verbose = true
called = false
@config.with_changes(:verbose => false) do
@config.should.not.be.verbose
called = true
end
called.should.be.true
end
it 'reverts to the previous value after the block' do
@config.verbose = true
@config.with_changes(:verbose => false)
@config.should.be.verbose
end
it 'reverts to the previous value even when an exception is raised' do
@config.verbose = true
should.raise do
@config.with_changes(:verbose => false) do
raise 'foo'
end
end
@config.should.be.verbose
end
it 'returns the return value of the block' do
@config.with_changes({}) do
'foo'
end.should == 'foo'
@config.with_changes({}).should.be.nil
end
end
#-------------------------------------------------------------------------#
describe 'Paths' do describe 'Paths' do
it 'returns the working directory as the installation root if a Podfile can be found' do it 'returns the working directory as the installation root if a Podfile can be found' do
Dir.chdir(temporary_directory) do Dir.chdir(temporary_directory) do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment