diff --git a/lab2/student.rb b/lab2/student.rb index 14f5464..99d3793 100644 --- a/lab2/student.rb +++ b/lab2/student.rb @@ -5,10 +5,31 @@ class Student phone.match?(/\A\+?[0-9]{10,15}\z/) end + def self.valid_name?(name) + 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/) + end + + def self.valid_email?(email) + 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/) + 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 @@ -16,10 +37,15 @@ class Student 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 + + @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) end def to_s @@ -30,4 +56,4 @@ class Student "Email: #{@email || 'N/A'}\n" \ "Git: #{@git || 'N/A'}" end - end \ No newline at end of file +end \ No newline at end of file