Class: SourcedConfig::ConfigManager

Inherits:
Object
  • Object
show all
Defined in:
lib/sourced_config/config_manager.rb

Constant Summary collapse

SOURCE_TYPE_LOCAL_FILE =
"local_file"
SOURCE_TYPE_S3_CONFIG_BUCKET =
"s3_config_bucket"

Instance Method Summary collapse

Instance Method Details

#load!(external_type, external_source_path, schema_klass: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sourced_config/config_manager.rb', line 14

def load!(external_type, external_source_path, schema_klass: nil)
  Rails.logger.info "Load configuration data from #{external_type} - #{external_source_path}" if external_type
  if loaded?
    Rails.logger.warn "An attempt to load configuration happened when it is already loaded"
    return false
  end
  primary_config = load_yaml_and_parse_erb(SourcedConfig.configuration.base_configuration_file_path).deep_symbolize_keys
  external = external_type.present? && load_external_config(external_type, external_source_path).deep_symbolize_keys

  schema = (schema_klass || SourcedConfig.configuration.config_schema_klass).new
  config_contract = schema.call(external ? primary_config.deep_merge(external) : primary_config)
  if config_contract.failure?
    messages = config_contract.errors(full: true).to_h
    Rails.logger.error "Error in configuration file! #{messages}"
    raise InvalidConfigurationError, "Failed to load configuration data! #{messages}"
  end
  @configuration = config_contract

  loaded?
end

#loaded?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/sourced_config/config_manager.rb', line 44

def loaded?
  configuration.present?
end

#reload!(external_type, external_source_path, force: false) ⇒ Object

Reload configuration files, only in development, this is hooked up to a file watcher on the config file and external directory if specified.



37
38
39
40
41
42
# File 'lib/sourced_config/config_manager.rb', line 37

def reload!(external_type, external_source_path, force: false)
  return false if Rails.env.production? && !force
  Rails.logger.warn "Something changed: Reloading application configuration!"
  @configuration = nil
  load!(external_type, external_source_path)
end

#root(key, raises: false) ⇒ Object



8
9
10
11
12
# File 'lib/sourced_config/config_manager.rb', line 8

def root(key, raises: false)
  raise ConfigurationNotLoadedError, "You cannot access config nodes until you have loaded configuration!" unless loaded?
  raise ConfigurationRootKeyNotFoundError, "Config key #{key} not found" if raises && !configuration.key?(key)
  configuration[key]
end