Class: Aruba::Platforms::UnixEnvironmentVariables
- Inherits:
-
Object
- Object
- Aruba::Platforms::UnixEnvironmentVariables
- Defined in:
- lib/aruba/platforms/unix_environment_variables.rb
Overview
Abstract environment variables
Direct Known Subclasses
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
Class Method Summary collapse
Instance Method Summary collapse
-
#[](name) ⇒ Object
Get value of variable.
-
#[]=(name, value) ⇒ Object
Set value of variable.
-
#append(name, value) ⇒ Object
Append value to variable.
-
#clear ⇒ Object
Reset environment.
-
#delete(name) ⇒ Object
Delete variable.
-
#fetch(name, default = UNDEFINED) ⇒ Object
Fetch variable from environment.
-
#initialize(env = ENV) ⇒ UnixEnvironmentVariables
constructor
A new instance of UnixEnvironmentVariables.
-
#key?(name) ⇒ Boolean
Check if variable exist.
-
#method_missing(name, *args, &block) ⇒ Object
Pass on checks.
- #nest ⇒ Object
-
#prepend(name, value) ⇒ Object
Prepend value to variable.
-
#respond_to_missing?(name, _private) ⇒ Boolean
Check for respond_to.
-
#to_h ⇒ Hash
Convert to hash.
-
#update(other_env) { ... } ⇒ Object
Update environment with other en.
Constructor Details
#initialize(env = ENV) ⇒ UnixEnvironmentVariables
Returns a new instance of UnixEnvironmentVariables.
50 51 52 53 54 |
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 50 def initialize(env = ENV) @actions = [] @env = env.to_h end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Pass on checks
159 160 161 162 163 |
# 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 |
Class Method Details
.hash_from_env ⇒ Object
194 195 196 |
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 194 def self.hash_from_env ENV.to_hash end |
Instance Method Details
#[](name) ⇒ Object
Get value of variable
96 97 98 |
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 96 def [](name) to_h[name.to_s] end |
#[]=(name, value) ⇒ Object
Set value of variable
107 108 109 110 111 |
# 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) ⇒ Object
Append value to variable
120 121 122 123 124 125 126 127 |
# 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 ⇒ Object
Reset environment
179 180 181 182 183 184 185 |
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 179 def clear value = to_h actions.clear value end |
#delete(name) ⇒ Object
Delete variable
149 150 151 152 153 154 155 156 |
# 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) ⇒ Object
Fetch variable from environment
76 77 78 79 80 81 82 |
# 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) ⇒ Boolean
Check if variable exist
88 89 90 |
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 88 def key?(name) to_h.key? name.to_s end |
#nest ⇒ Object
187 188 189 190 191 192 |
# 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) ⇒ Object
Prepend value to variable
136 137 138 139 140 141 142 143 |
# 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) ⇒ Boolean
Check for respond_to
166 167 168 |
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 166 def respond_to_missing?(name, _private) to_h.respond_to? name end |
#to_h ⇒ Hash
Convert to hash
174 175 176 |
# 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) { ... } ⇒ Object
Update environment with other en
63 64 65 66 67 |
# File 'lib/aruba/platforms/unix_environment_variables.rb', line 63 def update(other_env) actions << UpdateAction.new(other_env) self end |