You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kubsu-sm5-ruby/lab2/person.rb

85 lines
2.1 KiB

class Person
attr_reader :id, :git, :phone, :telegram, :email
def initialize(id:, git:, phone: nil, telegram: nil, email: nil)
self.class.validate_id(id)
self.phone = phone
self.telegram = telegram
self.email = email
raise ArgumentError, "Необходимо указать хотя бы один контакт (телефон, Telegram или email)" unless contact_present?
@id = id
@git = git
end
def git_present?
!@git.nil? && !@git.empty?
end
def contact_present?
@phone || @telegram || @email
end
def contact_info
return "Phone: #{@phone}" if @phone
return "Telegram: #{@telegram}" if @telegram
return "Email: #{@email}" if @email
'No contact available'
end
def self.valid_phone_number?(phone)
/\A\+?[0-9]{9,15}\z/.match?(phone)
end
def self.valid_telegram?(telegram)
/\A@[A-Za-z0-9_]{5,32}\z/.match?(telegram)
end
def self.valid_email?(email)
/\A[^@\s]+@[^@\s]+\.[^@\s]+\z/.match?(email)
end
def phone=(phone)
if phone && !self.class.valid_phone_number?(phone)
raise ArgumentError, "Недопустимый номер телефона: #{phone}"
end
@phone = phone
end
def telegram=(telegram)
if telegram && !self.class.valid_telegram?(telegram)
raise ArgumentError, "Некорректный Telegram: #{telegram}"
end
@telegram = telegram
end
def email=(email)
if email && !self.class.valid_email?(email)
raise ArgumentError, "Некорректный email: #{email}"
end
@email = email
end
def git=(git)
self.class.validate_git(git)
@git = git
end
def self.validate_id(id)
raise ArgumentError, 'ID is required and must be a non-empty string' unless valid_id?(id)
end
def self.validate_git(git)
raise ArgumentError, 'Git link is required' if git.nil? || git.strip.empty?
raise ArgumentError, 'Invalid Git link format' unless valid_git?(git)
end
def self.valid_git?(git)
/\Ahttps:\/\/github\.com\/[A-Za-z0-9_\-]+\z/.match?(git)
end
def self.valid_id?(id)
id.is_a?(String) && !id.strip.empty?
end
end