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

103 lines
2.8 KiB

require_relative 'person'
class Student < Person
attr_accessor :surname, :name, :patronymic, :birth_date
private
NAME_REGEX = /\A[А-Яа-яЁёA-Za-z\-]+\z/
public
def initialize(id:, git:, surname:, name:, patronymic:, birth_date:, phone: nil, telegram: nil, email: nil)
super(id: id, git: git, phone: phone, telegram: telegram, email: email)
self.surname = surname
self.name = name
self.patronymic = patronymic
self.birth_date = birth_date
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)
new(
id: id,
git: git,
surname: surname,
name: name,
patronymic: patronymic,
birth_date: birth_date,
phone: phone,
telegram: telegram,
email: email
)
end
def surname_and_initials
"#{@surname} #{name_initial(@name)}.#{patronymic_initial(@patronymic)}."
end
def to_s
"#{@surname} #{@name} #{@patronymic} | ID: #{@id} | " \
"Phone: #{@phone || 'N/A'} | Telegram: #{@telegram || 'N/A'} | " \
"Email: #{@email || 'N/A'} | Git: #{@git} | Birth Date: #{@birth_date}"
end
def get_info
"#{surname_and_initials}, Git: #{@git}, Contact: #{get_first_contact}, 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 self.class.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 self.class.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 self.class.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
def self.valid_name?(name)
NAME_REGEX.match?(name)
end
private
def name_initial(name)
name[0].upcase
end
def patronymic_initial(patronymic)
patronymic[0].upcase
end
end