|
|
|
|
require_relative 'person'
|
|
|
|
|
require_relative 'contact'
|
|
|
|
|
|
|
|
|
|
class Student < Person
|
|
|
|
|
attr_accessor :surname, :name, :patronymic, :birth_date
|
|
|
|
|
|
|
|
|
|
def initialize(id:, git:, contact:, surname: nil, name: nil, patronymic: nil, birth_date: nil)
|
|
|
|
|
super(id: id, git: git, contact: contact)
|
|
|
|
|
@surname = surname
|
|
|
|
|
@name = name
|
|
|
|
|
@patronymic = patronymic
|
|
|
|
|
@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)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
def self.valid_name?(name)
|
|
|
|
|
NAME_REGEX = /\A[А-Яа-яЁёA-Za-z\-]+\z/
|
|
|
|
|
NAME_REGEX.match?(name)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def name_initial(name)
|
|
|
|
|
name[0].upcase
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def patronymic_initial(patronymic)
|
|
|
|
|
patronymic[0].upcase
|
|
|
|
|
end
|
|
|
|
|
end
|