123456789_123456789_123456789_123456789_123456789_

Class: Bundler::EnvironmentPreserver

Relationships & Source Files
Inherits: Object
Defined in: lib/bundler/environment_preserver.rb

Constant Summary

Class Method Summary

Instance Method Summary

Constructor Details

.new(env, keys) ⇒ EnvironmentPreserver

Parameters:

  • env (Hash)
  • keys (Array<String>)
[ GitHub ]

  
# File 'lib/bundler/environment_preserver.rb', line 34

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

Class Method Details

.env_to_hash(env)

[ GitHub ]

  
# File 'lib/bundler/environment_preserver.rb', line 25

def self.env_to_hash(env)
  to_hash = env.to_hash
  return to_hash unless Gem.win_platform?

  to_hash.each_with_object({}) {|(k,v), a| a[k.upcase] = v }
end

.from_env

[ GitHub ]

  
# File 'lib/bundler/environment_preserver.rb', line 21

def self.from_env
  new(env_to_hash(ENV), BUNDLER_KEYS)
end

Instance Method Details

#backupHash

[ GitHub ]

  
# File 'lib/bundler/environment_preserver.rb', line 57

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

#replace_with_backup

Replaces ENV with the bundler environment variables backed up

[ GitHub ]

  
# File 'lib/bundler/environment_preserver.rb', line 41

def replace_with_backup
  unless Gem.win_platform?
    ENV.replace(backup)
    return
  end

  # Fallback logic for Windows below to workaround
  # https://bugs.ruby-lang.org/issues/16798. Can be dropped once all
  # supported rubies include the fix for that.

  ENV.clear

  backup.each {|k, v| ENV[k] = v }
end

#restoreHash

[ GitHub ]

  
# File 'lib/bundler/environment_preserver.rb', line 71

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