lib/tomatoharvest/config.rb in tomatoharvest-0.0.1 vs lib/tomatoharvest/config.rb in tomatoharvest-0.1.0

- old
+ new

@@ -1,21 +1,38 @@ module TomatoHarvest class Config - CONFIG_PATH = File.expand_path("#{ENV['$HOME']}/.tomaconfig") - LOCAL_CONFIG_PATH = File.join(Dir.pwd, '.tomaconfig') + DIR_NAME = '.toma' - def self.load(options = {}) - if !(File.exists? CONFIG_PATH) - File.open(CONFIG_PATH, 'w') do |file| - YAML.dump({}, file) + HOME_DIR = ENV['HOME'] + GLOBAL_DIR = File.join(HOME_DIR, DIR_NAME) + LOCAL_DIR = File.join(Dir.pwd, DIR_NAME) + + def self.load + old_config = merge_config(old_config_path) + + global_path = config_path(GLOBAL_DIR) + global_config = merge_config(global_path, old_config) + + local_path = config_path(LOCAL_DIR) + merge_config(local_path, global_config) + end + + def self.merge_config(path, base = {}) + mergable = + if File.exists?(path) + YAML.load_file(path) + else + {} end - end - hash = YAML.load_file(CONFIG_PATH) - if File.exists? LOCAL_CONFIG_PATH - hash.merge!(YAML.load_file(LOCAL_CONFIG_PATH)) - end + base.merge(mergable) + end - hash + def self.config_path(directory) + File.join(directory, 'config.yaml') + end + + def self.old_config_path + File.join(HOME_DIR, '.tomaconfig') end end end