-
-
Save retr0h/179886 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
::AppConfig = ApplicationConfiguration.new("main.yml", "main_local.yml") | |
AppConfig.use_environment!(Rails.env) | |
## main.yml | |
defaults: &defaults | |
one: won | |
two: too | |
web_service: | |
url: http://foo | |
port: 80 | |
development: | |
<<: *defaults | |
## main_local.yml | |
web_service: | |
port: 8080 | |
#### My app_config lib | |
class AppConfiguration | |
class << self | |
CONFIG_BASE = File.join(RAILS_ROOT, 'config', 'app_config') | |
def app_config_configuration(environment) | |
configs = load_config_file(app_config_configuration_file) | |
hashes = [configs['default'], configs[environment]] | |
begin | |
local_config = load_config_file(app_config_override_file) | |
hashes << local_config | |
rescue | |
nil ### local config optional. | |
end | |
hashes.compact! | |
hashes.inject({}){|aggregate,current| recursive_merge(aggregate, current)} | |
end | |
protected | |
def load_config_file(file) | |
YAML::load(ERB.new(IO.read(file)).result(binding)) | |
rescue => e | |
raise "ERROR: loading '#{file}': #{e}" | |
end | |
def app_config_configuration_file | |
"#{CONFIG_BASE}.yml" | |
end | |
def app_config_override_file | |
"#{CONFIG_BASE}_local.yml" | |
end | |
## | |
# Taken from http://github.com/cjbottaro/app_config/tree/master | |
def recursive_merge(h1, h2) | |
h1.merge(h2){|k, v1, v2| v2.kind_of?(Hash) ? recursive_merge(v1, v2) : v2} | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment