lib/exercism/config.rb in exercism-0.0.18 vs lib/exercism/config.rb in exercism-0.0.19

- old
+ new

@@ -1,25 +1,32 @@ class Exercism class Config + def self.alternate_path + File.join(Env.home, '.config') + end + def self.read(path) - new(path) + config = new(path) + return config if config.exists? + + new(alternate_path) unless config.exists? end def self.write(path, data) config = new(path) config.github_username = data['github_username'] config.key = data['key'] config.project_dir = data['project_dir'] config.save end - attr_reader :file + attr_reader :path attr_writer :github_username, :key, :project_dir def initialize(path) - @file = File.join(path, '.exercism') + @path = path end def github_username @github_username ||= from_yaml['github_username'] end @@ -32,10 +39,12 @@ @project_dir ||= from_yaml['project_dir'] 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 @@ -44,13 +53,29 @@ end self end def delete - FileUtils.rm(file) if File.exists?(file) + FileUtils.rm(file) if exists? end + def exists? + File.exists?(file) + end + + def file + @file ||= File.join(path, filename) + end + private + + def filename + default? ? ".exercism" : "exercism" + end + + def default? + path !~ /\.config/ + end def from_yaml unless @data @data = YAML.load(File.read(file)) unless @data