Class: Student
Instance Attribute Summary collapse
#Father_name, #Git, #ID, #Name, #Surname
Class Method Summary
collapse
Instance Method Summary
collapse
acc_valid?, id_valid?, name_valid?, #short_name
Constructor Details
#initialize(name:, surname:, father_name:, id: nil, git: nil, mail: nil, tg: nil, phone: nil) ⇒ Student
Returns a new instance of Student.
6
7
8
9
|
# File 'lib/files/student.rb', line 6
def initialize(name:, surname:, father_name:, id:nil, git:nil, mail:nil, tg:nil, phone:nil)
super(name, surname, father_name, id, git)
set_contacts(mail:mail, tg:tg, phone:phone)
end
|
Instance Attribute Details
#Mail ⇒ Object
Returns the value of attribute Mail.
4
5
6
|
# File 'lib/files/student.rb', line 4
def Mail
@Mail
end
|
#Phone ⇒ Object
Returns the value of attribute Phone.
4
5
6
|
# File 'lib/files/student.rb', line 4
def Phone
@Phone
end
|
#Tg ⇒ Object
Returns the value of attribute Tg.
4
5
6
|
# File 'lib/files/student.rb', line 4
def Tg
@Tg
end
|
Class Method Details
.mail_valid?(mail) ⇒ Boolean
25
26
27
|
# File 'lib/files/student.rb', line 25
def self.mail_valid?(mail)
mail.match(/^[\w\d]+@[\w]+\.[\w]+$/)
end
|
.parse_str(str) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/files/student.rb', line 49
def self.parse_str(str)
str_student=str.split(', ').map{|x| x.split(':')}.to_h
raise ArgumentError,"Invalid name " unless str_student.key?("name") && Student.name_valid?(str_student["name"])
raise ArgumentError,"Invalid surname" unless str_student.key?("surname") && Student.name_valid?(str_student["surname"])
raise ArgumentError,"Invalid father's name" unless str_student.key?("father_name") && Student.name_valid?(str_student["father_name"])
if str_student.key?("tg")
raise ArgumentError, "Invalid telegram" unless Student.acc_valid?(str_student["tg"])
end
if str_student.key?("git")
raise ArgumentError, "Invalid git" unless Student.acc_valid?(str_student["git"])
end
if str_student.key?("mail")
raise ArgumentError, "Invalid mail addres" unless Student.mail_valid?(str_student["mail"])
end
if str_student.key?("id")
raise ArgumentError, "Invalid id" unless Student.id_valid?(str_student["id"])
end
if str_student.key?("phone")
raise ArgumentError, "Invalid phone number" unless Student.phone_valid?(str_student["phone"])
end
Student.new(name:str_student["name"],surname:str_student["surname"],father_name:str_student["father_name"],id:str_student["id"],git:str_student["git"],mail:str_student["mail"],phone: str_student["phone"], tg:str_student["tg"])
end
|
.phone_valid?(phone) ⇒ Boolean
11
12
13
|
# File 'lib/files/student.rb', line 11
def self.phone_valid?(phone)
phone.match(/^\+?[7,8]{1}\-\d{3}\-\d{3}\-\d{2}\-\d{2}$/)
end
|
.read_from_txt(path_name) ⇒ Object
90
91
92
93
|
# File 'lib/files/student.rb', line 90
def self.read_from_txt(path_name)
raise FileNotFoundError unless File.exist?(path_name)
File.read(path_name).split("\n").map{|line| Student.parse_str(line)}
end
|
.write_to_txt(path_name, student) ⇒ Object
95
96
97
98
|
# File 'lib/files/student.rb', line 95
def self.write_to_txt(path_name,student)
raise FileNotFoundError unless File.exist?(path_name)
File.open(path_name,'w') {|file| file.write(student.map{|stud|stud.getInfo}.join("\n"))}
end
|
Instance Method Details
82
83
84
85
86
87
88
|
# File 'lib/files/student.rb', line 82
def contact
s=""
s+= self.phone_to_s
s+= self.tg_to_s
s+= self.mail_to_s
return s
end
|
#get_short ⇒ Object
77
78
79
80
|
# File 'lib/files/student.rb', line 77
def get_short
short=Student_short.from_student(self)
return short.short_name
end
|
#getInfo ⇒ Object
72
73
74
75
|
# File 'lib/files/student.rb', line 72
def getInfo
short=Student_short.from_student(self)
"#{short.short_name}, #{contact}, #{git_to_s}"
end
|
#git? ⇒ Boolean
104
105
106
|
# File 'lib/files/student.rb', line 104
def git?
!@Git.nil?
end
|
35
36
37
38
39
|
# File 'lib/files/student.rb', line 35
def set_contacts(mail:, tg:, phone:)
self.Phone = phone unless phone.nil?
self.Mail = mail unless mail.nil?
self.Tg = tg unless tg.nil?
end
|
#to_s ⇒ Object
41
42
43
44
45
46
47
|
# File 'lib/files/student.rb', line 41
def to_s
info=@Name.to_s+" "+@Surname.to_s+" "+@Father_name.to_s+" "
info+=tg_to_s
info+=phone_to_s
info+=git_to_s
info
end
|
#validate? ⇒ Boolean
100
101
102
|
# File 'lib/files/student.rb', line 100
def validate?
git? && self.contact!=""
end
|