refactor: update Student class initialization and validation; add StudentRepository for file operations

lab2
Artem-Darius Weber 4 weeks ago
parent 72fa11dbfa
commit d392be8d5d

@ -1,92 +1,81 @@
require_relative 'person' require_relative 'person'
require_relative 'contact'
class Student < Person class Student < Person
attr_accessor :surname, :name, :patronymic attr_accessor :surname, :name, :patronymic
def initialize(args = {}) NAME_REGEX = /\A[А-Яа-яЁёA-Za-z\-]+\z/
super(args)
@surname = args.fetch(:surname) def initialize(id:, git:, contact:, surname:, name:, patronymic:)
raise ArgumentError, "Invalid surname format: #{@surname}" unless self.class.valid_name?(@surname) super(id: id, git: git, contact: contact)
@surname = surname
@name = args.fetch(:name) @name = name
raise ArgumentError, "Invalid name format: #{@name}" unless self.class.valid_name?(@name) @patronymic = patronymic
@patronymic = args.fetch(:patronymic) validate_student
raise ArgumentError, "Invalid patronymic format: #{@patronymic}" unless self.class.valid_name?(@patronymic) end
set_contacts( def self.from_string(student_string)
phone: args[:phone], parts = student_string.split('|').map(&:strip)
telegram: args[:telegram], raise ArgumentError, 'Invalid student string format' if parts.size < 6
email: args[:email]
) name_parts = parts[0].split(' ')
end raise ArgumentError, 'Invalid name format' if name_parts.size != 3
def self.from_string(student_string) surname, name, patronymic = name_parts
parts = student_string.split('|').map(&:strip) id = parts[1].split(': ').last
surname, name, patronymic = parts[0].split(' ') phone = parts[2].split(': ').last
id = parts[1].split(': ').last.to_i telegram = parts[3].split(': ').last
phone = parts[2].split(': ').last email = parts[4].split(': ').last
telegram = parts[3].split(': ').last git = parts[5].split(': ').last
email = parts[4].split(': ').last
git = parts[5].split(': ').last contact = Contact.new(phone: phone, telegram: telegram, email: email)
new( new(
surname: surname, id: id,
name: name, git: git,
patronymic: patronymic, contact: contact,
id: id, surname: surname,
phone: phone, name: name,
telegram: telegram, patronymic: patronymic
email: email, )
git: git end
)
end def surname_and_initials
"#{@surname} #{name_initial(@name)}.#{patronymic_initial(@patronymic)}."
def self.read_from_txt(file_path) end
raise IOError, "File path is invalid or file does not exist: #{file_path}" unless File.exist?(file_path)
def to_s
students = [] "#{@surname} #{@name} #{@patronymic} | ID: #{@id} | " \
"Phone: #{@contact.phone || 'N/A'} | Telegram: #{@contact.telegram || 'N/A'} | " \
File.foreach(file_path) do |line| "Email: #{@contact.email || 'N/A'} | Git: #{@git}"
line.strip! end
next if line.empty?
def get_info
begin "#{surname_and_initials}, Git: #{@git}, Contact: #{contact_info}"
student = from_string(line) end
students << student
rescue ArgumentError => e private
puts "Error processing line: '#{line}'. Reason: #{e.message}"
end def validate_student
end raise ArgumentError, 'Surname is required' if @surname.nil? || @surname.strip.empty?
raise ArgumentError, 'Name is required' if @name.nil? || @name.strip.empty?
students raise ArgumentError, 'Patronymic is required' if @patronymic.nil? || @patronymic.strip.empty?
end
raise ArgumentError, "Invalid surname format: #{@surname}" unless valid_name?(@surname)
def self.write_to_txt(file_path, students) raise ArgumentError, "Invalid name format: #{@name}" unless valid_name?(@name)
raise ArgumentError, "Expected an array of Student objects" unless students.is_a?(Array) && students.all? { |s| s.is_a?(Student) } raise ArgumentError, "Invalid patronymic format: #{@patronymic}" unless valid_name?(@patronymic)
end
File.open(file_path, 'w') do |file|
students.each do |student| def valid_name?(name)
file.puts student.to_s NAME_REGEX.match?(name)
end end
end
def name_initial(name)
puts "Data successfully written to #{file_path}" name[0].upcase
rescue IOError => e end
puts "An error occurred while writing to the file: #{e.message}"
end def patronymic_initial(patronymic)
patronymic[0].upcase
def surname_and_initials end
"#{@surname} #{name[0]}.#{patronymic[0]}." end
end
def to_s
"#{@surname} #{@name} #{@patronymic} | ID: #{@id || 'N/A'} | " \
"Phone: #{@phone || 'N/A'} | Telegram: #{@telegram || 'N/A'} | " \
"Email: #{@email || 'N/A'} | Git: #{@git || 'N/A'}"
end
def get_info
"#{surname_and_initials}, Git: #{git_info}, Contact: #{contact_info}"
end
end

@ -0,0 +1,39 @@
require_relative 'student'
class StudentRepository
def self.read_from_txt(file_path)
raise IOError, "File does not exist: #{file_path}" unless File.exist?(file_path)
students = []
File.foreach(file_path) do |line|
line.strip!
next if line.empty?
begin
student = Student.from_string(line)
students << student
rescue ArgumentError => e
puts "Error processing line: '#{line}'. Reason: #{e.message}"
end
end
students
end
def self.write_to_txt(file_path, students)
unless students.is_a?(Array) && students.all? { |s| s.is_a?(Student) }
raise ArgumentError, 'Expected an array of Student objects'
end
File.open(file_path, 'w') do |file|
students.each do |student|
file.puts student.to_s
end
end
puts "Data successfully written to #{file_path}"
rescue IOError => e
puts "An error occurred while writing to the file: #{e.message}"
end
end
Loading…
Cancel
Save