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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 95c6092475db21fe4c65f122426f93af80bad40716d3c43a612ffd7a9b6700ae
4
- data.tar.gz: 33fcb5592ee4b4c9922537ce5c9e9c43d1850714764cbbe23200c156a77789ad
3
+ metadata.gz: 60a29ec36c1f46d57819b067b74cef537456451c37755f45e4aed25fd3b72551
4
+ data.tar.gz: a5423f98228dadbbcd6583480fe1fb54baa29860de666eca572e967863702b99
5
5
  SHA512:
6
- metadata.gz: 8cc4a7aae5c8d4013323fd639e12246ff77e3342742ca44070c576283d46fe842cfbaad8cc5a5ca2682b652daa67e3387b32ac99d9d36ddbecc38e4c9015d90a
7
- data.tar.gz: 378290c9c17c7685a217cac342d3a18ec74c53890644836ccc1c58a60d8767cfaf21165047f2dab2f09c73848ac63a65427c83df49e857c00cafe22b8bdadf23
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
@@ -1,3 +1,3 @@
1
1
  module StudentsListYaml
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,93 +1,116 @@
1
- # frozen_string_literal: true
2
-
3
- require "yaml"
4
- require_relative "students_list_yaml/version"
5
- require_relative "student"
6
- require_relative "student_short"
7
- require_relative "data_list"
8
- require_relative "data_table"
9
- require_relative "base"
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
- module StudentsListYaml
12
- class StudentsList
13
- attr_reader :file_path
16
+ def load_data
17
+ return unless @file_path && File.exist?(@file_path)
14
18
 
15
- def initialize(file_path = nil)
16
- @students = []
17
- @file_path = file_path
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
- def load_data
22
- return unless @file_path && File.exist?(@file_path)
23
+ # YAML.safe_load безопаснее, но требует разрешения на создание ваших классов,
24
+ # если в YAML хранятся объекты. Если там просто хеши — символизация ключей ок.
25
+ data = YAML.safe_load(file_content, permitted_classes: [Date, Symbol], symbolize_names: true)
23
26
 
24
- begin
25
- file_content = File.read(@file_path)
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
- rescue Psych::SyntaxError
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
- def save_data
36
- return unless @file_path
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
- def get_k_n_student_short_list(k, n, existing_data_list = nil)
47
- start_index = (n - 1) * k
48
- selected_students = @students[start_index, k] || []
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
- short_students = selected_students.map { |student| StudentShort.from_student(student) }
49
+ def get_student_by_id(id)
50
+ @students.find { |student| student.id == id }
51
+ end
51
52
 
52
- if existing_data_list && existing_data_list.is_a?(Data_list)
53
- existing_data_list.data = short_students
54
- existing_data_list
55
- else
56
- Data_list_student_short.new(short_students)
57
- end
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
- def sort_by_full_name
61
- @students.sort_by! { |student| student.last_name_initials.downcase }
62
- end
71
+ def sort_by_full_name
72
+ @students.sort_by! { |student| student.last_name_initials.downcase }
73
+ end
63
74
 
64
- def add_student(student)
65
- new_id = @students.empty? ? 1 : @students.map(&:id).max + 1
66
- student_with_id = Student.from_hash(student.to_hash.merge(id: new_id))
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
- def replace_student_by_id(id, new_student)
72
- index = @students.find_index { |s| s.id == id }
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
- @students[index] = Student.from_hash(new_student.to_hash.merge(id: id))
76
- true
77
- end
85
+ @students << student_with_id
86
+ student_with_id
87
+ end
78
88
 
79
- def delete_student_by_id(id)
80
- initial_size = @students.size
81
- @students.reject! { |s| s.id == id }
82
- initial_size != @students.size
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
- def get_student_short_count
86
- @students.size
87
- end
99
+ @students[index] = Student.from_hash(new_student.to_hash.merge(id: id))
100
+ true
101
+ end
88
102
 
89
- private
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
- attr_writer :students
109
+ def get_student_short_count
110
+ @students.size
92
111
  end
112
+
113
+ private
114
+
115
+ attr_accessor :students
93
116
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: students_list_yaml_Quartz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kosta717