Class: Aruba::Platforms::WindowsEnvironmentVariables

Inherits:
UnixEnvironmentVariables show all
Defined in:
lib/aruba/platforms/windows_environment_variables.rb

Overview

Windows is case-insensitive when it accesses its environment variables.

To work around this we turn all of the environment variable keys to upper-case so that aruba is ensured that accessing environment variables with upper-case keys will always work. See the following examples.

Examples:

Setting Windows environment variables using mixed case

C:>set Path
C:>Path=.;.\bin;c:\rubys\ruby-2.1.6-p336\bin;
C:>set PATH
C:>Path=.;.\bin;c:\rubys\ruby-2.1.6-p336\bin;

If you access environment variables through ENV, you can access values no matter the case of the key:

ENV["Path"] # => ".;.\bin;c:\rubys\ruby-2.1.6-p336\bin;"
ENV["PATH"] # => ".;.\bin;c:\rubys\ruby-2.1.6-p336\bin;"

But if you copy the ENV to a hash, Ruby treats the keys as case sensitive:

env_copy = ENV.to_hash
# => {"ALLUSERSPROFILE"=>"C:\\ProgramData", "ANSICON"=>"119x1000 (119x58)", "ANSICON_DEF"=>"7",  APPDATA"=>"C:\\Users\\blorf\\AppData\\Roaming", ....}
env["Path"]
# => ".;.\bin;c:\rubys\ruby-2.1.6-p336\bin;"
env["PATH"]
# => nil

Constant Summary

Constants inherited from UnixEnvironmentVariables

UnixEnvironmentVariables::UNDEFINED

Instance Method Summary collapse

Methods inherited from UnixEnvironmentVariables

#clear, #method_missing, #respond_to_missing?, #to_h

Constructor Details

#initialize(env = ENV.to_hash) ⇒ WindowsEnvironmentVariables

Returns a new instance of WindowsEnvironmentVariables.


31
32
33
34
35
# File 'lib/aruba/platforms/windows_environment_variables.rb', line 31

def initialize(env = ENV.to_hash)
  @actions = []

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Aruba::Platforms::UnixEnvironmentVariables

Instance Method Details

#[](name) ⇒ Object


51
52
53
# File 'lib/aruba/platforms/windows_environment_variables.rb', line 51

def [](name)
  super(name.upcase)
end

#[]=(name, value) ⇒ Object


55
56
57
# File 'lib/aruba/platforms/windows_environment_variables.rb', line 55

def []=(name, value)
  super(name.upcase, value)
end

#append(name, value) ⇒ Object


59
60
61
# File 'lib/aruba/platforms/windows_environment_variables.rb', line 59

def append(name, value)
  super(name.upcase, value)
end

#delete(name) ⇒ Object


67
68
69
# File 'lib/aruba/platforms/windows_environment_variables.rb', line 67

def delete(name)
  super(name.upcase)
end

#fetch(name, default = UnixEnvironmentVariables::UNDEFINED) ⇒ Object


43
44
45
# File 'lib/aruba/platforms/windows_environment_variables.rb', line 43

def fetch(name, default = UnixEnvironmentVariables::UNDEFINED)
  super(name.upcase, default)
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)

47
48
49
# File 'lib/aruba/platforms/windows_environment_variables.rb', line 47

def key?(name)
  super(name.upcase)
end

#prepend(name, value) ⇒ Object


63
64
65
# File 'lib/aruba/platforms/windows_environment_variables.rb', line 63

def prepend(name, value)
  super(name.upcase, value)
end

#update(other_env, &block) ⇒ Object


37
38
39
40
41
# File 'lib/aruba/platforms/windows_environment_variables.rb', line 37

def update(other_env, &block)
  other_env = other_env.each_with_object({}) { |(k, v), a| a[k.to_s.upcase] = v }

  super(other_env, &block)
end