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

73 lines
2.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

class Person
attr_accessor :id, :git, :name
def initialize(args = {})
@id = args[:id]
@git = args[:git]
@name = args[:name]
raise ArgumentError, "ID должен быть числом" if @id && !@id.is_a?(Integer)
raise ArgumentError, "Требуется ссылка на GitHub" unless git_present?
raise ArgumentError, "Некорректная ссылка на GitHub: #{@git}" unless self.class.valid_git?(@git)
raise ArgumentError, "Требуется имя" if @name.nil? || @name.strip.empty?
raise ArgumentError, "Некорректный формат имени: #{@name}" unless self.class.valid_name?(@name)
end
def set_contacts(phone: nil, telegram: nil, email: nil)
@phone = phone
raise ArgumentError, "Некорректный формат номера телефона: #{@phone}" if @phone && !self.class.valid_phone_number?(@phone)
@telegram = telegram
raise ArgumentError, "Некорректный формат Telegram: #{@telegram}" if @telegram && !self.class.valid_telegram?(@telegram)
@email = email
raise ArgumentError, "Некорректный формат email: #{@email}" if @email && !self.class.valid_email?(@email)
raise ArgumentError, "Требуется хотя бы один контакт (телефон, телеграм или email)" unless contact_present?
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
'Контактная информация отсутствует'
end
private
attr_reader :phone, :telegram, :email
attr_writer :phone, :telegram, :email
end