Class: Unenviable::ENVWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/unenviable/env_wrapper.rb

Overview

Wraps the ENV variable to catch calls to ENV where the value is missing or not specified in Unenviable’s YAML file.

Instance Method Summary collapse

Constructor Details

#initializeENVWrapper

Returns a new instance of ENVWrapper.



5
6
7
8
9
# File 'lib/unenviable/env_wrapper.rb', line 5

def initialize
  @saved_env = ENV
  @closed = false
  @missing_descriptions = []
end

Instance Method Details

#[](key) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/unenviable/env_wrapper.rb', line 11

def [](key)
  fail "Requested ENV[#{key}] after initialization finished!" if @closed
  value = @saved_env[key]
  @missing_descriptions << key unless Unenviable.described?(key)
  return value unless value.nil?
  'MISSING VALUE'
end

#[]=(key, value) ⇒ Object



19
20
21
# File 'lib/unenviable/env_wrapper.rb', line 19

def []=(key, value)
  @saved_env[key] = value
end

#close_wrapperObject



28
29
30
31
# File 'lib/unenviable/env_wrapper.rb', line 28

def close_wrapper
  @closed = true
  fail "Requested Keys [#{@missing_descriptions.join(', ')}] which are not described!" unless @missing_descriptions.empty?
end

#restore_savedObject



23
24
25
26
# File 'lib/unenviable/env_wrapper.rb', line 23

def restore_saved
  Object.send(:remove_const, :ENV)
  Object.const_set(:ENV, @saved_env)
end