Class: Exercism::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/exercism/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Config

Returns a new instance of Config.



19
20
21
# File 'lib/exercism/config.rb', line 19

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

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



16
17
18
# File 'lib/exercism/config.rb', line 16

def file
  @file
end

#github_usernameObject



23
24
25
# File 'lib/exercism/config.rb', line 23

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

#keyObject



27
28
29
# File 'lib/exercism/config.rb', line 27

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

#project_dirObject



31
32
33
# File 'lib/exercism/config.rb', line 31

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

Class Method Details

.read(path) ⇒ Object



4
5
6
# File 'lib/exercism/config.rb', line 4

def self.read(path)
  new(path)
end

.write(path, data) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/exercism/config.rb', line 8

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

Instance Method Details

#deleteObject



48
49
50
# File 'lib/exercism/config.rb', line 48

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

#saveObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/exercism/config.rb', line 35

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