feat: enhance validation in Person class and add present? method in Contact class

lab2
Artem-Darius Weber 4 weeks ago
parent 3490c8f0f7
commit 5f3c510253

@ -16,6 +16,10 @@ class Contact
@email = email @email = email
end end
def present?
@phone || @telegram || @email
end
def info def info
return "Phone: #{@phone}" if @phone return "Phone: #{@phone}" if @phone
return "Telegram: #{@telegram}" if @telegram return "Telegram: #{@telegram}" if @telegram

@ -1,15 +1,16 @@
require_relative 'contact' require_relative 'contact'
class Person class Person
attr_accessor :id, :git attr_reader :id, :git, :contact
attr_reader :contact
def initialize(id:, git:, contact: Contact.new) def initialize(id:, git:, contact: Contact.new)
validate_id(id)
validate_git(git)
validate_contact(contact)
@id = id @id = id
@git = git @git = git
@contact = contact @contact = contact
validate_person
end end
def git_present? def git_present?
@ -34,9 +35,17 @@ class Person
private private
def validate_person def validate_id(id)
raise ArgumentError, 'ID is required and must be a non-empty string' unless self.class.valid_id?(@id) raise ArgumentError, 'ID is required and must be a non-empty string' unless self.class.valid_id?(id)
raise ArgumentError, 'Git link is required' unless git_present? end
raise ArgumentError, 'Invalid Git link format' unless self.class.valid_git?(@git)
def validate_git(git)
raise ArgumentError, 'Git link is required' if git.nil? || git.strip.empty?
raise ArgumentError, 'Invalid Git link format' unless self.class.valid_git?(git)
end
def validate_contact(contact)
raise ArgumentError, 'Contact must be a valid Contact object' unless contact.is_a?(Contact)
raise ArgumentError, 'Contact must have at least one valid field' unless contact.present?
end end
end end

@ -13,7 +13,7 @@ class StudentShort < Person
id: student.id, id: student.id,
git: student.git, git: student.git,
surname_initials: student.surname_and_initials, surname_initials: student.surname_and_initials,
contact: student.contact_info contact: student.contact
) )
end end
@ -31,7 +31,7 @@ class StudentShort < Person
id: id, id: id,
git: git, git: git,
surname_initials: surname_initials, surname_initials: surname_initials,
contact: contact.info contact: contact
) )
end end

Loading…
Cancel
Save