Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Auto merge of #4917 - bundler:seg-settings-temporary, r=indirect
Browse files Browse the repository at this point in the history
[Settings] Allow temporarily overriding settings in-memory
  • Loading branch information
homu committed Aug 29, 2016
2 parents 2b4b826 + 3b6dd37 commit 138dde7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
8 changes: 3 additions & 5 deletions lib/bundler/cli.rb
Expand Up @@ -184,11 +184,9 @@ def check
map "i" => "install"
def install
require "bundler/cli/install"
no_install = Bundler.settings[:no_install]
Bundler.settings[:no_install] = false if no_install == true
Install.new(options.dup).run
ensure
Bundler.settings[:no_install] = no_install unless no_install.nil?
Bundler.settings.temporary(:no_install => false) do
Install.new(options.dup).run
end
end

desc "update [OPTIONS]", "update the current environment"
Expand Down
20 changes: 19 additions & 1 deletion lib/bundler/settings.rb
Expand Up @@ -42,11 +42,18 @@ def initialize(root = nil)
@local_config = load_config(local_config_file)
@global_config = load_config(global_config_file)
@cli_flags_given = false
@temporary = {}
end

def [](name)
key = key_for(name)
value = (@local_config[key] || ENV[key] || @global_config[key] || DEFAULT_CONFIG[name])
value = @temporary.fetch(name) do
@local_config.fetch(key) do
ENV.fetch(key) do
@global_config.fetch(key) do
DEFAULT_CONFIG.fetch(name) do
nil
end end end end end

if value.nil?
nil
Expand Down Expand Up @@ -77,6 +84,17 @@ def []=(key, value)
set_key(key, value, @local_config, local_config_file)
end

def temporary(update)
existing = Hash[update.map {|k, _| [k, @temporary[k]] }]
@temporary.update(update)
return unless block_given?
begin
yield
ensure
existing.each {|k, v| v.nil? ? @temporary.delete(k) : @temporary[k] = v }
end
end

alias_method :set_local, :[]=

def delete(key)
Expand Down

0 comments on commit 138dde7

Please sign in to comment.