require 'yaml' class CrossPost class Config DEFAULT_CONFIG = File.join Dir.home, '.config/cross-post/config.yml' def initialize @file = ENV.fetch 'CROSS_POST_CONFIG', DEFAULT_CONFIG raise 'Unable to find config file' unless File.readable? @file File.open(@file) { |f| @config = YAML.safe_load f } end def [](key) current = @config key.split(/\./).each do |k| current = current[k] return nil if current.nil? end current end def []=(key, value) *key, last = key.split(/\./) current = @config key.each do |k| next_ = current[k] case next_ when nil next_ = current[k] = {} when Hash else raise "Invalid entry, Hash expected, had #{next_.class} (#{next_})" end current = next_ end current[last] = value end def save File.write @file, YAML.dump(@config) end end end