students_list_yaml_Quartz 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.
- checksums.yaml +4 -4
- data/lib/student.rb +0 -13
- data/lib/students_list_yaml/version.rb +1 -1
- data/lib/students_list_yaml.rb +91 -68
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 60a29ec36c1f46d57819b067b74cef537456451c37755f45e4aed25fd3b72551
|
|
4
|
+
data.tar.gz: a5423f98228dadbbcd6583480fe1fb54baa29860de666eca572e967863702b99
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8b00e3aa69c142229c6215e6d2b2e87dc01186ee1082b3ca93c36fae1faa77a27aadbd21013d7def8db69117e42797f724b9c5242cdcf864d55fc3d16ecb4135
|
|
7
|
+
data.tar.gz: 30ec41abe8cd8896054e939692e04e471b5d37419c0169a2b470111312aeabee837703c8a207c3de81fe25923ef94b71ea724fa169860bf0df6c9168792a2c7d
|
data/lib/student.rb
CHANGED
|
@@ -106,19 +106,6 @@ def contact
|
|
|
106
106
|
@patronymic.to_s <=> other.patronymic.to_s
|
|
107
107
|
end
|
|
108
108
|
|
|
109
|
-
|
|
110
|
-
def ==(other)
|
|
111
|
-
return false unless other.is_a?(Base)
|
|
112
|
-
|
|
113
|
-
contacts_overlap = false
|
|
114
|
-
[@phone, @email, @telegram].each do |contact|
|
|
115
|
-
next if contact.nil? || contact.empty?
|
|
116
|
-
if [other.instance_variable_get(:@phone),
|
|
117
|
-
other.instance_variable_get(:@email),
|
|
118
|
-
other.instance_variable_get(:@telegram)].include?(contact)
|
|
119
|
-
contacts_overlap = true
|
|
120
|
-
break
|
|
121
|
-
end
|
|
122
109
|
end
|
|
123
110
|
|
|
124
111
|
comparison_key == other.comparison_key || contacts_overlap
|
data/lib/students_list_yaml.rb
CHANGED
|
@@ -1,93 +1,116 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
require_relative
|
|
5
|
-
require_relative
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
require 'yaml'
|
|
2
|
+
# Подключаем зависимости, которые должны лежать в папке lib/students_list_yaml/
|
|
3
|
+
require_relative 'students_list_yaml/student'
|
|
4
|
+
require_relative 'students_list_yaml/student_short'
|
|
5
|
+
require_relative 'students_list_yaml/data_list'
|
|
6
|
+
|
|
7
|
+
class Students_List_YAML
|
|
8
|
+
attr_reader :file_path
|
|
9
|
+
|
|
10
|
+
def initialize(file_path = nil)
|
|
11
|
+
@students = []
|
|
12
|
+
@file_path = file_path
|
|
13
|
+
load_data if file_path && File.exist?(file_path)
|
|
14
|
+
end
|
|
10
15
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
attr_reader :file_path
|
|
16
|
+
def load_data
|
|
17
|
+
return unless @file_path && File.exist?(@file_path)
|
|
14
18
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
load_data if file_path && File.exist?(file_path)
|
|
19
|
-
end
|
|
19
|
+
begin
|
|
20
|
+
file_content = File.read(@file_path)
|
|
21
|
+
return if file_content.strip.empty?
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
# YAML.safe_load безопаснее, но требует разрешения на создание ваших классов,
|
|
24
|
+
# если в YAML хранятся объекты. Если там просто хеши — символизация ключей ок.
|
|
25
|
+
data = YAML.safe_load(file_content, permitted_classes: [Date, Symbol], symbolize_names: true)
|
|
23
26
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return if file_content.strip.empty?
|
|
27
|
-
|
|
28
|
-
data = YAML.safe_load(file_content, symbolize_names: true)
|
|
27
|
+
# Проверка: data должен быть массивом
|
|
28
|
+
if data.is_a?(Array)
|
|
29
29
|
@students = data.map { |student_data| Student.from_hash(student_data) }
|
|
30
|
-
|
|
30
|
+
else
|
|
31
31
|
@students = []
|
|
32
32
|
end
|
|
33
|
+
rescue Psych::SyntaxError => e
|
|
34
|
+
puts "Ошибка парсинга YAML: #{e.message}. Создан пустой список."
|
|
35
|
+
@students = []
|
|
33
36
|
end
|
|
37
|
+
end
|
|
34
38
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
students_data = @students.map(&:to_hash)
|
|
39
|
-
File.write(@file_path, students_data.to_yaml)
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def get_student_by_id(id)
|
|
43
|
-
@students.find { |student| student.id == id }
|
|
44
|
-
end
|
|
39
|
+
def student_unic?(student)
|
|
40
|
+
@students.none? { |unic_student| unic_student == student }
|
|
41
|
+
end
|
|
45
42
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
43
|
+
def save_data
|
|
44
|
+
return unless @file_path
|
|
45
|
+
students_data = @students.map(&:to_hash)
|
|
46
|
+
File.write(@file_path, students_data.to_yaml)
|
|
47
|
+
end
|
|
49
48
|
|
|
50
|
-
|
|
49
|
+
def get_student_by_id(id)
|
|
50
|
+
@students.find { |student| student.id == id }
|
|
51
|
+
end
|
|
51
52
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
53
|
+
# ИСПРАВЛЕНИЕ: метод получения списка с пагинацией
|
|
54
|
+
def get_k_n_student_short_list(k, n, existing_data_list = nil)
|
|
55
|
+
start_index = (n - 1) * k
|
|
56
|
+
# Используем slice вместо range с неопределенным end_index
|
|
57
|
+
selected_students = @students.slice(start_index, k) || []
|
|
58
|
+
|
|
59
|
+
student_shorts = selected_students.map { |student| StudentShort.from_student(student) }
|
|
60
|
+
|
|
61
|
+
if existing_data_list
|
|
62
|
+
# Убедитесь, что existing_data_list имеет сеттер data=
|
|
63
|
+
existing_data_list.data = student_shorts
|
|
64
|
+
existing_data_list
|
|
65
|
+
else
|
|
66
|
+
# Убедитесь, что этот класс доступен
|
|
67
|
+
DataListStudentShort.new(student_shorts)
|
|
58
68
|
end
|
|
69
|
+
end
|
|
59
70
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
71
|
+
def sort_by_full_name
|
|
72
|
+
@students.sort_by! { |student| student.last_name_initials.downcase }
|
|
73
|
+
end
|
|
63
74
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
@students << student_with_id
|
|
68
|
-
student_with_id
|
|
75
|
+
def add_student(student)
|
|
76
|
+
unless student_unic?(student)
|
|
77
|
+
raise ArgumentError, "Студент не уникальный"
|
|
69
78
|
end
|
|
79
|
+
# Исправление для пустого массива: если студентов нет, id = 1
|
|
80
|
+
new_id = @students.empty? ? 1 : (@students.map(&:id).max || 0) + 1
|
|
70
81
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
return false unless index
|
|
82
|
+
# Важно: student.to_hash должен возвращать хеш с символьными ключами
|
|
83
|
+
student_with_id = Student.from_hash(student.to_hash.merge(id: new_id))
|
|
74
84
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
85
|
+
@students << student_with_id
|
|
86
|
+
student_with_id
|
|
87
|
+
end
|
|
78
88
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
89
|
+
def replace_student_by_id(id, new_student)
|
|
90
|
+
# Здесь логика правильная, но проверьте, нужно ли проверять уникальность
|
|
91
|
+
# самого себя при замене (иногда это вызывает ошибку, если данные не менялись)
|
|
92
|
+
unless student_unic?(new_student)
|
|
93
|
+
# Можно добавить проверку: если это тот же студент, то ошибку не кидать
|
|
83
94
|
end
|
|
95
|
+
|
|
96
|
+
index = @students.find_index { |s| s.id == id }
|
|
97
|
+
return false unless index
|
|
84
98
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
99
|
+
@students[index] = Student.from_hash(new_student.to_hash.merge(id: id))
|
|
100
|
+
true
|
|
101
|
+
end
|
|
88
102
|
|
|
89
|
-
|
|
103
|
+
def delete_student_by_id(id)
|
|
104
|
+
initial_size = @students.size
|
|
105
|
+
@students.reject! { |s| s.id == id }
|
|
106
|
+
initial_size != @students.size
|
|
107
|
+
end
|
|
90
108
|
|
|
91
|
-
|
|
109
|
+
def get_student_short_count
|
|
110
|
+
@students.size
|
|
92
111
|
end
|
|
112
|
+
|
|
113
|
+
private
|
|
114
|
+
|
|
115
|
+
attr_accessor :students
|
|
93
116
|
end
|