Class: Student
Instance Attribute Summary collapse
Attributes inherited from StudentShort
#git, #id, #surname
Class Method Summary
collapse
Instance Method Summary
collapse
#exist_contact?, #exist_git?, from_str, #short_name_initials, #validate?
Constructor Details
#initialize(first_name: nil, middle_name: nil, surname: nil, id: nil, phone_number: nil, git: nil, telegram: nil, mail: nil) ⇒ Student
43
44
45
46
47
48
49
50
|
# File 'lib/source/student/student.rb', line 43
def initialize(first_name: nil, middle_name:nil, surname:nil, id:nil,phone_number:nil, git:nil,telegram:nil, mail:nil)
self.first_name = first_name
self.middle_name = middle_name
self.surname = surname
self.id = id
self.git = git
set_contacts(phone_number: phone_number,mail: mail, telegram:telegram)
end
|
Instance Attribute Details
#first_name ⇒ Object
Returns the value of attribute first_name.
7
8
9
|
# File 'lib/source/student/student.rb', line 7
def first_name
@first_name
end
|
#mail ⇒ Object
Returns the value of attribute mail.
7
8
9
|
# File 'lib/source/student/student.rb', line 7
def mail
@mail
end
|
#middle_name ⇒ Object
Returns the value of attribute middle_name.
7
8
9
|
# File 'lib/source/student/student.rb', line 7
def middle_name
@middle_name
end
|
#phone_number ⇒ Object
Returns the value of attribute phone_number.
7
8
9
|
# File 'lib/source/student/student.rb', line 7
def phone_number
@phone_number
end
|
#telegram ⇒ Object
Returns the value of attribute telegram.
7
8
9
|
# File 'lib/source/student/student.rb', line 7
def telegram
@telegram
end
|
Class Method Details
.from_hash(hash) ⇒ Object
38
39
40
41
|
# File 'lib/source/student/student.rb', line 38
def self.from_hash(hash)
hash=hash.dup
Student.new(**hash)
end
|
.is_git?(git) ⇒ Boolean
25
26
27
28
29
30
|
# File 'lib/source/student/student.rb', line 25
def self.is_git?(git)
raise ArgumentError, "arg '#{git}' is not string" unless git.class == String or git.nil?
return true if git=~/^github\.com\/[a-zA-Z0-9\-_]+$/ or git.nil?
false
end
|
.is_mail?(mail) ⇒ Boolean
20
21
22
23
|
# File 'lib/source/student/student.rb', line 20
def self.is_mail?(mail)
raise ArgumentError, "arg '#{mail}' is not string" unless mail.class == String or mail.nil?
return true if mail=~/^[^@\s]+@[^@\s]+\.\w+$/ or mail.nil?
end
|
.is_name?(name) ⇒ Boolean
15
16
17
18
|
# File 'lib/source/student/student.rb', line 15
def self.is_name?(name)
raise ArgumentError, "arg '#{name}' is not string" unless name.class == String
return true if name=~/^[А-Яа-я]+$/
end
|
.is_phone?(phone_number) ⇒ Boolean
8
9
10
11
12
13
|
# File 'lib/source/student/student.rb', line 8
def self.is_phone?(phone_number)
raise ArgumentError, "arg '#{phone_number}' is not string" unless phone_number.class == String or phone_number.nil?
return true if phone_number=~/^(\+7|8)\s?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{2}[\s\-]?\d{2}/ or phone_number.nil?
false
end
|
.is_telegram?(telegram) ⇒ Boolean
32
33
34
35
36
37
|
# File 'lib/source/student/student.rb', line 32
def self.is_telegram?(telegram)
raise ArgumentError, "arg '#{telegram}' is not string" unless telegram.class == String or telegram.nil?
return true if telegram=~/^@[^@]+$/ or telegram.nil?
false
end
|
.pars_str(str) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/source/student/student.rb', line 64
def self.pars_str(str)
args = JSON.parse(str)
raise ArgumentError,"The argument must have first_name, middle_name and surname" unless
(args.has_key?('first_name') and args.has_key?('middle_name') and args.has_key?('surname'))
new(first_name: args['first_name'], middle_name: args['middle_name'],
surname: args['surname'], id: args['id'],
git: args['git'],
phone_number: args['phone_number'], telegram: args['telegram'],
mail: args['mail'])
end
|
Instance Method Details
147
148
149
150
151
152
153
|
# File 'lib/source/student/student.rb', line 147
def contact
return @contact = "phone_number= #{phone_number}" unless phone_number.nil?
return @contact = "telegram= #{telegram}" unless telegram.nil?
return @contact = "mail= #{mail}" unless mail.nil?
nil
end
|
#get_info ⇒ Object
56
57
58
59
60
61
62
|
# File 'lib/source/student/student.rb', line 56
def get_info
info = {}
info[:short_name] = short_name
info[:contact] = contact
info[:git] = git
JSON.generate(info)
end
|
#git=(git_value) ⇒ Object
112
113
114
115
116
|
# File 'lib/source/student/student.rb', line 112
def git=(git_value)
raise ArgumentError, "Incorrect value: git=#{git}!" unless Student.is_git?(git_value)
@git=git_value
end
|
#id=(id_value) ⇒ Object
90
91
92
|
# File 'lib/source/student/student.rb', line 90
def id=(id_value)
@id=id_value
end
|
82
83
84
85
86
87
|
# File 'lib/source/student/student.rb', line 82
def set_contacts(phone_number:nil, mail:nil, telegram:nil)
self.phone_number = phone_number unless phone_number.nil?
self.mail = mail unless mail.nil?
self.telegram = telegram unless telegram.nil?
@contact = contact
end
|
#short_name ⇒ Object
52
53
54
|
# File 'lib/source/student/student.rb', line 52
def short_name
"#{surname} #{first_name[0]}. #{middle_name[0]}."
end
|
#surname=(surname_value) ⇒ Object
106
107
108
109
110
|
# File 'lib/source/student/student.rb', line 106
def surname=(surname_value)
raise ArgumentError, "Incorrect value: surname=#{surname}!" unless Student.is_name?(surname_value)
@surname=surname_value
end
|
#to_hash ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/source/student/student.rb', line 75
def to_hash
info_hash = {}
%i[first_name middle_name surname id phone_number telegram mail git].each do |field|
info_hash[field] = send(field) unless send(field).nil?
end
info_hash
end
|
#to_s ⇒ Object
137
138
139
140
141
142
143
144
145
|
# File 'lib/source/student/student.rb', line 137
def to_s
result = "#{first_name} #{middle_name} #{surname}"
result += " id=#{id}" unless id.nil?
result += " phone=#{phone_number}" unless phone_number.nil?
result += " git=#{git}" unless git.nil?
result += " telegram=#{telegram}" unless telegram.nil?
result += " mail=#{mail}" unless mail.nil?
result
end
|