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/type_fetcher_methods.rb

Defined Under Namespace

Modules: TypeFetcherMethods, Types Classes: Builder, TypedEnv

Constant Summary collapse

VERSION =
'1.2.0'.freeze

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_arguments, &block) ⇒ Object



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

def method_missing(method_name, *_arguments, &block)
  return fetch method_name if known_key?(method_name)

  super
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

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(method_name, _include_private = false)
  known_key?(method_name) || super
end

#store(key, value) ⇒ Object



56
57
58
# File 'lib/environment_config.rb', line 56

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

#to_hObject

Raises:

  • (NotImplementedError)


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

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



48
49
50
# File 'lib/environment_config.rb', line 48

def to_string_hash
  @store.dup
end

#to_symbol_hashObject



52
53
54
# File 'lib/environment_config.rb', line 52

def to_symbol_hash
  @store.map { |k, v| [k.to_sym, v] }.to_h
end