You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kubsu-sm5-ruby/lab2/student.rb

114 lines
3.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

require_relative 'person'
require_relative 'contact'
class Student < Person
attr_accessor :surname, :name, :patronymic, :birth_date
NAME_REGEX = /\A[А-Яа-яЁёA-Za-z\-]+\z/
def initialize(id:, git:, contact:, surname:, name:, patronymic:, birth_date:)
super(id: id, git: git, contact: contact)
self.surname = surname
self.name = name
self.patronymic = patronymic
self.birth_date = birth_date
validate_student
end
def self.from_string(student_string)
parts = student_string.split('|').map(&:strip)
raise ArgumentError, 'Invalid student string format' if parts.size < 7
name_parts = parts[0].split(' ')
raise ArgumentError, 'Invalid name format' if name_parts.size != 3
surname, name, patronymic = name_parts
id = parts[1].split(': ').last
phone = parts[2].split(': ').last
telegram = parts[3].split(': ').last
email = parts[4].split(': ').last
git = parts[5].split(': ').last
birth_date = Date.parse(parts[6].split(': ').last)
contact = Contact.new(phone: phone, telegram: telegram, email: email)
new(
id: id,
git: git,
contact: contact,
surname: surname,
name: name,
patronymic: patronymic,
birth_date: birth_date
)
end
def surname_and_initials
"#{@surname} #{name_initial(@name)}.#{patronymic_initial(@patronymic)}."
end
def to_s
"#{@surname} #{@name} #{@patronymic} | ID: #{@id} | " \
"Phone: #{@contact.phone || 'N/A'} | Telegram: #{@contact.telegram || 'N/A'} | " \
"Email: #{@contact.email || 'N/A'} | Git: #{@git} | Birth Date: #{@birth_date}"
end
def get_info
"#{surname_and_initials}, Git: #{@git}, Contact: #{contact_info}, Birth Date: #{@birth_date}"
end
def surname=(surname)
raise ArgumentError, 'Surname is required' if surname.nil? || surname.strip.empty?
raise ArgumentError, "Invalid surname format: #{surname}" unless valid_name?(surname)
@surname = surname
end
def name=(name)
raise ArgumentError, 'Name is required' if name.nil? || name.strip.empty?
raise ArgumentError, "Invalid name format: #{name}" unless valid_name?(name)
@name = name
end
def patronymic=(patronymic)
raise ArgumentError, 'Patronymic is required' if patronymic.nil? || patronymic.strip.empty?
raise ArgumentError, "Invalid patronymic format: #{patronymic}" unless valid_name?(patronymic)
@patronymic = patronymic
end
def birth_date=(birth_date)
raise ArgumentError, 'Birth date is required' if birth_date.nil?
raise ArgumentError, "Invalid birth date: #{birth_date}" unless birth_date.is_a?(Date)
@birth_date = birth_date
end
private
def validate_student
raise ArgumentError, 'Surname is required' if @surname.nil? || @surname.strip.empty?
raise ArgumentError, 'Name is required' if @name.nil? || @name.strip.empty?
raise ArgumentError, 'Patronymic is required' if @patronymic.nil? || @patronymic.strip.empty?
raise ArgumentError, 'Birth date is required' if @birth_date.nil?
raise ArgumentError, "Invalid surname format: #{@surname}" unless valid_name?(@surname)
raise ArgumentError, "Invalid name format: #{@name}" unless valid_name?(@name)
raise ArgumentError, "Invalid patronymic format: #{@patronymic}" unless valid_name?(@patronymic)
end
def valid_name?(name)
NAME_REGEX.match?(name)
end
def name_initial(name)
name[0].upcase
end
def patronymic_initial(patronymic)
patronymic[0].upcase
end
end