Abstract environment variables
We need to use this, because `nil` is a valid value as default
# File lib/aruba/platforms/unix_environment_variables.rb, line 60 def initialize(env = ENV) @actions = [] @env = if RUBY_VERSION < '2.0' env.to_hash else env.to_h end end
Get value of variable
@param [#to_s] name
The name of the variable
# File lib/aruba/platforms/unix_environment_variables.rb, line 110 def [](name) to_h[name.to_s] end
Set value of variable
@param [#to_s] name
The name of the variable
@param [#to_s] value
The value of the variable
# File lib/aruba/platforms/unix_environment_variables.rb, line 121 def []=(name, value) value = value.to_s actions << UpdateAction.new(name.to_s => value) value end
Append value to variable
@param [#to_s] name
The name of the variable
@param [#to_s] value
The value of the variable
# File lib/aruba/platforms/unix_environment_variables.rb, line 136 def append(name, value) name = name.to_s value = self[name].to_s + value.to_s actions << UpdateAction.new(name => value ) value end
Reset environment
# File lib/aruba/platforms/unix_environment_variables.rb, line 199 def clear value = to_h actions.clear value end
Delete variable
@param [#to_s] name
The name of the variable
# File lib/aruba/platforms/unix_environment_variables.rb, line 165 def delete(name) # Rescue value, before it is deleted value = to_h[name.to_s] actions << RemoveAction.new(name.to_s) value end
Fetch variable from environment
@param [#to_s] name
The name of the variable
@param [Object] default
The default value used, if the variable is not defined
# File lib/aruba/platforms/unix_environment_variables.rb, line 90 def fetch(name, default = UNDEFINED) if default == UNDEFINED to_h.fetch name.to_s else to_h.fetch name.to_s, default end end
Check if variable exist
@param [#to_s] name
The name of the variable
# File lib/aruba/platforms/unix_environment_variables.rb, line 102 def key?(name) to_h.key? name.to_s end
Pass on checks
# File lib/aruba/platforms/unix_environment_variables.rb, line 175 def method_missing(name, *args, &block) super unless to_h.respond_to? name to_h.send name, *args, &block end
Prepend value to variable
@param [#to_s] name
The name of the variable
@param [#to_s] value
The value of the variable
# File lib/aruba/platforms/unix_environment_variables.rb, line 152 def prepend(name, value) name = name.to_s value = value.to_s + self[name].to_s actions << UpdateAction.new(name => value) value end
Check for respond_to
# File lib/aruba/platforms/unix_environment_variables.rb, line 182 def respond_to_missing?(name, _private) to_h.respond_to? name end
Convert to hash
@return [Hash]
A new hash from environment
# File lib/aruba/platforms/unix_environment_variables.rb, line 190 def to_h if RUBY_VERSION < '2.0' Marshal.load(Marshal.dump(prepared_environment.to_hash)) else Marshal.load(Marshal.dump(prepared_environment.to_h)) end end
Update environment with other en
@param [#to_hash, to_h] other_env
Another environment object or hash
@yield
Pass block to env
# File lib/aruba/platforms/unix_environment_variables.rb, line 77 def update(other_env) actions << UpdateAction.new(other_env) UnixEnvironmentVariables.new(to_h) end
# File lib/aruba/platforms/unix_environment_variables.rb, line 209 def prepared_environment if RUBY_VERSION <= '1.9.3' # rubocop:disable Style/EachWithObject actions.inject(ENV.to_hash.merge(env)) { |a, e| e.call(a) } # rubocop:enable Style/EachWithObject else actions.each_with_object(ENV.to_hash.merge(env)) { |e, a| a = e.call(a) } end end