feat: Add contact validation and secure modification for Student class

- Implemented `set_contacts` method to safely set phone, telegram, and email fields.
- Made contact fields (`phone`, `telegram`, `email`) private and accessible only through `set_contacts`.
- Added validation within `set_contacts` to ensure contact fields are correctly formatted.
- Modified the `initialize` method to utilize `set_contacts` for setting contact values during object creation.
- Added checks in the `validate` method to ensure Git presence and at least one contact method.
- Updated test cases in `main.rb` to demonstrate correct and incorrect uses of contact modifications.
- Ensured that contact fields cannot be modified directly, maintaining data integrity.
pull/4/head lab2.1
Artem-Darius Weber 4 months ago
parent c1ca0ea845
commit 1407d3e6fd

@ -1,27 +1,33 @@
require_relative 'student'
begin
student1 = Student.new(
surname: 'Алексеевич',
name: 'Артем-Дариус',
patronymic: 'Вебер',
id: 1,
phone: '+79891242223',
telegram: '@alstroemeria22',
email: 'no-replay@djft.ru',
git: 'https://git.djft.ru'
)
student1 = Student.new(
surname: 'Алексеевич',
name: 'Артем-Дариус',
patronymic: 'Вебер',
id: 1,
git: 'https://git.djft.ru'
)
student2 = Student.new(
surname: 'nil',
name: 'Норакет',
patronymic: 'nil'
)
student1.set_contacts(
phone: '+79891242223',
telegram: '@alstroemeria22',
email: 'no-replay@djft.ru'
)
puts student1
puts '-' * 40
puts student2
student2 = Student.new(
surname: 'Норакет',
name: 'Норакет',
patronymic: 'Фамилия'
)
student2.set_contacts(
phone: '+70000000000'
)
puts student1
puts '-' * 40
puts student2
rescue ArgumentError => e
puts "Err.: #{e.message}"
puts "Ошибка: #{e.message}"
end

@ -1,74 +1,86 @@
class Student
attr_accessor :id, :surname, :name, :patronymic, :phone, :telegram, :email, :git
attr_accessor :id, :surname, :name, :patronymic, :git
def self.valid_phone_number?(phone)
phone.match?(/\A\+?[0-9]{10,15}\z/)
phone.match?(/\A\+?[0-9]{10,15}\z/)
end
def self.valid_name?(name)
name.match?(/\A[А-Яа-яЁёA-Za-z\-]+\z/)
name.match?(/\A[А-Яа-яЁёA-Za-z\-]+\z/)
end
def self.valid_telegram?(telegram)
telegram.nil? || telegram.match?(/\A@[A-Za-z0-9_]{5,32}\z/)
telegram.nil? || telegram.match?(/\A@[A-Za-z0-9_]{5,32}\z/)
end
def self.valid_email?(email)
email.nil? || email.match?(/\A[^@\s]+@[^@\s]+\.[^@\s]+\z/)
email.nil? || email.match?(/\A[^@\s]+@[^@\s]+\.[^@\s]+\z/)
end
def self.valid_git?(git)
git.nil? || git.match?(/\Ahttps:\/\/github\.com\/[A-Za-z0-9_\-]+\z/)
git.nil? || git.match?(/\Ahttps:\/\/github\.com\/[A-Za-z0-9_\-]+\z/)
end
def initialize(args = {})
@surname = args.fetch(:surname)
raise ArgumentError, "Invalid surname format: #{@surname}" unless Student.valid_name?(@surname)
@name = args.fetch(:name)
raise ArgumentError, "Invalid name format: #{@name}" unless Student.valid_name?(@name)
@patronymic = args.fetch(:patronymic)
raise ArgumentError, "Invalid patronymic format: #{@patronymic}" unless Student.valid_name?(@patronymic)
@id = args[:id] || nil
@phone = args[:phone]
if @phone && !Student.valid_phone_number?(@phone)
raise ArgumentError, "Invalid phone number format: #{@phone}"
end
@telegram = args[:telegram]
raise ArgumentError, "Invalid telegram format: #{@telegram}" unless Student.valid_telegram?(@telegram)
@email = args[:email]
raise ArgumentError, "Invalid email format: #{@email}" unless Student.valid_email?(@email)
@git = args[:git]
raise ArgumentError, "Invalid git format: #{@git}" unless Student.valid_git?(@git)
validate
@surname = args.fetch(:surname)
raise ArgumentError, "Invalid surname format: #{@surname}" unless Student.valid_name?(@surname)
@name = args.fetch(:name)
raise ArgumentError, "Invalid name format: #{@name}" unless Student.valid_name?(@name)
@patronymic = args.fetch(:patronymic)
raise ArgumentError, "Invalid patronymic format: #{@patronymic}" unless Student.valid_name?(@patronymic)
@id = args[:id] || nil
set_contacts(
phone: args[:phone],
telegram: args[:telegram],
email: args[:email]
)
@git = args[:git]
raise ArgumentError, "Invalid git format: #{@git}" unless Student.valid_git?(@git)
validate
end
def set_contacts(phone: nil, telegram: nil, email: nil)
@phone = phone
raise ArgumentError, "Invalid phone number format: #{@phone}" if @phone && !Student.valid_phone_number?(@phone)
@telegram = telegram
raise ArgumentError, "Invalid telegram format: #{@telegram}" if @telegram && !Student.valid_telegram?(@telegram)
@email = email
raise ArgumentError, "Invalid email format: #{@email}" if @email && !Student.valid_email?(@email)
end
def git_present?
!@git.nil? && !@git.empty?
!@git.nil? && !@git.empty?
end
def contact_present?
!(@phone.nil? || @phone.empty?) || !(@telegram.nil? || @telegram.empty?) || !(@email.nil? || @email.empty?)
!(@phone.nil? || @phone.empty?) || !(@telegram.nil? || @telegram.empty?) || !(@email.nil? || @email.empty?)
end
def validate
raise ArgumentError, "Git link is required" unless git_present?
raise ArgumentError, "At least one contact (phone, telegram, or email) is required" unless contact_present?
raise ArgumentError, "Git link is required" unless git_present?
raise ArgumentError, "At least one contact (phone, telegram, or email) is required" unless contact_present?
end
def to_s
"Student: #{@surname} #{@name} #{@patronymic}\n" \
"ID: #{@id || 'N/A'}\n" \
"Phone: #{@phone || 'N/A'}\n" \
"Telegram: #{@telegram || 'N/A'}\n" \
"Email: #{@email || 'N/A'}\n" \
"Git: #{@git || 'N/A'}"
"Student: #{@surname} #{@name} #{@patronymic}\n" \
"ID: #{@id || 'N/A'}\n" \
"Phone: #{@phone || 'N/A'}\n" \
"Telegram: #{@telegram || 'N/A'}\n" \
"Email: #{@email || 'N/A'}\n" \
"Git: #{@git || 'N/A'}"
end
private
attr_reader :phone, :telegram, :email
attr_writer :phone, :telegram, :email
end
Loading…
Cancel
Save