Class: Aruba::Platforms::UnixEnvironmentVariables

Inherits:
Object
  • Object
show all
Defined in:
lib/aruba/platforms/unix_environment_variables.rb

Overview

Abstract environment variables

Direct Known Subclasses

WindowsEnvironmentVariables

Defined Under Namespace

Classes: RemoveAction, UpdateAction

Constant Summary collapse

UNDEFINED =

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

Object.new.freeze

Instance Method Summary collapse

Constructor Details

#initialize(env = ENV) ⇒ UnixEnvironmentVariables

Returns a new instance of UnixEnvironmentVariables.


56
57
58
59
60
61
62
63
64
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 56

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

  @env = if RUBY_VERSION < '2.0'
           env.to_hash
         else
           env.to_h
         end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Pass on checks


171
172
173
174
175
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 171

def method_missing(name, *args, &block)
  super unless to_h.respond_to? name

  to_h.send name, *args, &block
end

Instance Method Details

#[](name) ⇒ Object

Get value of variable

Parameters:

  • name (#to_s)

    The name of the variable


106
107
108
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 106

def [](name)
  to_h[name.to_s]
end

#[]=(name, value) ⇒ Object

Set value of variable

Parameters:

  • name (#to_s)

    The name of the variable

  • value (#to_s)

    The value of the variable


117
118
119
120
121
122
123
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 117

def []=(name, value)
  value = value.to_s

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

  value
end

#append(name, value) ⇒ Object

Append value to variable

Parameters:

  • name (#to_s)

    The name of the variable

  • value (#to_s)

    The value of the variable


132
133
134
135
136
137
138
139
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 132

def append(name, value)
  name  = name.to_s
  value = self[name].to_s + value.to_s

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

  value
end

#clearObject

Reset environment


195
196
197
198
199
200
201
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 195

def clear
  value = to_h

  actions.clear

  value
end

#delete(name) ⇒ Object

Delete variable

Parameters:

  • name (#to_s)

    The name of the variable


161
162
163
164
165
166
167
168
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 161

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) ⇒ Object

Fetch variable from environment

Parameters:

  • name (#to_s)

    The name of the variable

  • default (Object) (defaults to: UNDEFINED)

    The default value used, if the variable is not defined


86
87
88
89
90
91
92
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 86

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) ⇒ Boolean

Check if variable exist

Parameters:

  • name (#to_s)

    The name of the variable

Returns:

  • (Boolean)

98
99
100
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 98

def key?(name)
  to_h.key? name.to_s
end

#prepend(name, value) ⇒ Object

Prepend value to variable

Parameters:

  • name (#to_s)

    The name of the variable

  • value (#to_s)

    The value of the variable


148
149
150
151
152
153
154
155
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 148

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) ⇒ Boolean

Check for respond_to

Returns:

  • (Boolean)

178
179
180
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 178

def respond_to_missing?(name, _private)
  to_h.respond_to? name
end

#to_hHash

Convert to hash

Returns:

  • (Hash)

    A new hash from environment


186
187
188
189
190
191
192
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 186

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(other_env) { ... } ⇒ Object

Update environment with other en

Parameters:

  • other_env (#to_hash, #to_h)

    Another environment object or hash

Yields:

  • Pass block to env


73
74
75
76
77
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 73

def update(other_env)
  actions << UpdateAction.new(other_env)

  UnixEnvironmentVariables.new(to_h)
end