refactor: streamline initialization and validation in Person class

lab2
Artem-Darius Weber 1 month ago
parent dc752cdf43
commit e79d92eed3

@ -1,65 +1,62 @@
class Person class Person
attr_accessor :id, :git attr_accessor :id, :git
def initialize(args = {}) def initialize(args = {})
@id = args[:id] || nil @id = args[:id] || nil
@git = args[:git] @git = args[:git]
validate
end
def set_contacts(phone: nil, telegram: nil, email: nil) raise ArgumentError, "Git link is required" unless git_present?
@phone = phone raise ArgumentError, "At least one contact (phone, telegram, or email) is required" unless contact_present?
raise ArgumentError, "Invalid phone number format: #{@phone}" if @phone && !self.class.valid_phone_number?(@phone) end
@telegram = telegram def set_contacts(phone: nil, telegram: nil, email: nil)
raise ArgumentError, "Invalid telegram format: #{@telegram}" if @telegram && !self.class.valid_telegram?(@telegram) @phone = phone
raise ArgumentError, "Invalid phone number format: #{@phone}" if @phone && !self.class.valid_phone_number?(@phone)
@email = email @telegram = telegram
raise ArgumentError, "Invalid email format: #{@email}" if @email && !self.class.valid_email?(@email) raise ArgumentError, "Invalid telegram format: #{@telegram}" if @telegram && !self.class.valid_telegram?(@telegram)
end
def self.valid_phone_number?(phone) @email = email
phone.match?(/\A\+?[0-9]{10,15}\z/) raise ArgumentError, "Invalid email format: #{@email}" if @email && !self.class.valid_email?(@email)
end end
def self.valid_name?(name) def self.valid_phone_number?(phone)
name.match?(/\A[А-Яа-яЁёA-Za-z\-]+\z/) phone.match?(/\A\+?[0-9]{10,15}\z/)
end end
def self.valid_telegram?(telegram) def self.valid_name?(name)
telegram.nil? || telegram.match?(/\A@[A-Za-z0-9_]{5,32}\z/) name.match?(/\A[А-Яа-яЁёA-Za-z\-]+\z/)
end end
def self.valid_email?(email) def self.valid_telegram?(telegram)
email.nil? || email.match?(/\A[^@\s]+@[^@\s]+\.[^@\s]+\z/) telegram.nil? || telegram.match?(/\A@[A-Za-z0-9_]{5,32}\z/)
end end
def self.valid_git?(git) def self.valid_email?(email)
git.nil? || git.match?(/\Ahttps:\/\/github\.com\/[A-Za-z0-9_\-]+\z/) email.nil? || email.match?(/\A[^@\s]+@[^@\s]+\.[^@\s]+\z/)
end end
def git_present? def self.valid_git?(git)
!@git.nil? && !@git.empty? git.nil? || git.match?(/\Ahttps:\/\/github\.com\/[A-Za-z0-9_\-]+\z/)
end end
def contact_present? def git_present?
!(@phone.nil? || @phone.empty?) || !(@telegram.nil? || @telegram.empty?) || !(@email.nil? || @email.empty?) !@git.nil? && !@git.empty?
end end
def validate def contact_present?
raise ArgumentError, "Git link is required" unless git_present? !(@phone.nil? || @phone.empty?) || !(@telegram.nil? || @telegram.empty?) || !(@email.nil? || @email.empty?)
raise ArgumentError, "At least one contact (phone, telegram, or email) is required" unless contact_present? end
end
def contact_info def contact_info
return "Phone: #{@phone}" if @phone return "Phone: #{@phone}" if @phone
return "Telegram: #{@telegram}" if @telegram return "Telegram: #{@telegram}" if @telegram
return "Email: #{@email}" if @email return "Email: #{@email}" if @email
'No contact available' 'No contact available'
end end
private private
attr_reader :phone, :telegram, :email attr_reader :phone, :telegram, :email
attr_writer :phone, :telegram, :email attr_writer :phone, :telegram, :email
end end
Loading…
Cancel
Save