From a9717abae8cd9417e58a0dbe25f84b03c5fbf15b Mon Sep 17 00:00:00 2001 From: Artem Darius Weber Date: Thu, 2 Jan 2025 15:20:55 +0300 Subject: [PATCH] refactor: simplify Student initialization and move name validation to class method --- lab2/student.rb | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/lab2/student.rb b/lab2/student.rb index 2ca1cb8..742347e 100644 --- a/lab2/student.rb +++ b/lab2/student.rb @@ -4,16 +4,12 @@ require_relative 'contact' class Student < Person attr_accessor :surname, :name, :patronymic, :birth_date - NAME_REGEX = /\A[А-Яа-яЁёA-Za-z\-]+\z/ - - def initialize(id:, git:, contact:, surname:, name:, patronymic:, birth_date:) + def initialize(id:, git:, contact:, surname: nil, name: nil, patronymic: nil, birth_date: nil) super(id: id, git: git, contact: contact) - self.surname = surname - self.name = name - self.patronymic = patronymic - self.birth_date = birth_date - - validate_student + @surname = surname + @name = name + @patronymic = patronymic + @birth_date = birth_date end def self.from_string(student_string) @@ -86,23 +82,13 @@ class Student < Person @birth_date = birth_date end - private - - def validate_student - raise ArgumentError, 'Surname is required' if @surname.nil? || @surname.strip.empty? - raise ArgumentError, 'Name is required' if @name.nil? || @name.strip.empty? - raise ArgumentError, 'Patronymic is required' if @patronymic.nil? || @patronymic.strip.empty? - raise ArgumentError, 'Birth date is required' if @birth_date.nil? - - raise ArgumentError, "Invalid surname format: #{@surname}" unless valid_name?(@surname) - raise ArgumentError, "Invalid name format: #{@name}" unless valid_name?(@name) - raise ArgumentError, "Invalid patronymic format: #{@patronymic}" unless valid_name?(@patronymic) - end - - def valid_name?(name) + def self.valid_name?(name) + NAME_REGEX = /\A[А-Яа-яЁёA-Za-z\-]+\z/ NAME_REGEX.match?(name) end + private + def name_initial(name) name[0].upcase end