feat: add phone number validation to Student class and enforce format checks

main
Artem-Darius Weber 4 months ago
parent 3161870add
commit db00f79e72

@ -1,5 +1,6 @@
require_relative 'student'
begin
student1 = Student.new(
surname: 'Алексеевич',
name: 'Артем-Дариус',
@ -20,3 +21,7 @@ student2 = Student.new(
puts student1
puts '-' * 40
puts student2
rescue ArgumentError => e
puts "Err.: #{e.message}"
end

@ -1,14 +1,22 @@
class Student
attr_accessor :id, :surname, :name, :patronymic, :phone, :telegram, :email, :git
def self.valid_phone_number?(phone)
phone.match?(/\A\+?[0-9]{10,15}\z/)
end
def initialize(args = {})
@surname = args.fetch(:surname)
@name = args.fetch(:name)
@patronymic = args.fetch(:patronymic)
@id = args[:id] || nil
@phone = args[:phone] || nil
@phone = args[:phone]
if @phone && !Student.valid_phone_number?(@phone)
raise ArgumentError, "Invalid phone number format: #{@phone}"
end
@telegram = args[:telegram] || nil
@email = args[:email] || nil
@git = args[:git] || nil

Loading…
Cancel
Save