Class: WTT::Core::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/wtt/core/storage.rb

Overview

A utility class to store test-to-code mapping and metadata.

Instance Method Summary collapse

Constructor Details

#initialize(repo = nil, sha = nil) ⇒ Storage

Initialize the storage from given repo and sha. This reads contents from a .wtt file. When sha is not nil, contents of the file on that commit is read. Data can be written only when sha is nil (written to current working tree).



15
16
17
18
# File 'lib/wtt/core/storage.rb', line 15

def initialize(repo = nil, sha = nil)
  @repo = repo
  @sha = sha
end

Instance Method Details

#read(section) ⇒ Hash

Read data from the storage in the given section.



24
25
26
# File 'lib/wtt/core/storage.rb', line 24

def read(section)
  data_from_str(read_storage_content)[section] || {}
end

#write!(section, value) ⇒ Object

Write value to the given section in the storage. Locks the file so that concurrent write does not occur.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/wtt/core/storage.rb', line 33

def write!(section, value)
  fail 'Data cannot be written back in git history' unless @sha.nil?
  File.open(WTT::Core.wtt_root, File::RDWR | File::CREAT, 0644) do |f|
    f.flock(File::LOCK_EX)
    data = data_from_str(f.read)
    data[section] = value
    f.rewind
    f.write(data.to_json)
    f.flush
    f.truncate(f.pos)
  end
end