# frozen_string_literal: true module Codebreaker module Storage PATH = 'db' FILE_NAME = 'data.yml' def save(results) FileUtils.mkdir_p(PATH) unless Dir.exist?(PATH) data = load_database << results write_data(data) end def load_database YAML.safe_load(File.read(file_path)) end def sort_player create_storage unless File.exist?(file_path) load_database.sort_by { |game| [game[:difficulty], game[:attempts_used], game[:hints_used]] } end private def file_path File.join(Dir.pwd, PATH, FILE_NAME) end def create_storage write_data([]) end def write_data(data) File.open(file_path, 'w') { |file| file.write(data.to_yaml) } end end end