Class: Exercism::Rb::State
- Inherits:
-
Object
- Object
- Exercism::Rb::State
- Defined in:
- lib/exercism/rb/state.rb
Constant Summary collapse
- KEYS =
%w[track exercise path updated_at].freeze
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize(path: Config.state_path) ⇒ State
constructor
A new instance of State.
- #load ⇒ Object
- #save(track:, exercise:, path:) ⇒ Object
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.(path) end |
Instance Attribute Details
#path ⇒ Object (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
#clear ⇒ Object
38 39 40 |
# File 'lib/exercism/rb/state.rb', line 38 def clear File.delete(@path) if File.file?(@path) end |
#load ⇒ Object
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.(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 |