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
46 47 48 |
# File 'lib/vanagon/environment.rb', line 46 def initialize @data = {} end |
Instance Method Details
#[] ⇒ Object
22 |
# File 'lib/vanagon/environment.rb', line 22 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.
66 67 68 |
# File 'lib/vanagon/environment.rb', line 66 def []=(key, value) @data.update({ validate_key(key) => validate_value(value) }) end |
#delete ⇒ Object
36 |
# File 'lib/vanagon/environment.rb', line 36 def_delegators :@data, :delete, :delete_if |
#delete_if ⇒ Object
36 |
# File 'lib/vanagon/environment.rb', line 36 def_delegators :@data, :delete, :delete_if |
#each ⇒ Object
28 |
# File 'lib/vanagon/environment.rb', line 28 def_delegators :@data, :each, :each_pair, :each_key, :each_value |
#each_pair ⇒ Object
28 |
# File 'lib/vanagon/environment.rb', line 28 def_delegators :@data, :each, :each_pair, :each_key, :each_value |
#empty? ⇒ Object
22 |
# File 'lib/vanagon/environment.rb', line 22 def_delegators :@data, :[], :keys, :values, :empty? |
#keys ⇒ Object
22 |
# File 'lib/vanagon/environment.rb', line 22 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.
82 83 84 85 86 87 88 |
# File 'lib/vanagon/environment.rb', line 82 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.
102 103 104 |
# File 'lib/vanagon/environment.rb', line 102 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.
117 118 119 |
# File 'lib/vanagon/environment.rb', line 117 def to_a(delim = "=") @data.map { |k, v| %(#{k}#{delim}#{v}) } end |
#to_h ⇒ Object
42 |
# File 'lib/vanagon/environment.rb', line 42 def_delegators :@data, :to_h, :to_hash |
#to_hash ⇒ Object
42 |
# File 'lib/vanagon/environment.rb', line 42 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.
132 133 134 |
# File 'lib/vanagon/environment.rb', line 132 def to_s to_a.join("\s") end |
#values ⇒ Object
22 |
# File 'lib/vanagon/environment.rb', line 22 def_delegators :@data, :[], :keys, :values, :empty? |