Module: SourcedConfig

Defined in:
lib/sourced_config.rb,
lib/sourced_config/railtie.rb,
lib/sourced_config/s3_file.rb,
lib/sourced_config/version.rb,
lib/sourced_config/locale/loader.rb,
lib/sourced_config/config_manager.rb,
lib/sourced_config/config_contract.rb,
lib/sourced_config/locale/null_client.rb,
lib/sourced_config/locale/i18n_backend.rb,
lib/sourced_config/locale/g_sheets_client.rb,
lib/sourced_config/locale/s3_config_client.rb,
lib/generators/sourced_config/install_generator.rb

Defined Under Namespace

Modules: Generators, Locale Classes: ConfigContract, ConfigManager, Configuration, ConfigurationNotLoadedError, ConfigurationRootKeyNotFoundError, InvalidConfigurationError, Railtie, S3File

Constant Summary collapse

VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object

Get a key from the configuration



90
91
92
# File 'lib/sourced_config.rb', line 90

def [](key)
  manager.root(key)
end

.config_type_file?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/sourced_config.rb', line 47

def config_type_file?
  configuration.config_type == ConfigManager::SOURCE_TYPE_LOCAL_FILE
end

.configurationObject



17
18
19
# File 'lib/sourced_config.rb', line 17

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



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

def configure
  yield(configuration) if block_given?
  configuration
end

.dig(root, *keys) ⇒ Object

Dig lets you reach in and extract deeply nested keys from an array of keys. Note will NOT raise if the key doesnt exist, like Hash#dig etc



96
97
98
# File 'lib/sourced_config.rb', line 96

def dig(root, *keys)
  manager.root(root).dig(*keys)
end

.load!(type = SourcedConfig.configuration.config_type, source_path = SourcedConfig.configuration.configuration_file_path, force: false) ⇒ Object



81
82
83
# File 'lib/sourced_config.rb', line 81

def load!(type = SourcedConfig.configuration.config_type, source_path = SourcedConfig.configuration.configuration_file_path, force: false)
  loaded? ? manager.reload!(type, source_path, force: force) : manager.load!(type, source_path)
end

.loaded?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/sourced_config.rb', line 100

def loaded?
  manager&.loaded?
end

.setup(app) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sourced_config.rb', line 51

def setup(app)
  return if @inited

  I18n.backend = I18n::Backend::Chain.new(Locale::I18nBackend.new, I18n.backend)

  files = [configuration.base_configuration_file_path]

  files << Rails.root.join(configuration.configuration_file_path) if config_type_file?

  reloader = app.config.file_watcher.new(files) do
    Rails.logger.warn "*** Application configuration changed in #{Rails.env} " \
        "(zeitwerk: #{Rails.autoloaders.zeitwerk_enabled?})"
    SourcedConfig.load!(configuration.config_type, configuration.configuration_file_path)
  end

  app.reloaders << reloader
  app.reloader.to_run do
    # We are *not* using execute_if_updated cause if anything causes Classes to be reloaded it will kill the config
    # persisted in the class variable in the Config singleton. So we want to always execute a reload (eg when
    # I18n content changes)
    reloader.execute { require_unload_lock! }
  end

  # Load now
  reloader.execute

  @started_up ||= Time.zone.now
  @inited = true
end

.startup_timeObject



85
86
87
# File 'lib/sourced_config.rb', line 85

def startup_time
  @started_up
end