Class: Opto::Setters::Env

Inherits:
Opto::Setter show all
Defined in:
lib/opto/setters/environment_variable.rb

Overview

Set a value to environment.

Hint should be a name of environment variable, such as 'HOME'

Everything will be converted to strings unless hint is a hash with :options. (also include :name in that case)

Instance Attribute Summary collapse

Attributes inherited from Opto::Setter

#hint, #option

Instance Method Summary collapse

Methods inherited from Opto::Setter

for, inherited, #initialize, target, #target, targets

Constructor Details

This class inherits a constructor from Opto::Setter

Instance Attribute Details

#dont_stringifyObject

Returns the value of attribute dont_stringify


15
16
17
# File 'lib/opto/setters/environment_variable.rb', line 15

def dont_stringify
  @dont_stringify
end

#env_nameObject

Returns the value of attribute env_name


15
16
17
# File 'lib/opto/setters/environment_variable.rb', line 15

def env_name
  @env_name
end

Instance Method Details

#normalize_hintObject

Raises:

  • (ArgumentError)

17
18
19
20
21
22
23
24
25
# File 'lib/opto/setters/environment_variable.rb', line 17

def normalize_hint
  raise ArgumentError, "Environment variable name not set" if hint.nil?
  if hint.kind_of?(Hash)
    raise ArgumentError, "Environment variable name not set" unless hint[:name]
    @env_name = hint[:name].to_s
  else
    @env_name = hint.to_s
  end
end

#set(value) ⇒ Object


27
28
29
30
# File 'lib/opto/setters/environment_variable.rb', line 27

def set(value)
  normalize_hint
  ENV[env_name] = value.to_s
end