From a62281362110dae9e8d04140a4ae41e59e24a96e Mon Sep 17 00:00:00 2001 From: Artem Darius Weber Date: Sat, 14 Dec 2024 13:32:06 +0300 Subject: [PATCH] feat: add new_from_info method to Contact class for creating instances from formatted strings --- lab2/contact.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lab2/contact.rb b/lab2/contact.rb index 05d8df6..89cb1d5 100644 --- a/lab2/contact.rb +++ b/lab2/contact.rb @@ -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