rybish_code 1.1.4 → 1.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rybish_code/version.rb +5 -0
- data/lib/{rubbish_code.rb → rybish_code.rb} +2 -2
- data/lib/source/controllers/student_input_form_controller.rb +43 -0
- data/lib/source/controllers/student_input_form_controller_edit.rb +51 -0
- data/lib/source/controllers/tab_students_controller.rb +68 -82
- data/lib/source/models/data_list/data_list.rb +47 -0
- data/lib/source/models/data_list/data_list_short.rb +46 -0
- data/lib/source/models/data_list/data_table.rb +23 -0
- data/lib/source/models/student/student.rb +98 -0
- data/lib/source/models/student/student_abstract.rb +21 -0
- data/lib/source/models/student/student_short.rb +38 -0
- data/lib/source/models/student_list/data_transformers.rb +43 -0
- data/lib/source/models/student_list/student_list.rb +70 -0
- data/lib/source/support/meta_code.rb +62 -0
- data/{rubbish_code.gemspec → rybish_code.gemspec} +3 -3
- metadata +16 -22
- data/CHANGELOG.md +0 -5
- data/CODE_OF_CONDUCT.md +0 -84
- data/README.md +0 -1
- data/lib/rubbish_code/version.rb +0 -5
- data/lib/source/db_config/config.yaml +0 -5
- data/lib/source/db_config/migrations/001_create_table_student.sql +0 -12
- data/lib/source/db_config/mock_data/fill_student.sql +0 -5
- data/lib/source/repositories/adapters/db_source_adapter.rb +0 -54
- data/lib/source/repositories/adapters/file_source_adapter.rb +0 -37
- data/lib/source/repositories/containers/data_list.rb +0 -74
- data/lib/source/repositories/containers/data_list_student_short.rb +0 -18
- data/lib/source/repositories/containers/data_table.rb +0 -35
- data/lib/source/repositories/data_sources/db_data_source.rb +0 -37
- data/lib/source/repositories/data_sources/file_data_source.rb +0 -75
- data/lib/source/repositories/data_sources/transformers/data_transformer_base.rb +0 -15
- data/lib/source/repositories/data_sources/transformers/data_transformer_json.rb +0 -16
- data/lib/source/repositories/data_sources/transformers/data_transformer_yaml.rb +0 -16
- data/lib/source/repositories/student_repository.rb +0 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e09972789f424e2541e157cb6dcadefed4d477e1efb6bd80710d4ae27e27bfff
|
4
|
+
data.tar.gz: b97f74213ba3356bc99263b55495964f25e2d07ff5fe6f2daf3bc81fcac9a788
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 521f45b3bdafe593f2cc2de1b8bbfc5a81edce2fe8f474fc4ffcab03bd536bc03c7c551cd0e737d724fa6e54274687b5b1d050eeb945fd8e1a1a362ef40616dd
|
7
|
+
data.tar.gz: 8c2b2dc6845596ec1f5fb40aa511c4ac31145ad9feb784272fa053d2ef9902530680ec1c11a3fcd3fb0bc703bb76b944f618ec25f88bbf4c4bf6669b8a836f76
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'win32api'
|
4
|
+
|
5
|
+
class StudentInputFormControllerCreate
|
6
|
+
def initialize(parent_controller)
|
7
|
+
@parent_controller = parent_controller
|
8
|
+
end
|
9
|
+
|
10
|
+
def set_view(view)
|
11
|
+
@view = view
|
12
|
+
end
|
13
|
+
|
14
|
+
def on_view_created
|
15
|
+
end
|
16
|
+
|
17
|
+
def process_fields(fields)
|
18
|
+
begin
|
19
|
+
last_name = fields.delete(:last_name)
|
20
|
+
first_name = fields.delete(:first_name)
|
21
|
+
paternal_name = fields.delete(:paternal_name)
|
22
|
+
|
23
|
+
return if last_name.nil? || first_name.nil? || paternal_name.nil?
|
24
|
+
|
25
|
+
student = Student.new(last_name, first_name, paternal_name, **fields)
|
26
|
+
|
27
|
+
StudentsListDB.add_student(student)
|
28
|
+
|
29
|
+
@view.close
|
30
|
+
rescue ArgumentError => e
|
31
|
+
api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
|
32
|
+
api.call(0, e.message, 'Error', 0)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def on_db_conn_error
|
39
|
+
api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
|
40
|
+
api.call(0, "No connection to DB", "Error", 0)
|
41
|
+
@view.close
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'win32api'
|
4
|
+
|
5
|
+
class StudentInputFormControllerEdit
|
6
|
+
def initialize(parent_controller, existing_student_id)
|
7
|
+
@parent_controller = parent_controller
|
8
|
+
@existing_student_id = existing_student_id
|
9
|
+
end
|
10
|
+
|
11
|
+
def set_view(view)
|
12
|
+
@view = view
|
13
|
+
end
|
14
|
+
|
15
|
+
def on_view_created
|
16
|
+
@existing_student = StudentsListDB.get_student(@existing_student_id)
|
17
|
+
@view.make_readonly(:git, :telegram, :email, :phone)
|
18
|
+
populate_fields(@existing_student)
|
19
|
+
end
|
20
|
+
|
21
|
+
def populate_fields(student)
|
22
|
+
@view.set_value(:last_name, student.last_name)
|
23
|
+
@view.set_value(:first_name, student.first_name)
|
24
|
+
@view.set_value(:paternal_name, student.paternal_name)
|
25
|
+
@view.set_value(:git, student.git)
|
26
|
+
@view.set_value(:telegram, student.telegram)
|
27
|
+
@view.set_value(:email, student.email)
|
28
|
+
@view.set_value(:phone, student.phone)
|
29
|
+
end
|
30
|
+
|
31
|
+
def process_fields(fields)
|
32
|
+
begin
|
33
|
+
new_student = Student.from_json(JSON(fields))
|
34
|
+
|
35
|
+
StudentsListDB.replace_student(@existing_student_id, new_student)
|
36
|
+
|
37
|
+
@view.close
|
38
|
+
rescue ArgumentError => e
|
39
|
+
api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
|
40
|
+
api.call(0, e.message, 'Error', 0)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def on_db_conn_error
|
47
|
+
api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
|
48
|
+
api.call(0, "No connection to DB", "Error", 0)
|
49
|
+
@view.close
|
50
|
+
end
|
51
|
+
end
|
@@ -1,82 +1,68 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
@
|
17
|
-
@
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
def
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
view
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
private
|
73
|
-
|
74
|
-
def on_db_conn_error(error)
|
75
|
-
LoggerHolder.instance.error('TabStudentsController: DB connection error:')
|
76
|
-
LoggerHolder.instance.error(error.message)
|
77
|
-
api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
|
78
|
-
api.call(0, "No connection to DB", "Error", 0)
|
79
|
-
# TODO: Возможность переключения на JSON помимо exit
|
80
|
-
exit(false)
|
81
|
-
end
|
82
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../ui/main_screen'
|
4
|
+
require_relative '../data_base/students_list_db'
|
5
|
+
require_relative '../models/data_list/data_list_short'
|
6
|
+
require_relative '../event/event_manager'
|
7
|
+
require_relative '../event/event_update_students_table'
|
8
|
+
require_relative '../event/event_update_students_count'
|
9
|
+
require_relative 'student_input_form_controller'
|
10
|
+
require_relative 'student_input_form_controller_edit'
|
11
|
+
|
12
|
+
class TabStudentsController
|
13
|
+
def initialize(view, student_per_page)
|
14
|
+
@view = view
|
15
|
+
@data_list = DataListStudentShort.new(list: [])
|
16
|
+
@student_per_page = student_per_page
|
17
|
+
@current_page = 1
|
18
|
+
end
|
19
|
+
|
20
|
+
def show_view
|
21
|
+
@view.create.show
|
22
|
+
end
|
23
|
+
|
24
|
+
def show_modal_add
|
25
|
+
controller = StudentInputFormControllerCreate.new(self)
|
26
|
+
view = StudentInputForm.new(controller,self)
|
27
|
+
controller.set_view(view)
|
28
|
+
view.create.show
|
29
|
+
end
|
30
|
+
|
31
|
+
def show_modal_edit(selected_row)
|
32
|
+
student_num = (@current_page - 1) * @student_per_page + selected_row
|
33
|
+
student_id = @data_list.get_id_by_index(student_num)
|
34
|
+
controller = StudentInputFormControllerEdit.new(self, student_id)
|
35
|
+
view = StudentInputForm.new(controller, self)
|
36
|
+
controller.set_view(view)
|
37
|
+
view.create.show
|
38
|
+
end
|
39
|
+
|
40
|
+
def delete_selected(selected_row)
|
41
|
+
begin
|
42
|
+
student_num = (@current_page - 1) * @student_per_page + selected_row
|
43
|
+
student_id = @data_list.get_id_by_index(student_num)
|
44
|
+
StudentsListDB.remove_student(student_id)
|
45
|
+
rescue
|
46
|
+
on_db_conn_error
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
def refresh_data
|
52
|
+
StudentsListDB.get_students_slice(@current_page, @student_per_page, @data_list)
|
53
|
+
EventManager.notify(EventUpdateStudentsCount.new(StudentsListDB.count))
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_page
|
57
|
+
@current_page
|
58
|
+
end
|
59
|
+
|
60
|
+
def next_page(is_left)
|
61
|
+
if is_left
|
62
|
+
@current_page = [@current_page - 1, 1].max
|
63
|
+
else
|
64
|
+
@current_page = [@current_page + 1, (@total_count / STUDENTS_PER_PAGE.to_f).ceil].min
|
65
|
+
end
|
66
|
+
@controller.refresh_data(@current_page)
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require_relative "../../support/meta_code"
|
2
|
+
|
3
|
+
class DataList
|
4
|
+
private_attr_accessor :list
|
5
|
+
private_attr_accessor :selected_objects
|
6
|
+
|
7
|
+
def initialize(list:)
|
8
|
+
self.list = list
|
9
|
+
self.selected_objects = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def select(index)
|
13
|
+
selected_objects << list[index].id
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_id_by_index(index)
|
17
|
+
list[index].id
|
18
|
+
end
|
19
|
+
|
20
|
+
MESS = "SYSTEM ERROR: method missing"
|
21
|
+
protected def get_vars; raise MESS; end
|
22
|
+
protected def filter(object); raise MESS; end
|
23
|
+
protected def create_data(objects_names); raise MESS; end
|
24
|
+
protected def get_real_vars; raise MESS; end
|
25
|
+
protected def format_data(objects); end
|
26
|
+
|
27
|
+
def get_names
|
28
|
+
filter(get_vars)
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_data
|
32
|
+
DataTable.new(
|
33
|
+
data: create_data(
|
34
|
+
(0..list.size-1).map { |object_index|
|
35
|
+
get_real_vars
|
36
|
+
}
|
37
|
+
)
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
def replace_objects(objects)
|
42
|
+
objects = format_data(objects)
|
43
|
+
|
44
|
+
self.list = objects.dup
|
45
|
+
EventManager.notify(EventUpdateStudentsTable.new(get_data, get_names))
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative './data_list.rb'
|
2
|
+
require_relative './data_table.rb'
|
3
|
+
require_relative '../student/student_short.rb'
|
4
|
+
|
5
|
+
class DataListStudentShort < DataList
|
6
|
+
|
7
|
+
private_attr_accessor :names
|
8
|
+
|
9
|
+
def initialize(list:)
|
10
|
+
super(list: list)
|
11
|
+
|
12
|
+
self.names = get_names
|
13
|
+
end
|
14
|
+
|
15
|
+
private def get_vars
|
16
|
+
["ID","ФИО","Git","Контакт"]
|
17
|
+
end
|
18
|
+
|
19
|
+
private def get_real_vars
|
20
|
+
["fio","git","contact"]
|
21
|
+
end
|
22
|
+
|
23
|
+
private def filter(object)
|
24
|
+
object.filter { |x| x != "ID" }
|
25
|
+
end
|
26
|
+
|
27
|
+
private def format_data(objects)
|
28
|
+
if objects[0].is_a?(Student)
|
29
|
+
objects = objects.map { |x| StudentShort.from_student(x)}
|
30
|
+
end
|
31
|
+
objects
|
32
|
+
end
|
33
|
+
|
34
|
+
private def create_data(objects_names)
|
35
|
+
(0...objects_names.size).map { |item_index|
|
36
|
+
(0...objects_names[item_index].size + 1).map { |name_index|
|
37
|
+
if name_index == 0 then item_index else
|
38
|
+
tmp = list[item_index].instance_variable_get("@#{objects_names[item_index][name_index - 1]}")
|
39
|
+
tmp
|
40
|
+
end
|
41
|
+
}
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class DataTable
|
2
|
+
private_attr_accessor :data
|
3
|
+
|
4
|
+
def initialize(data:)
|
5
|
+
self.data = data
|
6
|
+
end
|
7
|
+
|
8
|
+
def element(row:, column:)
|
9
|
+
data[row][column]
|
10
|
+
end
|
11
|
+
|
12
|
+
def columns_count
|
13
|
+
data[0].size
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_2d_array
|
17
|
+
data.dup
|
18
|
+
end
|
19
|
+
|
20
|
+
def rows_count
|
21
|
+
data.size
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'json'
|
3
|
+
require 'yaml'
|
4
|
+
require_relative "../../support/meta_code"
|
5
|
+
require_relative "./student_abstract.rb"
|
6
|
+
|
7
|
+
class Student < AbstractStudent
|
8
|
+
attr_writer :id
|
9
|
+
attr_validate_accessor :phone, '/^\+?[7,8] ?\(?\d{3}\)?-?\d{3}-?\d{2}-?\d{2}$/'
|
10
|
+
attr_validate_accessor :email, '/\w*@\w*.\w{2,3}/'
|
11
|
+
attr_validate_accessor :telegram, '/@\w*/'
|
12
|
+
attr_validate_accessor :git, '/@\w*/'
|
13
|
+
attr_validate_accessor :last_name, '/^[А-Я][а-я]+$/'
|
14
|
+
attr_validate_accessor :first_name, '/^[А-Я][а-я]+$/'
|
15
|
+
attr_validate_accessor :paternal_name, '/^[А-Я][а-я]+$/'
|
16
|
+
|
17
|
+
def initialize(last_name, first_name, paternal_name, options = {})
|
18
|
+
self.last_name = last_name
|
19
|
+
self.first_name = first_name
|
20
|
+
self.paternal_name = paternal_name
|
21
|
+
self.id = options[:id] == 'NULL' ? nil : options[:id]
|
22
|
+
self.phone = options[:phone] == 'NULL' ? nil : options[:phone]
|
23
|
+
self.git = options[:git] == 'NULL' ? nil : options[:git]
|
24
|
+
self.telegram = options[:telegram] == 'NULL' ? nil : options[:telegram]
|
25
|
+
self.email = options[:email] == 'NULL' ? nil : options[:email]
|
26
|
+
end
|
27
|
+
|
28
|
+
attr_is_have_reader :fio
|
29
|
+
def fio
|
30
|
+
"#{last_name} #{first_name.upcase[0]}. #{paternal_name.upcase[0]}."
|
31
|
+
end
|
32
|
+
|
33
|
+
attr_is_have_reader :contact
|
34
|
+
def contact
|
35
|
+
return "#{phone}" unless phone.nil?
|
36
|
+
return "#{telegram}" unless telegram.nil?
|
37
|
+
return "#{email}" unless email.nil?
|
38
|
+
nil
|
39
|
+
end
|
40
|
+
|
41
|
+
def validate
|
42
|
+
git? && contact?
|
43
|
+
end
|
44
|
+
|
45
|
+
def set_contacts(contacts)
|
46
|
+
self.phone = contacts[:phone] if contacts.key?(:phone)
|
47
|
+
self.telegram = contacts[:telegram] if contacts.key?(:telegram)
|
48
|
+
self.email = contacts[:email] if contacts.key?(:email)
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.from_json(str)
|
52
|
+
unless str.is_a?(String)
|
53
|
+
str = JSON.pretty_generate(str)
|
54
|
+
end
|
55
|
+
result = JSON.parse(str)
|
56
|
+
raise ArgumentError, 'Missing fields: last_name, first_name, paternal_name' unless result.key?('first_name') && result.key?('last_name') && result.key?('paternal_name')
|
57
|
+
|
58
|
+
last_name = result.delete('last_name')
|
59
|
+
first_name = result.delete('first_name')
|
60
|
+
paternal_name = result.delete('paternal_name')
|
61
|
+
Student.new(last_name, first_name, paternal_name, **result.transform_keys(&:to_sym))
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.from_yaml(str)
|
65
|
+
result = YAML.load(str)
|
66
|
+
raise ArgumentError, 'Missing fields: last_name, first_name, paternal_name' unless result.key?('first_name') && result.key?('last_name') && result.key?('paternal_name')
|
67
|
+
|
68
|
+
last_name = result.delete('last_name')
|
69
|
+
first_name = result.delete('first_name')
|
70
|
+
paternal_name = result.delete('paternal_name')
|
71
|
+
Student.new(last_name, first_name, paternal_name, **result.transform_keys(&:to_sym))
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.read_from_txt(file_path)
|
75
|
+
arr = []
|
76
|
+
begin
|
77
|
+
File.foreach(file_path) do |line|
|
78
|
+
arr += [Student.from_json(line)]
|
79
|
+
end
|
80
|
+
return arr
|
81
|
+
rescue => exception
|
82
|
+
raise "File not found at the given address #{file_path}. Exception: #{exception.message}"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.write_to_txt(file_path, students)
|
87
|
+
begin
|
88
|
+
File.open(file_path, 'w') do |file|
|
89
|
+
students.each do |student|
|
90
|
+
file.puts student.to_s
|
91
|
+
end
|
92
|
+
end
|
93
|
+
rescue => exception
|
94
|
+
raise "Error writing to file at the given address #{file_path}. Exception: #{exception.message}"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../../support/meta_code'
|
4
|
+
|
5
|
+
class AbstractStudent
|
6
|
+
attr_reader :git, :contact, :id
|
7
|
+
|
8
|
+
getter :fio, :git, :contact, :id
|
9
|
+
|
10
|
+
def get_info
|
11
|
+
"#{get_fio};#{get_contact};#{get_git}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
vars = {}
|
16
|
+
instance_variables.map do |attribute|
|
17
|
+
vars[attribute.to_s[1..]] = instance_variable_get(attribute)
|
18
|
+
end
|
19
|
+
JSON(vars)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'json'
|
3
|
+
require_relative "../../support/meta_code"
|
4
|
+
require_relative "./student_abstract.rb"
|
5
|
+
|
6
|
+
class StudentShort < AbstractStudent
|
7
|
+
attr_reader :fio
|
8
|
+
private_attr_writer :git, :contact, :id, :fio
|
9
|
+
|
10
|
+
def initialize(id:, fio:, git:, contact:)
|
11
|
+
self.id = id
|
12
|
+
self.fio = fio
|
13
|
+
self.git = git
|
14
|
+
self.contact = contact
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.from_student(student)
|
18
|
+
StudentShort.new(
|
19
|
+
id: student.id,
|
20
|
+
fio: "#{student.first_name} #{student.last_name.upcase[0]}. #{student.paternal_name.upcase[0]}.",
|
21
|
+
git: student.git,
|
22
|
+
contact: student.contact
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.from_json(json)
|
27
|
+
result = JSON.parse(json)
|
28
|
+
raise ArgumentError, 'Missing field: fio' unless result.key?('fio')
|
29
|
+
|
30
|
+
fio = result.delete('fio')
|
31
|
+
StudentShort.new(
|
32
|
+
id: result['id'],
|
33
|
+
fio: fio,
|
34
|
+
git: result['git'],
|
35
|
+
contact: result['contact']
|
36
|
+
)
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
class DataTransformer
|
5
|
+
|
6
|
+
MESS = "SYSTEM ERROR: method missing"
|
7
|
+
def parse_to_student(line); raise MESS; end
|
8
|
+
|
9
|
+
def puts_student(student); raise MESS; end
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
class DataTransformerYaml < DataTransformer
|
14
|
+
|
15
|
+
def parse_to_student(line)
|
16
|
+
Student.from_yaml(line)
|
17
|
+
end
|
18
|
+
|
19
|
+
def puts_student(student)
|
20
|
+
vars = {}
|
21
|
+
student.instance_variables.map do |attribute|
|
22
|
+
vars[attribute.to_s[1..-1]]=student.instance_variable_get(attribute)
|
23
|
+
end
|
24
|
+
(vars).to_yaml
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
class DataTransformerJson < DataTransformer
|
30
|
+
|
31
|
+
def parse_to_student(line)
|
32
|
+
Student.from_json(line)
|
33
|
+
end
|
34
|
+
|
35
|
+
def puts_student(student)
|
36
|
+
vars = {}
|
37
|
+
student.instance_variables.map do |attribute|
|
38
|
+
vars[attribute.to_s[1..-1]]=student.instance_variable_get(attribute)
|
39
|
+
end
|
40
|
+
JSON(vars)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
class StudentsList
|
2
|
+
private_attr_accessor :students
|
3
|
+
private_attr_accessor :transformer
|
4
|
+
|
5
|
+
|
6
|
+
def initialize(dt)
|
7
|
+
self.students = []
|
8
|
+
self.transformer = dt
|
9
|
+
end
|
10
|
+
|
11
|
+
def read_from_txt(file_path)
|
12
|
+
begin
|
13
|
+
File.foreach(file_path) do |line|
|
14
|
+
students << transformer.parse_to_student(line)
|
15
|
+
end
|
16
|
+
rescue => exception
|
17
|
+
raise "File not found at the given address #{file_path}. Exception: #{exception.message}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def write_to_txt(file_path)
|
22
|
+
begin
|
23
|
+
File.open(file_path, 'w') do |file|
|
24
|
+
students.each do |student|
|
25
|
+
file.puts transformer.puts_student(student)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
rescue => exception
|
29
|
+
raise "Error writing to file at the given address #{file_path}. Exception: #{exception.message}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def student_by_id(student_id)
|
34
|
+
students.detect { |s| s.id == student_id }
|
35
|
+
end
|
36
|
+
|
37
|
+
def sorted
|
38
|
+
students.sort_by(&:fio)
|
39
|
+
end
|
40
|
+
|
41
|
+
def add_student(student)
|
42
|
+
maxID = students.max_by(&:id)
|
43
|
+
student.id = maxID != nil ? maxID.id + 1 : 0
|
44
|
+
students << student
|
45
|
+
end
|
46
|
+
|
47
|
+
def replace(student_id, student)
|
48
|
+
idx = student.find_index { |s| s.id == student_id }
|
49
|
+
students[idx] = student
|
50
|
+
end
|
51
|
+
|
52
|
+
def remove(student_id)
|
53
|
+
students.reject! { |s| s.id == student_id }
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_student_short_count
|
57
|
+
students.count
|
58
|
+
end
|
59
|
+
|
60
|
+
# Получить page по счету count элементов (страница начинается с 1)
|
61
|
+
def get_k_n_student_short_list(page, count, existing_data_list: nil)
|
62
|
+
offset = (page - 1) * count
|
63
|
+
slice = students[offset, count].map { |s| StudentShort.from_student(s) }
|
64
|
+
|
65
|
+
return DataListStudentShort.new(list: slice) if existing_data_list.nil?
|
66
|
+
|
67
|
+
existing_data_list.append(slice)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|