diff --git a/lab2/contact.rb b/lab2/contact.rb index e20c619..caaf745 100644 --- a/lab2/contact.rb +++ b/lab2/contact.rb @@ -16,6 +16,10 @@ class Contact @email = email end + def present? + @phone || @telegram || @email + end + def info return "Phone: #{@phone}" if @phone return "Telegram: #{@telegram}" if @telegram diff --git a/lab2/person.rb b/lab2/person.rb index 3a6fdad..0e17ac3 100644 --- a/lab2/person.rb +++ b/lab2/person.rb @@ -1,15 +1,16 @@ require_relative 'contact' class Person - attr_accessor :id, :git - attr_reader :contact + attr_reader :id, :git, :contact def initialize(id:, git:, contact: Contact.new) + validate_id(id) + validate_git(git) + validate_contact(contact) + @id = id @git = git @contact = contact - - validate_person end def git_present? @@ -34,9 +35,17 @@ class Person private - def validate_person - 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? - raise ArgumentError, 'Invalid Git link format' unless self.class.valid_git?(@git) + def validate_id(id) + raise ArgumentError, 'ID is required and must be a non-empty string' unless self.class.valid_id?(id) + end + + 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 diff --git a/lab2/student_short.rb b/lab2/student_short.rb index 3596d24..6801b1f 100644 --- a/lab2/student_short.rb +++ b/lab2/student_short.rb @@ -13,25 +13,25 @@ class StudentShort < Person id: student.id, git: student.git, surname_initials: student.surname_and_initials, - contact: student.contact_info + contact: student.contact ) end def self.from_string(id, info_string) parts = info_string.split(',').map(&:strip) raise ArgumentError, 'Invalid info string format' if parts.size < 3 - + surname_initials = parts[0] git = parts[1].split(': ').last.strip contact_string = parts[2].split(': ', 2).last.strip - + contact = Contact.new_from_info(contact_string) - + new( id: id, git: git, surname_initials: surname_initials, - contact: contact.info + contact: contact ) end