refactor: enhance StudentShort initialization and add from_student method

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

@ -1,31 +1,41 @@
require_relative 'person' 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
end
def self.from_student(student)
def self.from_string(id, info_string) new(
parts = info_string.split(', ').map(&:strip) id: student.id,
surname_initials = parts[0] git: student.git,
git = parts[1].split(': ').last surname_initials: student.surname_and_initials,
contact = parts[2].split(': ').last contact: student.contact_info
)
end
def self.from_string(id, info_string)
parts = info_string.split(',').map(&:strip)
raise ArgumentError, 'Invalid info string format' if parts.size < 3
new_instance = allocate surname_initials = parts[0]
new_instance.send(:initialize_from_data, id, surname_initials, git, contact) git = parts[1].split(': ').last.strip
new_instance contact_string = parts[2].split(': ', 2).last.strip
end
private contact = Contact.new_from_info(contact_string)
def initialize_from_data(id, surname_initials, git, contact) new(
@id = id id: id,
@surname_initials = surname_initials git: git,
@git = git surname_initials: surname_initials,
@contact = contact contact: contact.info
end )
end end
def to_s
"#{@surname_initials}, Git: #{@git}, Contact: #{@contact}"
end
end

Loading…
Cancel
Save