Class: Vanagon::Environment
- Inherits:
-
Object
- Object
- Vanagon::Environment
- Extended by:
- Forwardable
- Defined in:
- lib/vanagon/environment.rb
Overview
Environment is a validating wrapper around a delegated Hash, analogous to Ruby’s built in accessor Env. It’s intended to be used for defining multiple Environments, which can be used and manipulated the same way a bare Hash would be. We’re delegating instead of subclassing because subclassing from Ruby Core is inviting calamity – that stuff is written in C and may not correspond to assumptions you could safely make about Ruby.
Instance Method Summary collapse
- #[] ⇒ Object
-
#[]=(key, value) ⇒ Object
Associates the value given by value with the key given by key.
- #delete ⇒ Object
- #delete_if ⇒ Object
- #each ⇒ Object
- #each_pair ⇒ Object
- #empty? ⇒ Object
-
#initialize ⇒ Vanagon::Environment
constructor
Create a new Environment.
- #keys ⇒ Object
-
#merge(other_env) ⇒ Object
(also: #update)
Returns a new Environment containing the contents of other_env and the contents of env.
-
#merge!(other_env) ⇒ Object
Adds the contents of other_env to env.
-
#to_a(delim = "=") ⇒ Object
(also: #to_array)
Converts env to an array of “#key=#value” strings, suitable for joining into a command.
- #to_h ⇒ Object
- #to_hash ⇒ Object
-
#to_s ⇒ Object
(also: #to_string)
Converts env to a string by concatenating together all key-value pairs with a single space.
- #values ⇒ Object
Constructor Details
#initialize ⇒ Vanagon::Environment
Create a new Environment
47 48 49 |
# File 'lib/vanagon/environment.rb', line 47 def initialize @data = {} end |
Instance Method Details
#[] ⇒ Object
23 |
# File 'lib/vanagon/environment.rb', line 23 def_delegators :@data, :[], :keys, :values, :empty? |
#[]=(key, value) ⇒ Object
Associates the value given by value with the key given by key. Keys will be cast to Strings, and should conform to the Open Group’s guidelines for portable shell variable names:
Environment variable names used by the utilities in the Shell and
Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase
letters, digits, and the '_' (underscore) from the characters defined
in Portable Character Set and do not begin with a digit.
Values will be cast to Strings, and will be stored precisely as given, so any escaped characters, single or double quotes, or whitespace will be preserved exactly as passed during assignment.
67 68 69 |
# File 'lib/vanagon/environment.rb', line 67 def []=(key, value) @data.update({ validate_key(key) => validate_value(value) }) end |
#delete ⇒ Object
37 |
# File 'lib/vanagon/environment.rb', line 37 def_delegators :@data, :delete, :delete_if |
#delete_if ⇒ Object
37 |
# File 'lib/vanagon/environment.rb', line 37 def_delegators :@data, :delete, :delete_if |
#each ⇒ Object
29 |
# File 'lib/vanagon/environment.rb', line 29 def_delegators :@data, :each, :each_pair, :each_key, :each_value |
#each_pair ⇒ Object
29 |
# File 'lib/vanagon/environment.rb', line 29 def_delegators :@data, :each, :each_pair, :each_key, :each_value |
#empty? ⇒ Object
23 |
# File 'lib/vanagon/environment.rb', line 23 def_delegators :@data, :[], :keys, :values, :empty? |
#keys ⇒ Object
23 |
# File 'lib/vanagon/environment.rb', line 23 def_delegators :@data, :[], :keys, :values, :empty? |
#merge(other_env) ⇒ Object Also known as: update
Returns a new Environment containing the contents of other_env and the contents of env.
83 84 85 86 87 88 89 |
# File 'lib/vanagon/environment.rb', line 83 def merge(other_env) env_copy = self.dup other_env.each_pair do |k, v| env_copy[k] = v end env_copy end |
#merge!(other_env) ⇒ Object
Adds the contents of other_env to env.
103 104 105 |
# File 'lib/vanagon/environment.rb', line 103 def merge!(other_env) @data = merge(other_env).instance_variable_get(:@data) end |
#to_a(delim = "=") ⇒ Object Also known as: to_array
Converts env to an array of “#key=#value” strings, suitable for joining into a command.
118 119 120 |
# File 'lib/vanagon/environment.rb', line 118 def to_a(delim = "=") @data.map { |k, v| %(#{k}#{delim}#{v}) } end |
#to_h ⇒ Object
43 |
# File 'lib/vanagon/environment.rb', line 43 def_delegators :@data, :to_h, :to_hash |
#to_hash ⇒ Object
43 |
# File 'lib/vanagon/environment.rb', line 43 def_delegators :@data, :to_h, :to_hash |
#to_s ⇒ Object Also known as: to_string
Converts env to a string by concatenating together all key-value pairs with a single space.
133 134 135 |
# File 'lib/vanagon/environment.rb', line 133 def to_s to_a.join("\s") end |
#values ⇒ Object
23 |
# File 'lib/vanagon/environment.rb', line 23 def_delegators :@data, :[], :keys, :values, :empty? |