Class: top_level_module::DeepCover::Persistence
- Inherits:
-
Object
- Object
- top_level_module::DeepCover::Persistence
- Defined in:
- lib/deep_cover/persistence.rb
Constant Summary collapse
- BASENAME =
'coverage.dc'
- TRACKER_TEMPLATE =
'trackers%{unique}.dct'
Instance Attribute Summary collapse
-
#dir_path ⇒ Object
readonly
Returns the value of attribute dir_path.
Class Method Summary collapse
Instance Method Summary collapse
- #clear_directory ⇒ Object
- #delete_trackers ⇒ Object
-
#initialize(cache_directory) ⇒ Persistence
constructor
A new instance of Persistence.
-
#load_trackers ⇒ Object
returns a TrackerHitsPerPath.
- #merge_persisted_trackers ⇒ Object
- #save_trackers(tracker_hits_per_path) ⇒ Object
Constructor Details
#initialize(cache_directory) ⇒ Persistence
Returns a new instance of Persistence.
17 18 19 |
# File 'lib/deep_cover/persistence.rb', line 17 def initialize(cache_directory) @dir_path = Pathname(cache_directory). end |
Instance Attribute Details
#dir_path ⇒ Object (readonly)
Returns the value of attribute dir_path.
16 17 18 |
# File 'lib/deep_cover/persistence.rb', line 16 def dir_path @dir_path end |
Class Method Details
.merge_tracker_hits_per_paths(*tracker_hits_per_path_hashes) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/deep_cover/persistence.rb', line 63 def self.merge_tracker_hits_per_paths(*tracker_hits_per_path_hashes) return {} if tracker_hits_per_path_hashes.empty? result = tracker_hits_per_path_hashes[0].transform_values(&:dup) tracker_hits_per_path_hashes[1..-1].each do |tracker_hits_per_path| tracker_hits_per_path.each do |path, tracker_hits| matching_result = result[path] if matching_result.nil? result[path] = tracker_hits.dup next end if matching_result.size != tracker_hits.size raise "Attempting to merge trackers of different sizes: #{matching_result.size} vs #{tracker_hits.size}, for path #{path}" end tracker_hits.each_with_index do |nb_hits, i| matching_result[i] += nb_hits end end end result end |
Instance Method Details
#clear_directory ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/deep_cover/persistence.rb', line 55 def clear_directory delete_trackers begin dir_path.rmdir rescue SystemCallError # rubocop:disable Lint/HandleExceptions end end |
#delete_trackers ⇒ Object
51 52 53 |
# File 'lib/deep_cover/persistence.rb', line 51 def delete_trackers tracker_files.each(&:delete) end |
#load_trackers ⇒ Object
returns a TrackerHitsPerPath
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/deep_cover/persistence.rb', line 32 def load_trackers tracker_hits_per_path_hashes = tracker_files.map do |full_path| JSON.parse(full_path.binread).transform_keys(&:to_sym).yield_self do |version:, tracker_hits_per_path:| raise "dump version mismatch: #{version}, currently #{VERSION}" unless version == VERSION tracker_hits_per_path end end self.class.merge_tracker_hits_per_paths(*tracker_hits_per_path_hashes) end |
#merge_persisted_trackers ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/deep_cover/persistence.rb', line 43 def merge_persisted_trackers tracker_hits_per_path = load_trackers return if tracker_hits_per_path.empty? tracker_files_before = tracker_files save_trackers(tracker_hits_per_path) tracker_files_before.each(&:delete) end |
#save_trackers(tracker_hits_per_path) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/deep_cover/persistence.rb', line 21 def save_trackers(tracker_hits_per_path) create_directory_if_needed basename = format(TRACKER_TEMPLATE, unique: SecureRandom.urlsafe_base64) dir_path.join(basename).binwrite(JSON.dump( version: VERSION, tracker_hits_per_path: tracker_hits_per_path, )) end |