Class: SkillBench::TrendTracker::Persistence
- Inherits:
-
Object
- Object
- SkillBench::TrendTracker::Persistence
- Defined in:
- lib/skill_bench/trend_tracker/persistence.rb
Overview
Handles history file persistence operations including backup management
Instance Method Summary collapse
-
#initialize(history_file) ⇒ Persistence
constructor
A new instance of Persistence.
-
#load ⇒ Array<Hash>
Loads history from file with corruption recovery.
-
#write(history) ⇒ Hash
Writes history to file with atomic operation and backup.
Constructor Details
#initialize(history_file) ⇒ Persistence
Returns a new instance of Persistence.
11 12 13 |
# File 'lib/skill_bench/trend_tracker/persistence.rb', line 11 def initialize(history_file) @history_file = File.(history_file) end |
Instance Method Details
#load ⇒ Array<Hash>
Loads history from file with corruption recovery
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/skill_bench/trend_tracker/persistence.rb', line 18 def load return [] unless File.exist?(history_file) JSON.parse(File.read(history_file), symbolize_names: true) rescue JSON::ParserError => e backup = read_backup return backup if backup SkillBench::ErrorLogger.log_error(e, "History file #{history_file} corrupted") [] end |
#write(history) ⇒ Hash
Writes history to file with atomic operation and backup. Returns a result hash so callers do not need to rescue SystemCallError.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/skill_bench/trend_tracker/persistence.rb', line 35 def write(history) json = JSON.pretty_generate(history) temp_file = "#{history_file}.tmp" File.write(temp_file, json) File.rename(temp_file, history_file) begin File.write("#{history_file}.bak", json) rescue SystemCallError => e warn "Backup write failed for #{history_file}: #{e.}" end { success: true } rescue SystemCallError => e { success: false, error: { message: e. } } end |