module LiteConfig

Copied from raw.github.com/mikel/mail/master/lib/mail/indifferent_hash.rb which itself is a copy from active_support

Constants

VERSION

Public Instance Methods

app_env=(app_env) click to toggle source
# File lib/lite_config.rb, line 27
def app_env=(app_env)
  raise ImmutableError, "app_env is frozen after the first file load" unless @configs.nil?

  @app_env = app_env
end
config_path=(path) click to toggle source
# File lib/lite_config.rb, line 21
def config_path=(path)
  raise ImmutableError, "config_path is frozen after the first file load" unless @configs.nil?

  @config_path = path
end
fetch(name) click to toggle source
# File lib/lite_config.rb, line 15
def fetch(name)
  name = name.to_sym
  @configs ||= {}
  @configs.key?(name) ? @configs[name] : (@configs[name] = IndifferentHash.new(load(name)))
end
reset() click to toggle source
# File lib/lite_config.rb, line 33
def reset
  @configs = nil
end

Private Instance Methods

app_env() click to toggle source
# File lib/lite_config.rb, line 83
def app_env
  @app_env ||=
  if defined?(Rails)
    Rails.env
  elsif ENV['RAILS_ENV']
    ENV['RAILS_ENV']
  elsif ENV['RACK_ENV']
    ENV['RACK_ENV']
  else
    'development'
  end
end
app_root() click to toggle source
# File lib/lite_config.rb, line 79
def app_root
  defined?(Rails) ? Rails.root : `pwd`.strip
end
config_filename(name) click to toggle source
# File lib/lite_config.rb, line 69
def config_filename(name)
  filename = File.join(config_path, name.to_s + '.yml')
  filename << '.erb' if File.exist?(filename + '.erb')
  filename
end
config_path() click to toggle source
# File lib/lite_config.rb, line 65
def config_path
  @config_path ||= File.join(app_root, 'config')
end
has_environmenty_key?(hash) click to toggle source
# File lib/lite_config.rb, line 96
def has_environmenty_key?(hash)
  %w(development test production).any?{ |envy| hash.key?(envy) } if hash
end
load(name) click to toggle source
# File lib/lite_config.rb, line 39
def load(name)
  if File.exist?(config_filename(name))
    config = load_single(config_filename(name))
  else
    raise NotFoundError, "No config found for #{name}"
  end

  if File.exist?(local_config_filename(name))
    local_config = load_single(local_config_filename(name))

    config.deep_merge!(local_config) if local_config
  end

  config
end
load_single(filename) click to toggle source
# File lib/lite_config.rb, line 55
def load_single(filename)
  hash = if File.extname(filename) == '.erb'
    YAML.load ERB.new(IO.read(filename)).result
  else
    YAML.load_file filename
  end

  has_environmenty_key?(hash) ? hash[app_env] : hash
end
local_config_filename(name) click to toggle source
# File lib/lite_config.rb, line 75
def local_config_filename(name)
  config_filename(name).gsub(/.yml$/, '_local.yml')
end