lib/sapience/config_loader.rb in sapience-1.0.2 vs lib/sapience/config_loader.rb in sapience-1.0.3
- old
+ new
@@ -15,22 +15,32 @@
SAPIENCE_FILE = "sapience.yml".freeze
SAPIENCE_HOME = File.realpath(File.join(File.dirname(__FILE__), "..", ".."))
DEFAULT_FILE = File.join(SAPIENCE_HOME, "config", "default.yml")
def self.load_from_file
- file_path = config_file_path
- path = File.absolute_path(file_path)
- load_yaml_configuration(path)
+ default_config = load_yaml_configuration(config_file_path)
+ application_config = load_yaml_configuration(application_config_file)
+ merge_configs(default_config, application_config)
end
+ def self.merge_configs(left_config = {}, right_config = {})
+ left_config.each do |key, config = {}|
+ right = right_config.fetch(key) { Hash.new }
+ config.merge!(right)
+ end
+
+ (right_config.keys - left_config.keys).each do |left_over_key|
+ left_config[left_over_key] = right_config[left_over_key]
+ end
+ left_config
+ end
+
class << self
private
def config_file_path
- return application_config_file if File.exist?(application_config_file)
-
- DEFAULT_FILE
+ File.absolute_path(DEFAULT_FILE)
end
def application_config_file
File.join(root_dir, "config", SAPIENCE_FILE)
end
@@ -42,9 +52,10 @@
Dir.pwd
end
end
def load_yaml_configuration(absolute_path)
+ return {} unless File.exist?(absolute_path)
text = IO.read(absolute_path, encoding: "UTF-8")
erb = ERB.new(text)
yaml_code = erb.result
hash = yaml_safe_load(yaml_code, absolute_path) || {}