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

82 lines
2.2 KiB

require_relative 'person'
require_relative 'contact'
class Student < Person
attr_accessor :surname, :name, :patronymic
NAME_REGEX = /\A[А-Яа-яЁёA-Za-z\-]+\z/
def initialize(id:, git:, contact:, surname:, name:, patronymic:)
super(id: id, git: git, contact: contact)
@surname = surname
@name = name
@patronymic = patronymic
validate_student
end
def self.from_string(student_string)
parts = student_string.split('|').map(&:strip)
raise ArgumentError, 'Invalid student string format' if parts.size < 6
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
contact = Contact.new(phone: phone, telegram: telegram, email: email)
new(
id: id,
git: git,
contact: contact,
surname: surname,
name: name,
patronymic: patronymic
)
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}"
end
def get_info
"#{surname_and_initials}, Git: #{@git}, Contact: #{contact_info}"
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, "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