You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kubsu-sm5-ruby/lab2/student.rb

59 lines
1.9 KiB

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 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
@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)
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'}"
end
end