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

52 lines
1.1 KiB

require_relative 'contact'
class Person
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
end
def git_present?
!@git.nil? && !@git.empty?
end
def contact_present?
@contact.present?
end
def contact_info
@contact.info
end
def self.valid_git?(git)
/\Ahttps:\/\/github\.com\/[A-Za-z0-9_\-]+\z/.match?(git)
end
def self.valid_id?(id)
id.is_a?(String) && !id.strip.empty?
end
private
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