Class: Vanagon::Environment

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeVanagon::Environment

Create a new Environment



46
47
48
# File 'lib/vanagon/environment.rb', line 46

def initialize
  @data = {}
end

Instance Method Details

#[]Object

See Also:

  • Hash#[]


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.

Parameters:

Raises:

  • (ArgumentError)

    if key or value cannot be cast to a String



66
67
68
# File 'lib/vanagon/environment.rb', line 66

def []=(key, value)
  @data.update({ validate_key(key) => validate_value(value) })
end

#deleteObject

See Also:

  • Hash#delete


36
# File 'lib/vanagon/environment.rb', line 36

def_delegators :@data, :delete, :delete_if

#delete_ifObject

See Also:

  • Hash#delete_if


36
# File 'lib/vanagon/environment.rb', line 36

def_delegators :@data, :delete, :delete_if

#eachObject

See Also:

  • Hash#each


28
# File 'lib/vanagon/environment.rb', line 28

def_delegators :@data, :each, :each_pair, :each_key, :each_value

#each_pairObject

See Also:

  • Hash#each_pair


28
# File 'lib/vanagon/environment.rb', line 28

def_delegators :@data, :each, :each_pair, :each_key, :each_value

#empty?Object

See Also:

  • Hash#empty?


22
# File 'lib/vanagon/environment.rb', line 22

def_delegators :@data, :[], :keys, :values, :empty?

#keysObject

See Also:

  • Hash#keys


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.

Examples:

Merge two Environments

>> local = Vanagon::Environment.new
=> #<Vanagon::Environment:0x007fc54d913f38 @data={}>
>> global = Vanagon::Environment.new
=> #<Vanagon::Environment:0x007fc54b06da70 @data={}>
>> local['PATH'] = '/usr/local/bin:/usr/bin:/bin'
>> global['CC'] = 'ccache gcc'
>> local.merge global
=> #<Vanagon::Environment:0x007fc54b0a72e8 @data={"PATH"=>"/usr/local/bin:/usr/bin:/bin", "CC"=>"ccache gcc"}>

Parameters:



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.

Examples:

Merge two Environments

>> local = Vanagon::Environment.new
=> #<Vanagon::Environment:0x007f8c68933b08 @data={}>
>> global = Vanagon::Environment.new
=> #<Vanagon::Environment:0x007f8c644e5640 @data={}>
>> local['PATH'] = '/usr/local/bin:/usr/bin:/bin'
>> global['CC'] = 'ccache gcc'
>> local.merge! global
=> #<Vanagon::Environment:0x007f8c68933b08 @data={"PATH"=>"/usr/local/bin:/usr/bin:/bin", "CC"=>"ccache gcc"}>

Parameters:



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.

Examples:

Convert to an Array

>> local = Vanagon::Environment.new
=> #<Vanagon::Environment:0x007f8c68991258 @data={}>
>> local['PATH'] = '/usr/local/bin:/usr/bin:/bin'
=> "/usr/local/bin:/usr/bin:/bin"
>> local['CC'] = 'clang'
=> "clang"
>> local.to_a
=> ["PATH=\"/usr/local/bin:/usr/bin:/bin\"", "CC=\"clang\""]


117
118
119
# File 'lib/vanagon/environment.rb', line 117

def to_a(delim = "=")
  @data.map { |k, v| %(#{k}#{delim}#{v}) }
end

#to_hObject

See Also:

  • Hash#to_h


42
# File 'lib/vanagon/environment.rb', line 42

def_delegators :@data, :to_h, :to_hash

#to_hashObject

See Also:

  • Hash#to_h


42
# File 'lib/vanagon/environment.rb', line 42

def_delegators :@data, :to_h, :to_hash

#to_sObject Also known as: to_string

Converts env to a string by concatenating together all key-value pairs with a single space.

Examples:

Convert to an Array

>> local = Vanagon::Environment.new
=> #<Vanagon::Environment:0x007f8c68014358 @data={}>
>> local['PATH'] = '/usr/local/bin:/usr/bin:/bin'
>> local['CC'] = 'clang'
>> puts local
PATH=/usr/local/bin:/usr/bin:/bin
>>


132
133
134
# File 'lib/vanagon/environment.rb', line 132

def to_s
  to_a.join("\s")
end

#valuesObject

See Also:

  • Hash#values


22
# File 'lib/vanagon/environment.rb', line 22

def_delegators :@data, :[], :keys, :values, :empty?