Class: Exercism::Rb::State

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

Constant Summary collapse

KEYS =
%w[track exercise path updated_at].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: Config.state_path) ⇒ State

Returns a new instance of State.



13
14
15
# File 'lib/exercism/rb/state.rb', line 13

def initialize(path: Config.state_path)
  @path = File.expand_path(path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/exercism/rb/state.rb', line 11

def path
  @path
end

Instance Method Details

#clearObject



38
39
40
# File 'lib/exercism/rb/state.rb', line 38

def clear
  File.delete(@path) if File.file?(@path)
end

#loadObject



17
18
19
20
21
# File 'lib/exercism/rb/state.rb', line 17

def load
  return {} unless File.file?(@path)

  parse(File.read(@path))
end

#save(track:, exercise:, path:) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/exercism/rb/state.rb', line 23

def save(track:, exercise:, path:)
  data = {
    "track" => track,
    "exercise" => exercise,
    "path" => File.expand_path(path),
    "updated_at" => Time.now.utc.iso8601
  }

  FileUtils.mkdir_p(File.dirname(@path))
  tmp_path = "#{@path}.tmp.#{Process.pid}"
  File.write(tmp_path, to_toml(data))
  File.rename(tmp_path, @path)
  data
end