module Configs

Constants

VERSION

Attributes

config_dir[W]

Where the wild .yml live. In a Rails app, this is Rails.root.join(‘config’)

environment[W]

The name of our environment. In a Rails app, this is Rails.env

Public Class Methods

[](name) click to toggle source

will find (and memoize) the yml config file with this name

cascades through a loading order to find the most specific yml file available (see Configs.load)

if none can be found, it will raise an error

# File lib/configs.rb, line 36
def [](name)
  @_configs ||= {}
  @_configs[name.to_sym] ||= load(name)
end
config_dir() click to toggle source
# File lib/configs.rb, line 20
def config_dir
  @config_dir ||= Pathname.new('./configs')
end
environment() click to toggle source
# File lib/configs.rb, line 27
def environment
  @environment ||= (ENV['RACK_ENV'] || 'default')
end
inspect() click to toggle source
# File lib/configs.rb, line 41
def inspect
  @_configs.inspect
end
reload() click to toggle source
# File lib/configs.rb, line 45
def reload
  @_configs = {}
end

Protected Class Methods

load(name) click to toggle source

checks loading order for named yml file

1) ‘config/$NAME/$ENV.yml’ 2) ‘config/$NAME.yml’ with $ENV key 3) ‘config/$NAME.yml’ with ‘default’ key

# File lib/configs.rb, line 56
def load(name)
  yml_file("#{name}/#{environment}") ||
    yml_file_with_key("#{name}", environment) ||
    yml_file("#{name}/default") ||
    yml_file_with_key("#{name}", 'default') ||
    raise(NotFound.new(name, environment))
end
yml_file(name) click to toggle source
# File lib/configs.rb, line 64
def yml_file(name)
  path = config_dir.join(name + '.yml')
  contents = ERB.new(File.read(path)).result(binding)
  YAML.load(contents).with_indifferent_access
rescue Errno::ENOENT
  # If file is missing, return nil
  nil
end
yml_file_with_key(path, key) click to toggle source
# File lib/configs.rb, line 73
def yml_file_with_key(path, key)
  hash = yml_file(path)
  hash && hash[key]
end