refactor: update validation methods in Contact and Student classes to use class methods

lab2
Artem-Darius Weber 1 week ago
parent 3fade34834
commit f8a012e9dd

@ -1,5 +1,5 @@
class Contact
# add setters with validation
# add setters with validation
attr_reader :phone, :telegram, :email
def initialize(args = {})
@ -7,9 +7,9 @@ class Contact
telegram = args[:telegram]
email = args[:email]
raise ArgumentError, "Недопустимый номер телефона: #{phone}" if phone && !valid_phone_number?(phone)
raise ArgumentError, "Некорректный Telegram: #{telegram}" if telegram && !valid_telegram?(telegram)
raise ArgumentError, "Некорректный email: #{email}" if email && !valid_email?(email)
raise ArgumentError, "Недопустимый номер телефона: #{phone}" if phone && !self.class.valid_phone_number?(phone)
raise ArgumentError, "Некорректный Telegram: #{telegram}" if telegram && !self.class.valid_telegram?(telegram)
raise ArgumentError, "Некорректный email: #{email}" if email && !self.class.valid_email?(email)
raise ArgumentError, "Необходимо указать хотя бы один контакт (телефон, Telegram или email)" unless phone || telegram || email
@phone = phone
@ -55,4 +55,4 @@ class Contact
raise ArgumentError, "Некорректный формат строки контакта: #{info_string}"
end
end
end
end

@ -4,6 +4,12 @@ require_relative 'contact'
class Student < Person
attr_accessor :surname, :name, :patronymic, :birth_date
private
NAME_REGEX = /\A[А-Яа-яЁёA-Za-z\-]+\z/
public
def initialize(id:, git:, contact:, surname: nil, name: nil, patronymic: nil, birth_date: nil)
super(id: id, git: git, contact: contact)
@surname = surname
@ -83,7 +89,6 @@ class Student < Person
end
def self.valid_name?(name)
NAME_REGEX = /\A[А-Яа-яЁёA-Za-z\-]+\z/
NAME_REGEX.match?(name)
end

Loading…
Cancel
Save