From e79d92eed34810e0de18b978a27648ddaf9b1d23 Mon Sep 17 00:00:00 2001 From: Artem Darius Weber Date: Fri, 29 Nov 2024 15:26:16 +0300 Subject: [PATCH] refactor: streamline initialization and validation in Person class --- lab2/person.rb | 123 ++++++++++++++++++++++++------------------------- 1 file changed, 60 insertions(+), 63 deletions(-) diff --git a/lab2/person.rb b/lab2/person.rb index 1dc8f9e..7fc8dad 100644 --- a/lab2/person.rb +++ b/lab2/person.rb @@ -1,65 +1,62 @@ class Person - attr_accessor :id, :git - - def initialize(args = {}) - @id = args[:id] || nil - @git = args[:git] - validate - end - - def set_contacts(phone: nil, telegram: nil, email: nil) - @phone = phone - raise ArgumentError, "Invalid phone number format: #{@phone}" if @phone && !self.class.valid_phone_number?(@phone) - - @telegram = telegram - raise ArgumentError, "Invalid telegram format: #{@telegram}" if @telegram && !self.class.valid_telegram?(@telegram) - - @email = email - raise ArgumentError, "Invalid email format: #{@email}" if @email && !self.class.valid_email?(@email) - end - - def self.valid_phone_number?(phone) - phone.match?(/\A\+?[0-9]{10,15}\z/) - end - - def self.valid_name?(name) - name.match?(/\A[А-Яа-яЁёA-Za-z\-]+\z/) - end - - def self.valid_telegram?(telegram) - telegram.nil? || telegram.match?(/\A@[A-Za-z0-9_]{5,32}\z/) - end - - def self.valid_email?(email) - email.nil? || email.match?(/\A[^@\s]+@[^@\s]+\.[^@\s]+\z/) - end - - def self.valid_git?(git) - git.nil? || git.match?(/\Ahttps:\/\/github\.com\/[A-Za-z0-9_\-]+\z/) - end - - def git_present? - !@git.nil? && !@git.empty? - end - - def contact_present? - !(@phone.nil? || @phone.empty?) || !(@telegram.nil? || @telegram.empty?) || !(@email.nil? || @email.empty?) - end - - def validate - raise ArgumentError, "Git link is required" unless git_present? - raise ArgumentError, "At least one contact (phone, telegram, or email) is required" unless contact_present? - end - - def contact_info - return "Phone: #{@phone}" if @phone - return "Telegram: #{@telegram}" if @telegram - return "Email: #{@email}" if @email - 'No contact available' - end - - private - - attr_reader :phone, :telegram, :email - attr_writer :phone, :telegram, :email + attr_accessor :id, :git + + def initialize(args = {}) + @id = args[:id] || nil + @git = args[:git] + + raise ArgumentError, "Git link is required" unless git_present? + raise ArgumentError, "At least one contact (phone, telegram, or email) is required" unless contact_present? + end + + def set_contacts(phone: nil, telegram: nil, email: nil) + @phone = phone + raise ArgumentError, "Invalid phone number format: #{@phone}" if @phone && !self.class.valid_phone_number?(@phone) + + @telegram = telegram + raise ArgumentError, "Invalid telegram format: #{@telegram}" if @telegram && !self.class.valid_telegram?(@telegram) + + @email = email + raise ArgumentError, "Invalid email format: #{@email}" if @email && !self.class.valid_email?(@email) + end + + def self.valid_phone_number?(phone) + phone.match?(/\A\+?[0-9]{10,15}\z/) + end + + def self.valid_name?(name) + name.match?(/\A[А-Яа-яЁёA-Za-z\-]+\z/) + end + + def self.valid_telegram?(telegram) + telegram.nil? || telegram.match?(/\A@[A-Za-z0-9_]{5,32}\z/) + end + + def self.valid_email?(email) + email.nil? || email.match?(/\A[^@\s]+@[^@\s]+\.[^@\s]+\z/) + end + + def self.valid_git?(git) + git.nil? || git.match?(/\Ahttps:\/\/github\.com\/[A-Za-z0-9_\-]+\z/) + end + + def git_present? + !@git.nil? && !@git.empty? + end + + def contact_present? + !(@phone.nil? || @phone.empty?) || !(@telegram.nil? || @telegram.empty?) || !(@email.nil? || @email.empty?) + end + + def contact_info + return "Phone: #{@phone}" if @phone + return "Telegram: #{@telegram}" if @telegram + return "Email: #{@email}" if @email + 'No contact available' + end + + private + + attr_reader :phone, :telegram, :email + attr_writer :phone, :telegram, :email end \ No newline at end of file