student_list_yaml 1.0.0 → 1.0.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.
- checksums.yaml +4 -4
- data/lib/student_list_base.rb +99 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f22bb36f6d03a29e56a3a4af7298ad2a51f93b9a2ab865124f2fb229c7a191cd
|
|
4
|
+
data.tar.gz: 778b6dd35518589bf1bd4b69cc775f0cdf82ba28595b7f22757f763acc4956a4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ece7981c5e88f463696f04a1c3fa4c9d18c0c6d5405c9ffce063b0df3f8497855c8ea18e432fe06a10d42e28857c7858ee617b2cb81cfd52f10dad6c49ace098
|
|
7
|
+
data.tar.gz: 3df31db99690dc0d6c549fbd86e4c6ba437368aaf0abd25ea2350077ca273087606394646f44c2280c623222625e33ed256e6e155d8479d8e2c84296ee6ee623
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
require_relative 'student/student'
|
|
2
|
+
require_relative 'student/student_short'
|
|
3
|
+
require_relative 'student/data_list_student_short'
|
|
4
|
+
|
|
5
|
+
# Базовый класс для работы со списками студентов.
|
|
6
|
+
# Определяет общий интерфейс для чтения/записи данных студентов.
|
|
7
|
+
class StudentListBase
|
|
8
|
+
attr_reader :students
|
|
9
|
+
|
|
10
|
+
def initialize(file_path)
|
|
11
|
+
@file_path = file_path
|
|
12
|
+
@students = []
|
|
13
|
+
read_from_file
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def read_from_file
|
|
17
|
+
raise NotImplementedError, "Метод должен быть реализован в наследнике"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def write_to_file
|
|
21
|
+
raise NotImplementedError, "Метод должен быть реализован в наследнике"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def get_student_by_id(id)
|
|
25
|
+
@students.find { |student| student.id == id }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Получает список студентов для отображения на странице.
|
|
29
|
+
# Возвращает объект DataListStudentShort
|
|
30
|
+
def get_k_n_student_short_list(k, n, data_list = nil)
|
|
31
|
+
sorted_students = @students.sort_by(&:last_name_initials)
|
|
32
|
+
start_index = (k - 1) * n
|
|
33
|
+
end_index = start_index + n - 1
|
|
34
|
+
|
|
35
|
+
k_n_students = sorted_students[start_index..end_index] || []
|
|
36
|
+
student_shorts = k_n_students.map { |student| StudentShort.from_student(student) }
|
|
37
|
+
|
|
38
|
+
if data_list
|
|
39
|
+
data_list.data_list = student_shorts
|
|
40
|
+
data_list
|
|
41
|
+
else
|
|
42
|
+
DataListStudentShort.from_array(student_shorts)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def sort_by_last_name_initials
|
|
47
|
+
@students.sort_by!(&:last_name_initials)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def student_unic?(student)
|
|
51
|
+
@students.none? { |unic_student| unic_student == student }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Добавляет нового студента в список.
|
|
55
|
+
# Вызывает исключение, если студент уже существует.
|
|
56
|
+
def add_student(student)
|
|
57
|
+
unless student_unic?(student)
|
|
58
|
+
raise ArgumentError, "Студент не уникален"
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
existing_ids = @students.map(&:id)
|
|
62
|
+
new_id = existing_ids.empty? ? 1 : existing_ids.max + 1
|
|
63
|
+
|
|
64
|
+
hash = student.student_to_hash.merge(id: new_id)
|
|
65
|
+
student_with_id = Student.new(**hash)
|
|
66
|
+
|
|
67
|
+
@students << student_with_id
|
|
68
|
+
student_with_id
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def replace_student_by_id(id, new_student)
|
|
72
|
+
unless student_unic?(new_student)
|
|
73
|
+
raise ArgumentError, "Студент не уникален"
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
index = @students.find_index { |student| student.id == id }
|
|
77
|
+
return nil unless index
|
|
78
|
+
|
|
79
|
+
updated_student = Student.new(**new_student.student_to_hash, id: id)
|
|
80
|
+
|
|
81
|
+
@students[index] = updated_student
|
|
82
|
+
updated_student
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def delete_student_by_id(id)
|
|
86
|
+
@students.reject! { |student| student.id == id }
|
|
87
|
+
write_to_file
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def get_student_short_count
|
|
91
|
+
@students.size
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
protected
|
|
95
|
+
|
|
96
|
+
def students_to_hash
|
|
97
|
+
@students.map(&:student_to_hash)
|
|
98
|
+
end
|
|
99
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: student_list_yaml
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- dice365
|
|
@@ -15,6 +15,7 @@ executables: []
|
|
|
15
15
|
extensions: []
|
|
16
16
|
extra_rdoc_files: []
|
|
17
17
|
files:
|
|
18
|
+
- lib/student_list_base.rb
|
|
18
19
|
- lib/student_list_yaml.rb
|
|
19
20
|
licenses:
|
|
20
21
|
- MIT
|