refactor: simplify Student initialization and move name validation to class method

lab2
Artem-Darius Weber 1 week ago
parent d5f67c2de0
commit a9717abae8

@ -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

Loading…
Cancel
Save