Skip to content

Instantly share code, notes, and snippets.

@chsh
Created February 16, 2012 07:01
Show Gist options
  • Save chsh/1842811 to your computer and use it in GitHub Desktop.
Save chsh/1842811 to your computer and use it in GitHub Desktop.
configuration gem configurations loader. :-)
# place this file under Rails.root/config/initializers/configuration.rb
#
# load configurations from Rails.root/config/environments.
#
# load order is:
# 0. application.rb
# 1. #{Rails.env}.rb
# 2. app(1|2).rb
# 3. app(1|2)_#{rails.env}.rb
# 4. app(1|2)_local.rb
# 5. local.rb
ef = File.join(Rails.root, 'config', 'environments')
environments = Dir.glob("#{ef}/*.rb").map { |f| File.basename(f).gsub(/\.rb$/, '') }
environments_regex = "(#{environments.join('|')})"
path = File.join(Rails.root, 'config', 'settings')
files = Dir.glob("#{path}/*.rb")
loads = [[], [], [], [], [], []]
files.each do |file|
bn = File.basename(file)
case bn
when 'application.rb' then loads[0] << file
when /^.+_#{Rails.env}\.rb$/ then loads[3] << file
when /^.+_local\.rb$/ then loads[4] << file
when 'local.rb' then loads[5] << file
when "#{Rails.env}.rb" then loads[1] << file
else
loads[2] << file unless bn =~ /^(.+_)?#{environments_regex}\.rb$/
end
end
loads.each do |slot|
slot.sort.each do |f|
Kernel.load f
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment