students_lab4_s0195941 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,126 +1,135 @@
1
- require_relative 'student_base'
2
- require_relative 'validated_attr_writer'
3
-
4
- class Student < StudentBase
5
- include Comparable
6
- singleton_class.prepend(ValidatedAttrWriter)
7
-
8
- attr_accessor :last_name, :first_name, :patronymic
9
- attr_reader :phone, :telegram, :email
10
- attr_writer :git, :phone, :telegram, :email
11
- private :phone=, :telegram=, :email=
12
-
13
- def <=>(other)
14
- return nil unless other.is_a?(Student)
15
-
16
- [last_name, first_name, patronymic.to_s] <=>
17
- [other.last_name, other.first_name, other.patronymic.to_s]
18
- end
19
-
20
- def initialize(last_name:, first_name:, id: nil, patronymic: nil,
21
- phone: nil, telegram: nil, email: nil, git: nil)
22
- raise ArgumentError, "Невалидный ID: #{id}" unless valid_id?(id)
23
-
24
- super(id: id)
25
- self.contact = { phone: phone, telegram: telegram, email: email }
26
-
27
- self.last_name = last_name
28
- self.first_name = first_name
29
- self.patronymic = patronymic
30
- self.git = git
31
- end
32
-
33
- def self.from_hash(attributes)
34
- new(
35
- id: attributes['id'],
36
- last_name: attributes['last_name'],
37
- first_name: attributes['first_name'],
38
- patronymic: attributes['patronymic'],
39
- phone: attributes['phone'],
40
- telegram: attributes['telegram'],
41
- email: attributes['email'],
42
- git: attributes['git']
43
- )
44
- end
45
-
46
- def self.valid_name?(name)
47
- valid_format?(name, /\A[А-ЯЁA-Z][а-яёa-z]+\z/, required: true)
48
- end
49
-
50
- def self.valid_phone?(phone)
51
- valid_format?(
52
- phone,
53
- /\A(?:\+7\d{10}|8\d{10}|8\(\d{3}\)\d{3}-\d{2}-\d{2})\z/
54
- )
55
- end
56
-
57
- def self.valid_telegram?(telegram)
58
- valid_format?(telegram, /\A@[a-zA-Z0-9_]{5,32}\z/)
59
- end
60
-
61
- def self.valid_email?(email)
62
- valid_format?(email, /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i)
63
- end
64
-
65
- def self.valid_git?(git)
66
- valid_format?(git, /\Ahttps:\/\/(www\.)?(github|gitlab)\.com\/[\w\-]+\z/)
67
- end
68
-
69
- def self.valid_format?(value, pattern, required: false)
70
- return !required if value.nil?
71
-
72
- value.is_a?(String) && value.match?(pattern)
73
- end
74
-
75
- def self.valid_last_name?(value)
76
- valid_name?(value)
77
- end
78
-
79
- def self.valid_first_name?(value)
80
- valid_name?(value)
81
- end
82
-
83
- def self.valid_patronymic?(value)
84
- value.nil? || valid_name?(value)
85
- end
86
-
87
- private_class_method :valid_format?, :valid_last_name?, :valid_first_name?, :valid_patronymic?
88
-
89
- def contact
90
- return "telegram - #{@telegram}" if @telegram
91
- return "email - #{@email}" if @email
92
- return "phone - #{@phone}" if @phone
93
-
94
- nil
95
- end
96
-
97
- def contact=(contacts)
98
- self.phone = contacts[:phone]
99
- self.telegram = contacts[:telegram]
100
- self.email = contacts[:email]
101
- end
102
-
103
- def last_name_initials
104
- initials = "#{last_name} #{first_name[0]}."
105
- initials += " #{patronymic[0]}." if patronymic
106
- initials
107
- end
108
-
109
- def to_s
110
- fio = "#{last_name} #{first_name}"
111
- fio += " #{patronymic}" if patronymic
112
- parts = [fio]
113
- parts << "ID: #{id}" if id
114
- parts << "git: #{git}" if git
115
- parts << "phone: #{@phone}" if @phone
116
- parts << "telegram: #{@telegram}" if @telegram
117
- parts << "email: #{@email}" if @email
118
- parts.join(", ")
119
- end
120
-
121
- private
122
-
123
- def valid_id?(value)
124
- value.nil? || value.to_s.match?(/\A\d+\z/)
125
- end
126
- end
1
+ require_relative 'student_base'
2
+ require_relative 'validated_attr_writer'
3
+
4
+ # Полная модель студента с проверкой имени и форматов контактов.
5
+ class Student < StudentBase
6
+ include Comparable
7
+ singleton_class.prepend(ValidatedAttrWriter)
8
+
9
+ attr_accessor :last_name, :first_name, :patronymic
10
+ attr_reader :phone, :telegram, :email
11
+ attr_writer :git, :phone, :telegram, :email
12
+ private :phone=, :telegram=, :email=
13
+
14
+ # Сравнивает ФИО лексикографически, считая отсутствующее отчество пустой строкой.
15
+ def <=>(other)
16
+ return nil unless other.is_a?(Student)
17
+
18
+ [last_name, first_name, patronymic.to_s] <=>
19
+ [other.last_name, other.first_name, other.patronymic.to_s]
20
+ end
21
+
22
+ def initialize(last_name:, first_name:, id: nil, patronymic: nil,
23
+ phone: nil, telegram: nil, email: nil, git: nil)
24
+ raise ArgumentError, "Невалидный ID: #{id}" unless valid_id?(id)
25
+
26
+ super(id: id)
27
+ self.contact = { phone: phone, telegram: telegram, email: email }
28
+
29
+ self.last_name = last_name
30
+ self.first_name = first_name
31
+ self.patronymic = patronymic
32
+ self.git = git
33
+ end
34
+
35
+ # Создаёт студента из строки, возвращённой PostgreSQL.
36
+ # @param attributes [Hash{String => Object}] значения столбцов со строковыми ключами
37
+ # @return [Student] студент с проверенными полями
38
+ def self.from_hash(attributes)
39
+ new(
40
+ id: attributes['id'],
41
+ last_name: attributes['last_name'],
42
+ first_name: attributes['first_name'],
43
+ patronymic: attributes['patronymic'],
44
+ phone: attributes['phone'],
45
+ telegram: attributes['telegram'],
46
+ email: attributes['email'],
47
+ git: attributes['git']
48
+ )
49
+ end
50
+
51
+ def self.valid_name?(name)
52
+ valid_format?(name, /\A[А-ЯЁA-Z][а-яёa-z]+\z/, required: true)
53
+ end
54
+
55
+ def self.valid_phone?(phone)
56
+ valid_format?(
57
+ phone,
58
+ /\A(?:\+7\d{10}|8\d{10}|8\(\d{3}\)\d{3}-\d{2}-\d{2})\z/
59
+ )
60
+ end
61
+
62
+ def self.valid_telegram?(telegram)
63
+ valid_format?(telegram, /\A@[a-zA-Z0-9_]{5,32}\z/)
64
+ end
65
+
66
+ def self.valid_email?(email)
67
+ valid_format?(email, /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i)
68
+ end
69
+
70
+ def self.valid_git?(git)
71
+ valid_format?(git, /\Ahttps:\/\/(www\.)?(github|gitlab)\.com\/[\w\-]+\z/)
72
+ end
73
+
74
+ def self.valid_format?(value, pattern, required: false)
75
+ return !required if value.nil?
76
+
77
+ value.is_a?(String) && value.match?(pattern)
78
+ end
79
+
80
+ def self.valid_last_name?(value)
81
+ valid_name?(value)
82
+ end
83
+
84
+ def self.valid_first_name?(value)
85
+ valid_name?(value)
86
+ end
87
+
88
+ def self.valid_patronymic?(value)
89
+ value.nil? || valid_name?(value)
90
+ end
91
+
92
+ private_class_method :valid_format?, :valid_last_name?, :valid_first_name?, :valid_patronymic?
93
+
94
+ # @return [String, nil] контакт для списка, с приоритетом Telegram, email, телефон
95
+ def contact
96
+ return "telegram - #{telegram}" if telegram
97
+ return "email - #{email}" if email
98
+ return "phone - #{phone}" if phone
99
+
100
+ nil
101
+ end
102
+
103
+ # Присваивает телефон, Telegram и почту через проверяющие сеттеры.
104
+ def contact=(contacts)
105
+ self.phone = contacts[:phone]
106
+ self.telegram = contacts[:telegram]
107
+ self.email = contacts[:email]
108
+ end
109
+
110
+ # @return [String] фамилия и инициалы для краткого представления
111
+ def last_name_initials
112
+ initials = "#{last_name} #{first_name[0]}."
113
+ initials += " #{patronymic[0]}." if patronymic
114
+ initials
115
+ end
116
+
117
+ # @return [String] ФИО и заполненные поля студента
118
+ def to_s
119
+ fio = "#{last_name} #{first_name}"
120
+ fio += " #{patronymic}" if patronymic
121
+ parts = [fio]
122
+ parts << "ID: #{id}" if id
123
+ parts << "git: #{git}" if git
124
+ parts << "phone: #{phone}" if phone
125
+ parts << "telegram: #{telegram}" if telegram
126
+ parts << "email: #{email}" if email
127
+ parts.join(", ")
128
+ end
129
+
130
+ private
131
+
132
+ def valid_id?(value)
133
+ value.nil? || value.to_s.match?(/\A\d+\z/)
134
+ end
135
+ end
@@ -1,33 +1,35 @@
1
- class StudentBase
2
- attr_reader :id, :git
3
-
4
- def initialize(id:, git: nil)
5
- @id = id
6
- @git = git
7
- end
8
-
9
- def has_git?
10
- !git.nil?
11
- end
12
-
13
- def has_contact?
14
- !contact.nil?
15
- end
16
-
17
- def last_name_initials
18
- raise NotImplementedError, 'Наследник должен реализовать last_name_initials'
19
- end
20
-
21
- def contact
22
- raise NotImplementedError, 'Наследник должен реализовать contact'
23
- end
24
-
25
- def short_info
26
- parts = [last_name_initials]
27
- parts << "ID: #{id}" if id
28
- selected_contact = contact
29
- parts << selected_contact if selected_contact
30
- parts << "git: #{git}" if git
31
- parts.join(", ")
32
- end
33
- end
1
+ # Общий интерфейс полного и краткого представлений студента.
2
+ class StudentBase
3
+ attr_reader :id, :git
4
+
5
+ def initialize(id:, git: nil)
6
+ @id = id
7
+ @git = git
8
+ end
9
+
10
+ def has_git?
11
+ !git.nil?
12
+ end
13
+
14
+ def has_contact?
15
+ !contact.nil?
16
+ end
17
+
18
+ def last_name_initials
19
+ raise NotImplementedError, 'Наследник должен реализовать last_name_initials'
20
+ end
21
+
22
+ def contact
23
+ raise NotImplementedError, 'Наследник должен реализовать contact'
24
+ end
25
+
26
+ # Формирует общую строку из инициалов, ID, доступного контакта и Git.
27
+ def short_info
28
+ parts = [last_name_initials]
29
+ parts << "ID: #{id}" if id
30
+ selected_contact = contact
31
+ parts << selected_contact if selected_contact
32
+ parts << "git: #{git}" if git
33
+ parts.join(", ")
34
+ end
35
+ end
@@ -1,29 +1,33 @@
1
- require_relative 'student_base'
2
-
3
- class StudentShort < StudentBase
4
- attr_reader :last_name_initials, :contact
5
-
6
- def initialize(id:, last_name_initials:, contact: nil, git: nil)
7
- raise ArgumentError, 'ID обязателен' if id.nil?
8
- raise ArgumentError, 'Фамилия и инициалы обязательны' if last_name_initials.nil?
9
-
10
- super(id: id, git: git)
11
-
12
- @last_name_initials = last_name_initials
13
- @contact = contact
14
- end
15
-
16
- def self.from_student(student)
17
- new(
18
- id: student.id,
19
- last_name_initials: student.last_name_initials,
20
- contact: student.contact,
21
- git: student.git
22
- )
23
- end
24
-
25
- def to_s
26
- short_info
27
- end
28
-
29
- end
1
+ require_relative 'student_base'
2
+
3
+ # Краткое представление студента для строк таблицы.
4
+ class StudentShort < StudentBase
5
+ attr_reader :last_name_initials, :contact
6
+
7
+ def initialize(id:, last_name_initials:, contact: nil, git: nil)
8
+ raise ArgumentError, 'ID обязателен' if id.nil?
9
+ raise ArgumentError, 'Фамилия и инициалы обязательны' if last_name_initials.nil?
10
+
11
+ super(id: id, git: git)
12
+
13
+ @last_name_initials = last_name_initials
14
+ @contact = contact
15
+ end
16
+
17
+ # @param student [Student] студент с назначенным ID
18
+ # @return [StudentShort] инициалы, выбранный контакт и Git
19
+ def self.from_student(student)
20
+ new(
21
+ id: student.id,
22
+ last_name_initials: student.last_name_initials,
23
+ contact: student.contact,
24
+ git: student.git
25
+ )
26
+ end
27
+
28
+ # @return [String] краткая информация о студенте
29
+ def to_s
30
+ short_info
31
+ end
32
+
33
+ end