Module: AppConfigLoader

Defined in:
lib/app_config_loader.rb,
lib/app_config_loader/config.rb,
lib/app_config_loader/errors.rb,
lib/app_config_loader/loader.rb,
lib/app_config_loader/parser.rb,
lib/app_config_loader/railtie.rb,
lib/app_config_loader/version.rb,
lib/app_config_loader/config_map.rb,
lib/app_config_loader/config_entry.rb,
lib/app_config_loader/config_with_indifferent_access.rb

Defined Under Namespace

Classes: Config, ConfigEntry, ConfigKeyConflict, ConfigMap, ConfigWithIndifferentAccess, InvalidConfigFile, InvalidConfigKey, Loader, Parser, Railtie

Constant Summary collapse

VERSION =
'1.0.7'

Class Method Summary collapse

Class Method Details

.configure {|config| ... } ⇒ Object

Configure the module

The block will be executed at during ::init

Examples:

Change the default settings

AppConfigLoader.configure do |config|
  config.use_domain = true
  config.domain = 'us'
  config.config_paths << '/path/to/app_config.yml'
end

Yields:

  • (config)

    configuration block

Yield Parameters:



20
21
22
# File 'lib/app_config_loader.rb', line 20

def self.configure(&block)
  @cfg_blocks << block if block_given?
end

.initObject

Initialize the module This parse and load the app config into the constant specified by the configuration’s const_name property

Raises:

  • (NameError)

    the constant has already been defined



28
29
30
31
32
33
34
35
36
# File 'lib/app_config_loader.rb', line 28

def self.init
  self.run_config_blocks
  cfg = self.config

  raise NameError, "cannot assign app config because '#{cfg.const_name}' is already defined" if Object.const_defined?(cfg.const_name)
  Object.const_set cfg.const_name, self.load(self.config)

  @inited = true
end

.initialized?Boolean

Whether the module has been initialized

Returns:

  • (Boolean)

    the module is initialized



41
42
43
# File 'lib/app_config_loader.rb', line 41

def self.initialized?
  !!@inited
end

.load(config = nil) ⇒ AppConfigLoader::ConfigWithIndifferentAccess

Parse and load the app config

Examples:

Manually load app config

config = AppConfigLoader::Config.new
config.use_domain = true
config.env = 'development'
config.config_paths << '/path/to/app_config.yml'

app_config = AppConfigLoader::load(config)
app_config['some_config.key']   #=> config value for the 'some_config.key' key

Parameters:

Returns:

Raises:

  • (ArgumentError)

    configuration is invalid



61
62
63
64
# File 'lib/app_config_loader.rb', line 61

def self.load(config = nil)
  raise ArgumentError, 'config must be a AppConfigLoader::Config instance' unless config.nil? || config.is_a?(AppConfigLoader::Config)
  Loader.new(config || self.config).load
end