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_short.rb

61 lines
1.6 KiB

require_relative 'person'
class StudentShort < Person
attr_reader :surname_initials
def initialize(id:, git:, surname_initials:, phone: nil, telegram: nil, email: nil)
super(id: id, git: git, phone: phone, telegram: telegram, email: email)
@surname_initials = surname_initials
end
def self.from_student(student)
new(
id: student.id,
git: student.git,
surname_initials: student.surname_and_initials,
phone: student.phone,
telegram: student.telegram,
email: student.email
)
end
def self.from_string(id, info_string)
parts = info_string.split(',').map(&:strip)
raise ArgumentError, 'Invalid info string format' if parts.size < 3
surname_initials = parts[0]
git = parts[1].split(': ').last.strip
contact_string = parts[2].split(': ', 2).last.strip
phone, telegram, email = parse_contact_string(contact_string)
new(
id: id,
git: git,
surname_initials: surname_initials,
phone: phone,
telegram: telegram,
email: email
)
end
def to_s
contact_info = contact_info()
"#{@surname_initials}, Git: #{@git}, Contact: #{contact_info}"
end
private
def self.parse_contact_string(contact_string)
case contact_string
when /\APhone: (.+)\z/i
[Regexp.last_match(1).strip, nil, nil]
when /\ATelegram: (.+)\z/i
[nil, Regexp.last_match(1).strip, nil]
when /\AEmail: (.+)\z/i
[nil, nil, Regexp.last_match(1).strip]
else
raise ArgumentError, "Invalid contact string format: #{contact_string}"
end
end
end