class Aruba::Platforms::UnixEnvironmentVariables

Abstract environment variables

Constants

UNDEFINED

We need to use this, because ‘nil` is a valid value as default

Attributes

actions[R]
env[R]

Public Class Methods

hash_from_env() click to toggle source
# File lib/aruba/platforms/unix_environment_variables.rb, line 194
def self.hash_from_env
  ENV.to_hash
end
new(env = ENV) click to toggle source
# File lib/aruba/platforms/unix_environment_variables.rb, line 50
def initialize(env = ENV)
  @actions = []

  @env = env.to_h
end

Public Instance Methods

[](name) click to toggle source

Get value of variable

@param [#to_s] name

The name of the variable
# File lib/aruba/platforms/unix_environment_variables.rb, line 96
def [](name)
  to_h[name.to_s]
end
[]=(name, value) click to toggle source

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 107
def []=(name, value)
  value = value.to_s

  actions << UpdateAction.new(name.to_s => value)
end
append(name, value) click to toggle source

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 120
def append(name, value)
  name  = name.to_s
  value = self[name].to_s + value.to_s

  actions << UpdateAction.new(name => value)

  value
end
clear() click to toggle source

Reset environment

# File lib/aruba/platforms/unix_environment_variables.rb, line 179
def clear
  value = to_h

  actions.clear

  value
end
delete(name) click to toggle source

Delete variable

@param [#to_s] name

The name of the variable
# File lib/aruba/platforms/unix_environment_variables.rb, line 149
def delete(name)
  # Rescue value, before it is deleted
  value = to_h[name.to_s]

  actions << RemoveAction.new(name.to_s)

  value
end
fetch(name, default = UNDEFINED) click to toggle source

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 76
def fetch(name, default = UNDEFINED)
  if default == UNDEFINED
    to_h.fetch name.to_s
  else
    to_h.fetch name.to_s, default
  end
end
key?(name) click to toggle source

Check if variable exist

@param [#to_s] name

The name of the variable
# File lib/aruba/platforms/unix_environment_variables.rb, line 88
def key?(name)
  to_h.key? name.to_s
end
method_missing(name, *args, &block) click to toggle source

Pass on checks

Calls superclass method
# File lib/aruba/platforms/unix_environment_variables.rb, line 159
def method_missing(name, *args, &block)
  super unless to_h.respond_to? name

  to_h.send name, *args, &block
end
nest() { |self| ... } click to toggle source
# File lib/aruba/platforms/unix_environment_variables.rb, line 187
def nest
  old_actions = @actions.dup
  yield(self)
ensure
  @actions = old_actions
end
prepend(name, value) click to toggle source

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 136
def prepend(name, value)
  name  = name.to_s
  value = value.to_s + self[name].to_s

  actions << UpdateAction.new(name => value)

  value
end
respond_to_missing?(name, _private) click to toggle source

Check for respond_to

# File lib/aruba/platforms/unix_environment_variables.rb, line 166
def respond_to_missing?(name, _private)
  to_h.respond_to? name
end
to_h() click to toggle source

Convert to hash

@return [Hash]

A new hash from environment
# File lib/aruba/platforms/unix_environment_variables.rb, line 174
def to_h
  actions.inject(hash_from_env.merge(env)) { |a, e| e.call(a) }
end
update(other_env) click to toggle source

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 63
def update(other_env)
  actions << UpdateAction.new(other_env)

  self
end

Private Instance Methods

hash_from_env() click to toggle source
# File lib/aruba/platforms/unix_environment_variables.rb, line 200
def hash_from_env
  self.class.hash_from_env
end