Class: TTNT::Storage
- Inherits:
-
Object
- Object
- TTNT::Storage
- Defined in:
- lib/ttnt/storage.rb
Overview
A utility class to store TTNT data such as test-to-code mapping and metadata.
Instance Method Summary collapse
- #filename ⇒ Object private
-
#initialize(repo, sha = nil) ⇒ Storage
constructor
Initialize the storage from given repo and sha.
-
#read(section) ⇒ Hash
Read data in section from the storage.
- #read_storage_content ⇒ Object private
-
#write!(section, value) ⇒ Object
Write value to section in the storage.
Constructor Details
#initialize(repo, sha = nil) ⇒ Storage
Initialize the storage from given repo and sha. This reads contents from a file .ttnt. 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).
12 13 14 15 |
# File 'lib/ttnt/storage.rb', line 12 def initialize(repo, sha = nil) @repo = repo @sha = sha end |
Instance Method Details
#filename ⇒ Object (private)
51 52 53 |
# File 'lib/ttnt/storage.rb', line 51 def filename "#{@repo.workdir}/.ttnt" end |
#read(section) ⇒ Hash
Read data in section from the storage.
21 22 23 24 25 26 27 28 |
# File 'lib/ttnt/storage.rb', line 21 def read(section) str = read_storage_content if str.length > 0 JSON.parse(str)[section] || {} else {} end end |
#read_storage_content ⇒ Object (private)
55 56 57 58 59 60 61 |
# File 'lib/ttnt/storage.rb', line 55 def read_storage_content if @sha @repo.lookup(@repo.lookup(@sha).tree['.ttnt'][:oid]).content else File.exist?(filename) ? File.read(filename) : '' end end |
#write!(section, value) ⇒ Object
Write value to section in the storage. Locks the file so that concurrent write does not occur.
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ttnt/storage.rb', line 35 def write!(section, value) raise 'Data cannot be written to the storage back in git history' unless @sha.nil? File.open(filename, File::RDWR|File::CREAT, 0644) do |f| f.flock(File::LOCK_EX) str = f.read data = str.length > 0 ? JSON.parse(str) : {} data[section] = value f.rewind f.write(data.to_json) f.flush f.truncate(f.pos) end end |