Class: EnvironmentConfig

Inherits:
Object
  • Object
show all
Includes:
TypeFetcherMethods
Defined in:
lib/environment_config.rb,
lib/environment_config/types.rb,
lib/environment_config/builder.rb,
lib/environment_config/version.rb,
lib/environment_config/typed_env.rb,
lib/environment_config/types/json.rb,
lib/environment_config/types/yaml.rb,
lib/environment_config/types/string.rb,
lib/environment_config/types/symbol.rb,
lib/environment_config/types/boolean.rb,
lib/environment_config/types/integer.rb,
lib/environment_config/types/type_error.rb,
lib/environment_config/types/string_list.rb,
lib/environment_config/types/integer_list.rb,
lib/environment_config/type_fetcher_methods.rb

Defined Under Namespace

Modules: TypeFetcherMethods, Types Classes: Builder, TypedEnv

Constant Summary collapse

VERSION =
'2.0.1'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TypeFetcherMethods

included

Constructor Details

#initializeEnvironmentConfig

Returns a new instance of EnvironmentConfig.



27
28
29
# File 'lib/environment_config.rb', line 27

def initialize
  @store = {}
end

Class Method Details

.ensure(&block) ⇒ Object

Accepts the same block as ‘load` does, but only validates the presence and type of environment variables. Does not return a configuration.



13
14
15
16
# File 'lib/environment_config.rb', line 13

def ensure(&block)
  load(&block)
  nil
end

.load(**options) {|builder| ... } ⇒ Object

Loads a configuration from environment variables as specified by the block given (using the environment config DSL)

Yields:

  • (builder)


20
21
22
23
24
# File 'lib/environment_config.rb', line 20

def load(**options)
  builder = Builder.new(**options)
  yield builder
  builder.config
end

Instance Method Details

#store(key, value) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/environment_config.rb', line 46

def store(key, value)
  @store[key.to_s] = value

  define_accessor_method(key, value)
  return unless [true, false].include? value

  define_accessor_method("#{key}?", value)
end

#to_hObject

Raises:

  • (NotImplementedError)


31
32
33
34
35
36
# File 'lib/environment_config.rb', line 31

def to_h
  # This method solely exists for purposes of "irb discoverability"
  raise NotImplementedError,
        'Please choose between to_string_hash and to_symbol_hash, ' \
        'depending on the key type you want to get.'
end

#to_string_hashObject



38
39
40
# File 'lib/environment_config.rb', line 38

def to_string_hash
  @store.dup
end

#to_symbol_hashObject



42
43
44
# File 'lib/environment_config.rb', line 42

def to_symbol_hash
  @store.transform_keys(&:to_sym)
end