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'
class StudentShort < Person
attr_reader :surname_initials, :contact
def initialize(student)
super(id: student.id, git: student.git_info)
@surname_initials = student.surname_and_initials
@contact = student.contact_info
end
def self.from_string(id, info_string)
parts = info_string.split(', ').map(&:strip)
surname_initials = parts[0]
git = parts[1].split(': ').last
contact = parts[2].split(': ').last
attr_reader :surname_initials, :contact
def initialize(id:, git:, surname_initials:, contact:)
super(id: id, git: git, contact: contact)
@surname_initials = surname_initials
end
def self.from_student(student)
new(
id: student.id,
git: student.git,
surname_initials: student.surname_and_initials,
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
new_instance.send(:initialize_from_data, id, surname_initials, git, contact)
new_instance
end
surname_initials = parts[0]
git = parts[1].split(': ').last.strip
contact_string = parts[2].split(': ', 2).last.strip
private
contact = Contact.new_from_info(contact_string)
def initialize_from_data(id, surname_initials, git, contact)
@id = id
@surname_initials = surname_initials
@git = git
@contact = contact
end
end
new(
id: id,
git: git,
surname_initials: surname_initials,
contact: contact.info
)
end
def to_s
"#{@surname_initials}, Git: #{@git}, Contact: #{@contact}"
end
end

Loading…
Cancel
Save