lib/exercism/config.rb in exercism-0.0.20 vs lib/exercism/config.rb in exercism-0.0.21

- old
+ new

@@ -1,5 +1,7 @@ +require 'yaml/store' + class Exercism class Config def self.alternate_path File.join(Env.home, '.config') @@ -38,19 +40,11 @@ end def save FileUtils.mkdir_p(project_dir) FileUtils.mkdir_p(path) - - File.open file, 'w' do |f| - data = { - 'github_username' => github_username, - 'key' => key, - 'project_dir' => project_dir - } - f.write data.to_yaml - end + save_to_file self end def delete FileUtils.rm(file) if exists? @@ -64,24 +58,40 @@ @file ||= File.join(path, filename) end private + def save_to_file + data = { + 'github_username' => github_username, + 'key' => key, + 'project_dir' => project_dir + } + + store.transaction do + data.each_pair do |k,v| + store[k] = v + end + end + end + + def store + @store ||= YAML::Store.new(file) + end + def filename default? ? ".exercism" : "exercism" end def default? path !~ /\.config/ end def from_yaml - unless @data - @data = YAML.load(File.read(file)) - unless @data - raise StandardError.new "Cannot read #{file}" - end - end + @data ||= store.load(File.read(file)) + unless @data + raise StandardError.new "Cannot read #{file}" + end @data end end end