Class: FPM::Cookery::Environment

Inherits:
Hash
  • Object
show all
Defined in:
lib/fpm/cookery/environment.rb

Constant Summary collapse

REMOVALS =
%w(
  BUNDLE_GEMFILE RUBYOPT BUNDLE_BIN_PATH GEM_HOME GEM_PATH
).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](h = {}) ⇒ Object

Coerce keys and values to Strings on creation



11
12
13
# File 'lib/fpm/cookery/environment.rb', line 11

def self.[](h = {})
  super(Hash[h.map { |k, v| [k.to_s, v.to_s] }])
end

Instance Method Details

#[](key) ⇒ Object



15
16
17
# File 'lib/fpm/cookery/environment.rb', line 15

def [](key)
  super(key.to_s)
end

#[]=(key, value) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/fpm/cookery/environment.rb', line 19

def []=(key, value)
  if value.nil?
    delete(key.to_s)
  else
    super(key.to_s, value.to_s)
  end
end

#merge(other = {}) ⇒ Object



27
28
29
# File 'lib/fpm/cookery/environment.rb', line 27

def merge(other = {})
  super(self.class[other])
end

#merge!(other = {}) ⇒ Object



31
32
33
# File 'lib/fpm/cookery/environment.rb', line 31

def merge!(other = {})
  super(self.class[other])
end

#to_hashObject



53
54
55
# File 'lib/fpm/cookery/environment.rb', line 53

def to_hash
  Hash[self]
end

#with_cleanObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fpm/cookery/environment.rb', line 35

def with_clean
  saved_env = ENV.to_hash

  REMOVALS.each do |var|
    value = ENV.delete(var)
    Log.debug("Removing '#{var}' => '#{value}' from environment")
  end

  each do |k, v|
    Log.debug("Adding '#{k}' => '#{v}' to environment")
    ENV[k] = v
  end

  yield
ensure
  ENV.replace(saved_env.to_hash)
end