module Codebreaker class Stats PATH = './db/data.yml'.freeze def initialize @storage = FileStorage.new(PATH) end def load @storage.load || [] end def save_game(game) validate_game_is_win(game) data = load data << game_data(game) @storage.save(data) end private def game_data(game) { name: game.player.name, difficulty: game.difficulty.type, attempts_total: game.difficulty.attempts, attempts_used: game.difficulty.attempts - game.attempts_left, hints_total: game.difficulty.hints, hints_used: game.difficulty.hints - game.hints_left } end def validate_game_is_win(game) raise Errors::GameSaveError unless game.win? end end end