refactor: enhance StudentShort initialization and add from_student method

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

@ -3,29 +3,39 @@ require_relative 'person'
class StudentShort < Person class StudentShort < Person
attr_reader :surname_initials, :contact attr_reader :surname_initials, :contact
def initialize(student) def initialize(id:, git:, surname_initials:, contact:)
super(id: student.id, git: student.git_info) super(id: id, git: git, contact: contact)
@surname_initials = student.surname_and_initials @surname_initials = surname_initials
@contact = student.contact_info end
def self.from_student(student)
new(
id: student.id,
git: student.git,
surname_initials: student.surname_and_initials,
contact: student.contact_info
)
end end
def self.from_string(id, info_string) def self.from_string(id, info_string)
parts = info_string.split(',').map(&:strip) parts = info_string.split(',').map(&:strip)
raise ArgumentError, 'Invalid info string format' if parts.size < 3
surname_initials = parts[0] surname_initials = parts[0]
git = parts[1].split(': ').last git = parts[1].split(': ').last.strip
contact = parts[2].split(': ').last contact_string = parts[2].split(': ', 2).last.strip
new_instance = allocate contact = Contact.new_from_info(contact_string)
new_instance.send(:initialize_from_data, id, surname_initials, git, contact)
new_instance
end
private new(
id: id,
git: git,
surname_initials: surname_initials,
contact: contact.info
)
end
def initialize_from_data(id, surname_initials, git, contact) def to_s
@id = id "#{@surname_initials}, Git: #{@git}, Contact: #{@contact}"
@surname_initials = surname_initials
@git = git
@contact = contact
end end
end end
Loading…
Cancel
Save