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