Class: Bundler::EnvironmentPreserver

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/environment_preserver.rb

Constant Summary collapse

INTENTIONALLY_NIL =
"BUNDLER_ENVIRONMENT_PRESERVER_INTENTIONALLY_NIL".freeze
BUNDLER_KEYS =
%w[
  BUNDLE_BIN_PATH
  BUNDLE_GEMFILE
  BUNDLER_VERSION
  GEM_HOME
  GEM_PATH
  MANPATH
  PATH
  RB_USER_INSTALL
  RUBYLIB
  RUBYOPT
].map(&:freeze).freeze
BUNDLER_PREFIX =
"BUNDLER_ORIG_".freeze

Instance Method Summary collapse

Constructor Details

#initialize(env, keys) ⇒ EnvironmentPreserver

Returns a new instance of EnvironmentPreserver.

Parameters:

  • env (ENV)
  • keys (Array<String>)


22
23
24
25
26
# File 'lib/bundler/environment_preserver.rb', line 22

def initialize(env, keys)
  @original = env.to_hash
  @keys = keys
  @prefix = BUNDLER_PREFIX
end

Instance Method Details

#backupHash

Returns:

  • (Hash)


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bundler/environment_preserver.rb', line 29

def backup
  env = @original.clone
  @keys.each do |key|
    value = env[key]
    if !value.nil? && !value.empty?
      env[@prefix + key] ||= value
    elsif value.nil?
      env[@prefix + key] ||= INTENTIONALLY_NIL
    end
  end
  env
end

#restoreHash

Returns:

  • (Hash)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bundler/environment_preserver.rb', line 43

def restore
  env = @original.clone
  @keys.each do |key|
    value_original = env[@prefix + key]
    next if value_original.nil? || value_original.empty?
    if value_original == INTENTIONALLY_NIL
      env.delete(key)
    else
      env[key] = value_original
    end
    env.delete(@prefix + key)
  end
  env
end