|
|
|
@ -1,20 +1,12 @@
|
|
|
|
|
class Contact
|
|
|
|
|
# add setters with validation
|
|
|
|
|
attr_reader :phone, :telegram, :email
|
|
|
|
|
|
|
|
|
|
def initialize(args = {})
|
|
|
|
|
phone = args[:phone]
|
|
|
|
|
telegram = args[:telegram]
|
|
|
|
|
email = args[:email]
|
|
|
|
|
self.phone = args[:phone]
|
|
|
|
|
self.telegram = args[:telegram]
|
|
|
|
|
self.email = args[: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
|
|
|
|
|
@telegram = telegram
|
|
|
|
|
@email = email
|
|
|
|
|
raise ArgumentError, "Необходимо указать хотя бы один контакт (телефон, Telegram или email)" unless present?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def present?
|
|
|
|
@ -29,6 +21,27 @@ class Contact
|
|
|
|
|
'No contact available'
|
|
|
|
|
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 self.valid_phone_number?(phone)
|
|
|
|
|
/\A\+?[0-9]{9,15}\z/.match?(phone)
|
|
|
|
|
end
|
|
|
|
|