feat: add new_from_info method to Contact class for creating instances from formatted strings

lab2
Artem-Darius Weber 4 weeks ago
parent d392be8d5d
commit a622813621

@ -49,4 +49,19 @@ class Contact
raise ArgumentError, "Invalid telegram format: #{@telegram}" if @telegram && !valid_telegram?
raise ArgumentError, "Invalid email format: #{@email}" if @email && !valid_email?
end
def self.new_from_info(info_string)
info_string = info_string.sub(/^Contact: /, '').strip
case info_string
when /\APhone: (.+)\z/i
new(phone: Regexp.last_match(1).strip)
when /\ATelegram: (.+)\z/i
new(telegram: Regexp.last_match(1).strip)
when /\AEmail: (.+)\z/i
new(email: Regexp.last_match(1).strip)
else
raise ArgumentError, "Invalid contact info format: #{info_string}"
end
end
end

Loading…
Cancel
Save