Sha256: d040a26ca0290443ba780167a14943ed2441786ddda4366bfda75d663f9de2e7

Contents?: true

Size: 1.22 KB

Versions: 11

Compression:

Stored size: 1.22 KB

Contents

class Exercism
  class Config

    def self.read(path)
      new(path)
    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_writer :github_username, :key, :project_dir

    def initialize(path)
      @file = File.join(path, '.exercism')
    end

    def github_username
      @github_username ||= from_yaml['github_username']
    end

    def key
      @key ||= from_yaml['key']
    end

    def project_dir
      @project_dir ||= from_yaml['project_dir']
    end

    def save
      FileUtils.mkdir_p(project_dir)
      File.open file, 'w' do |f|
        data = {
          'github_username' => github_username,
          'key' => key,
          'project_dir' => project_dir
        }
        f.write data.to_yaml
      end
      self
    end

    def delete
      FileUtils.rm(file) if File.exists?(file)
    end

    private

    def from_yaml
      unless @data
         @data = YAML.load(File.read(file))
         unless @data
           raise StandardError.new "Cannot read #{file}"
         end
      end
      @data
    end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
exercism-0.0.18 lib/exercism/config.rb
exercism-0.0.17 lib/exercism/config.rb
exercism-0.0.16 lib/exercism/config.rb
exercism-0.0.15 lib/exercism/config.rb
exercism-0.0.14 lib/exercism/config.rb
exercism-0.0.13 lib/exercism/config.rb
exercism-0.0.12 lib/exercism/config.rb
exercism-0.0.11 lib/exercism/config.rb
exercism-0.0.10 lib/exercism/config.rb
exercism-0.0.9 lib/exercism/config.rb
exercism-0.0.8 lib/exercism/config.rb