Class: Bundler::EnvironmentPreserver

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

Instance Method Summary collapse

Constructor Details

#initialize(env, keys) ⇒ EnvironmentPreserver

Returns a new instance of EnvironmentPreserver.

Parameters:

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


6
7
8
9
10
# File 'lib/bundler/environment_preserver.rb', line 6

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

Instance Method Details

#backupHash

Returns:

  • (Hash)


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bundler/environment_preserver.rb', line 13

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

#restoreHash

Returns:

  • (Hash)


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bundler/environment_preserver.rb', line 26

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