# students_list_yaml.rb require 'yaml' require 'date' require_relative 'students_list_base' require_relative '../student' class StudentsListYAML < StudentsListBase def load_from_file if File.exist?(@filename) begin data = YAML.load_file(@filename) @students = data.map { |student_hash| hash_to_student(student_hash) } rescue StandardError => e warn "Ошибка при загрузке YAML: #{e.message}" @students = [] end else @students = [] end self end def save_to_file data = @students.map { |student| student_to_hash(student) } File.open(@filename, 'w') do |file| file.write(data.to_yaml) end self rescue IOError => e warn "Ошибка при записи в YAML: #{e.message}" self end private def student_to_hash(student) { 'id' => student.id, 'git' => student.git, 'surname' => student.surname, 'name' => student.name, 'patronymic' => student.patronymic, 'birth_date' => student.birth_date.to_s, 'phone' => student.phone, 'telegram' => student.telegram, 'email' => student.email } end def hash_to_student(hash) Student.new( id: hash['id'], git: hash['git'], surname: hash['surname'], name: hash['name'], patronymic: hash['patronymic'], birth_date: Date.parse(hash['birth_date']), phone: hash['phone'], telegram: hash['telegram'], email: hash['email'] ) end end