commit
						c222d7bdd0
					
				@ -0,0 +1,67 @@
 | 
				
			||||
class Contact
 | 
				
			||||
  attr_reader :phone, :telegram, :email
 | 
				
			||||
 | 
				
			||||
  def initialize(phone: nil, telegram: nil, email: nil)
 | 
				
			||||
    @phone = phone
 | 
				
			||||
    @telegram = telegram
 | 
				
			||||
    @email = email
 | 
				
			||||
    validate_contacts
 | 
				
			||||
  end
 | 
				
			||||
 | 
				
			||||
  def valid_phone_number?
 | 
				
			||||
    return true if @phone.nil?
 | 
				
			||||
 | 
				
			||||
    /\A\+?[0-9]{10,15}\z/.match?(@phone)
 | 
				
			||||
  end
 | 
				
			||||
 | 
				
			||||
  def valid_telegram?
 | 
				
			||||
    return true if @telegram.nil?
 | 
				
			||||
 | 
				
			||||
    /\A@[A-Za-z0-9_]{5,32}\z/.match?(@telegram)
 | 
				
			||||
  end
 | 
				
			||||
 | 
				
			||||
  def valid_email?
 | 
				
			||||
    return true if @email.nil?
 | 
				
			||||
 | 
				
			||||
    /\A[^@\s]+@[^@\s]+\.[^@\s]+\z/.match?(@email)
 | 
				
			||||
  end
 | 
				
			||||
 | 
				
			||||
  def present?
 | 
				
			||||
    @phone || @telegram || @email
 | 
				
			||||
  end
 | 
				
			||||
 | 
				
			||||
  def info
 | 
				
			||||
    return "Phone: #{@phone}" if @phone
 | 
				
			||||
    return "Telegram: #{@telegram}" if @telegram
 | 
				
			||||
    return "Email: #{@email}" if @email
 | 
				
			||||
 | 
				
			||||
    'No contact available'
 | 
				
			||||
  end
 | 
				
			||||
 | 
				
			||||
  private
 | 
				
			||||
 | 
				
			||||
  def validate_contacts
 | 
				
			||||
    if !present?
 | 
				
			||||
      raise ArgumentError, 'At least one contact (phone, telegram, or email) is required'
 | 
				
			||||
    end
 | 
				
			||||
 | 
				
			||||
    raise ArgumentError, "Invalid phone number format: #{@phone}" if @phone && !valid_phone_number?
 | 
				
			||||
    raise ArgumentError, "Invalid telegram format: #{@telegram}" if @telegram && !valid_telegram?
 | 
				
			||||
    raise ArgumentError, "Invalid email format: #{@email}" if @email && !valid_email?
 | 
				
			||||
  end
 | 
				
			||||
 | 
				
			||||
  def self.new_from_info(info_string)
 | 
				
			||||
    info_string = info_string.sub(/^Contact: /, '').strip
 | 
				
			||||
  
 | 
				
			||||
    case info_string
 | 
				
			||||
    when /\APhone: (.+)\z/i
 | 
				
			||||
      new(phone: Regexp.last_match(1).strip)
 | 
				
			||||
    when /\ATelegram: (.+)\z/i
 | 
				
			||||
      new(telegram: Regexp.last_match(1).strip)
 | 
				
			||||
    when /\AEmail: (.+)\z/i
 | 
				
			||||
      new(email: Regexp.last_match(1).strip)
 | 
				
			||||
    else
 | 
				
			||||
      raise ArgumentError, "Invalid contact info format: #{info_string}"
 | 
				
			||||
    end
 | 
				
			||||
  end
 | 
				
			||||
end
 | 
				
			||||
@ -0,0 +1,39 @@
 | 
				
			||||
require_relative 'student'
 | 
				
			||||
 | 
				
			||||
class StudentRepository
 | 
				
			||||
  def self.read_from_txt(file_path)
 | 
				
			||||
    raise IOError, "File does not exist: #{file_path}" unless File.exist?(file_path)
 | 
				
			||||
 | 
				
			||||
    students = []
 | 
				
			||||
 | 
				
			||||
    File.foreach(file_path) do |line|
 | 
				
			||||
      line.strip!
 | 
				
			||||
      next if line.empty?
 | 
				
			||||
 | 
				
			||||
      begin
 | 
				
			||||
        student = Student.from_string(line)
 | 
				
			||||
        students << student
 | 
				
			||||
      rescue ArgumentError => e
 | 
				
			||||
        puts "Error processing line: '#{line}'. Reason: #{e.message}"
 | 
				
			||||
      end
 | 
				
			||||
    end
 | 
				
			||||
 | 
				
			||||
    students
 | 
				
			||||
  end
 | 
				
			||||
 | 
				
			||||
  def self.write_to_txt(file_path, students)
 | 
				
			||||
    unless students.is_a?(Array) && students.all? { |s| s.is_a?(Student) }
 | 
				
			||||
      raise ArgumentError, 'Expected an array of Student objects'
 | 
				
			||||
    end
 | 
				
			||||
 | 
				
			||||
    File.open(file_path, 'w') do |file|
 | 
				
			||||
      students.each do |student|
 | 
				
			||||
        file.puts student.to_s
 | 
				
			||||
      end
 | 
				
			||||
    end
 | 
				
			||||
 | 
				
			||||
    puts "Data successfully written to #{file_path}"
 | 
				
			||||
  rescue IOError => e
 | 
				
			||||
    puts "An error occurred while writing to the file: #{e.message}"
 | 
				
			||||
  end
 | 
				
			||||
end
 | 
				
			||||
@ -0,0 +1,3 @@
 | 
				
			||||
Иванов Иван Иванович | ID: 1 | Phone: +12345678901 | Telegram: @ivanov_user | Email: ivanov@example.com | Git: https://github.com/ivanov
 | 
				
			||||
Петров Петр Петрович | ID: 2 | Phone: +98765432101 | Telegram: @petrov_user | Email: petrov@example.com | Git: https://github.com/petrov
 | 
				
			||||
Сидоров Сидор Сидорович | ID: 3 | Phone: +56789012345 | Telegram: @sidorov_user | Email: sidorov@example.com | Git: https://github.com/sidorov
 | 
				
			||||
					Loading…
					
					
				
		Reference in new issue