module AppConfigLoader

Constants

VERSION

Public Class Methods

configure(&block) click to toggle source

Configure the module

The block will be executed at during ::init

@yield [config] configuration block @yieldparam [AppConfigLoader::Config] config the config object

@example Change the default settings

AppConfigLoader.configure do |config|
  config.use_domain = true
  config.domain = 'us'
  config.config_paths << '/path/to/app_config.yml'
end
# File lib/app_config_loader.rb, line 20
def self.configure(&block)
  @cfg_blocks << block if block_given?
end
init() click to toggle source

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

@raise [NameError] the constant has already been defined

# 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?() click to toggle source

Whether the module has been initialized

@return [Boolean] the module is initialized

# File lib/app_config_loader.rb, line 41
def self.initialized?
  !!@inited
end
load(config = nil) click to toggle source

Parse and load the app config

@param [AppConfigLoader::Config] config configuration to use when loading the app config

@raise [ArgumentError] configuration is invalid

@return [AppConfigLoader::ConfigWithIndifferentAccess] app config map

@example 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
# 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

Private Class Methods

config() click to toggle source
# File lib/app_config_loader.rb, line 75
def self.config
  @config ||= self.default_config
end
default_config() click to toggle source
# File lib/app_config_loader.rb, line 79
def self.default_config
  cfg = AppConfigLoader::Config.new

  cfg.const_name = 'APP_CONFIG'

  if defined?(Rails)
    cfg.env = Rails.env || ENV['RACK_ENV'] || ENV['RUBY_ENV'] || 'development'
    cfg.config_paths << Rails.root.join('config', 'app_configs')
    cfg.local_overrides = Rails.root.join('config', 'app_configs', 'local_overrides.yml')
  else
    cfg.env = ENV['RACK_ENV'] || ENV['RUBY_ENV'] || 'development'
    cfg.config_paths << File.join(Dir.pwd, 'app_configs')
    cfg.local_overrides = File.join(Dir.pwd, 'config', 'local_overrides.yml')
  end

  cfg
end
run_config_blocks() click to toggle source
# File lib/app_config_loader.rb, line 68
def self.run_config_blocks
  @config = @cfg_blocks.reduce(self.config) do |cfg, cfg_block|
    cfg_block.call cfg
    cfg
  end
end