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



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

def initialize
  @data = {}
end

Instance Method Details

#[]Object

See Also:

  • Hash#[]


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.

Parameters:

Raises:

  • (ArgumentError)

    if key or value cannot be cast to a String



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

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

#deleteObject

See Also:

  • Hash#delete


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

def_delegators :@data, :delete, :delete_if

#delete_ifObject

See Also:

  • Hash#delete_if


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

def_delegators :@data, :delete, :delete_if

#eachObject

See Also:

  • Hash#each


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

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

#each_pairObject

See Also:

  • Hash#each_pair


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

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

#empty?Object

See Also:

  • Hash#empty?


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

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

#keysObject

See Also:

  • Hash#keys


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.

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:



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.

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:



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.

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\""]


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

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

#to_hObject

See Also:

  • Hash#to_h


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

def_delegators :@data, :to_h, :to_hash

#to_hashObject

See Also:

  • Hash#to_h


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

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
>>


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

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

#valuesObject

See Also:

  • Hash#values


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

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