mc_gem 0.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 +7 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +91 -0
- data/Lab1/arr1_3.txt +1 -0
- data/Lab1/lb1_1.rb +19 -0
- data/Lab1/lb1_2.rb +28 -0
- data/Lab1/lb1_3.rb +46 -0
- data/Lab1/lb1_4.rb +82 -0
- data/Lab2/1Class.jpg +0 -0
- data/Lab2/Web/main.rb +89 -0
- data/Lab2/Web/views/add.html +168 -0
- data/Lab2/Web/views/main.html +233 -0
- data/Lab2/data_storage/students1.txt +1 -0
- data/Lab2/data_storage/studentsRead.json +36 -0
- data/Lab2/data_storage/studentsRead.txt +3 -0
- data/Lab2/data_storage/studentsRead.yaml +26 -0
- data/Lab2/data_storage/studentsWrite.json +42 -0
- data/Lab2/data_storage/studentsWrite.txt +4 -0
- data/Lab2/data_storage/studentsWrite.yaml +31 -0
- data/Lab2/examples/Strategy.rb +38 -0
- data/Lab2/examples/adapter.rb +30 -0
- data/Lab2/examples/database.rb +15 -0
- data/Lab2/examples/observer_example.rb +51 -0
- data/Lab2/examples/pattern_pattern.rb +44 -0
- data/Lab2/examples/site_example.rb +21 -0
- data/Lab2/examples/views/about.erb +11 -0
- data/Lab2/examples/views/contact.erb +15 -0
- data/Lab2/examples/views/index.erb +11 -0
- data/Lab2/main.rb +6 -0
- data/Lab2/main_window.rb +31 -0
- data/Lab2/student_input_form.rb +71 -0
- data/Lab2/tab_students.rb +157 -0
- data/Lab2/test/student_test.rb +91 -0
- data/README.md +3 -0
- data/mc_gem/CHANGELOG.md +5 -0
- data/mc_gem/CODE_OF_CONDUCT.md +84 -0
- data/mc_gem/Documentation.md +33 -0
- data/mc_gem/Gemfile +13 -0
- data/mc_gem/LICENSE.txt +21 -0
- data/mc_gem/README.md +1 -0
- data/mc_gem/lib/mc_gem/version.rb +5 -0
- data/mc_gem/lib/mc_gem.rb +10 -0
- data/mc_gem/lib/source/adapters/student_list_adapter.rb +95 -0
- data/mc_gem/lib/source/containers/Data_list.rb +66 -0
- data/mc_gem/lib/source/containers/Data_list_student_short.rb +17 -0
- data/mc_gem/lib/source/containers/Data_table.rb +25 -0
- data/mc_gem/lib/source/controllers/student_edit_form_controller.rb +63 -0
- data/mc_gem/lib/source/controllers/student_input_form_controller.rb +56 -0
- data/mc_gem/lib/source/controllers/student_list_controller.rb +83 -0
- data/mc_gem/lib/source/converters/Converter.rb +11 -0
- data/mc_gem/lib/source/converters/Converter_json.rb +14 -0
- data/mc_gem/lib/source/converters/Converter_txt.rb +26 -0
- data/mc_gem/lib/source/converters/Converter_yaml.rb +14 -0
- data/mc_gem/lib/source/database/scripts/create_table.sql +10 -0
- data/mc_gem/lib/source/database/scripts/insert_data.sql +4 -0
- data/mc_gem/lib/source/database/student_list_db.rb +41 -0
- data/mc_gem/lib/source/database/students_db.rb +79 -0
- data/mc_gem/lib/source/model/Student.rb +106 -0
- data/mc_gem/lib/source/model/StudentBase.rb +50 -0
- data/mc_gem/lib/source/model/Student_short.rb +39 -0
- data/mc_gem/lib/source/repositories/Student_list.rb +65 -0
- data/mc_gem/lib/source/repositories/student_list_adv.rb +35 -0
- data/mc_gem/lib/source/util/LoggerHolder.rb +22 -0
- data/mc_gem/mc_gem.gemspec +17 -0
- data/mc_gem/sig/mvcStudentXD.rbs +4 -0
- metadata +121 -0
@@ -0,0 +1,25 @@
|
|
1
|
+
class DataTable
|
2
|
+
|
3
|
+
attr_accessor :data
|
4
|
+
|
5
|
+
def initialize(data)
|
6
|
+
self.data = data
|
7
|
+
end
|
8
|
+
|
9
|
+
def get_cell(row,col)
|
10
|
+
self.data[row][col]
|
11
|
+
end
|
12
|
+
|
13
|
+
def num_columns
|
14
|
+
self.data[0].length
|
15
|
+
end
|
16
|
+
|
17
|
+
def num_rows
|
18
|
+
self.data.length
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_2d_array
|
22
|
+
data.dup
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative '../util/LoggerHolder'
|
3
|
+
class StudentEditFormController
|
4
|
+
def initialize(parent_controller, existing_student_id)
|
5
|
+
@parent_controller = parent_controller
|
6
|
+
@existing_student_id = existing_student_id
|
7
|
+
LoggerHolder.instance.debug('StudentEditFormController: initialized')
|
8
|
+
end
|
9
|
+
|
10
|
+
def set_view(view)
|
11
|
+
@view = view
|
12
|
+
end
|
13
|
+
|
14
|
+
def refresh
|
15
|
+
@parent_controller.refresh_data(1,20)
|
16
|
+
end
|
17
|
+
|
18
|
+
def on_view_created
|
19
|
+
begin
|
20
|
+
@student_list = StudentListAdv.new(StudentsListDBAdapter.new(StudentListDB.instance))
|
21
|
+
rescue Mysql2::Error::ConnectionError=>e
|
22
|
+
on_db_conn_error(e)
|
23
|
+
end
|
24
|
+
@existing_student = @student_list.get_student(@existing_student_id)
|
25
|
+
|
26
|
+
@view.make_readonly(:git, :telegram, :email, :phone)
|
27
|
+
populate_fields(@existing_student)
|
28
|
+
end
|
29
|
+
|
30
|
+
def populate_fields(student)
|
31
|
+
@view.set_value(:last_name, student.last_name)
|
32
|
+
@view.set_value(:first_name, student.first_name)
|
33
|
+
@view.set_value(:parental_name, student.parental_name)
|
34
|
+
@view.set_value(:git, student.git)
|
35
|
+
@view.set_value(:telegram, student.telegram)
|
36
|
+
@view.set_value(:email, student.email)
|
37
|
+
@view.set_value(:phone, student.phone)
|
38
|
+
print("IM IN Populate EDIT")
|
39
|
+
end
|
40
|
+
|
41
|
+
def process_fields(fields)
|
42
|
+
begin
|
43
|
+
new_student = Student.from_hash(fields)
|
44
|
+
@student_list.replace_student(@existing_student_id, new_student)
|
45
|
+
LoggerHolder.instance.debug('StudentEditFormController: replacing student in DB')
|
46
|
+
@view.close
|
47
|
+
rescue ArgumentError => e
|
48
|
+
LoggerHolder.instance.debug("StudentEditFormController: wrong fields: #{e.message}")
|
49
|
+
api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
|
50
|
+
api.call(0, e.message, 'Error', 0)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def on_db_conn_error(error)
|
57
|
+
LoggerHolder.instance.debug('StudentEditFormController: DB connection error:')
|
58
|
+
LoggerHolder.instance.error(error.message)
|
59
|
+
api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
|
60
|
+
api.call(0, "No connection to DB", "Error", 0)
|
61
|
+
@view.close
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'win32api'
|
4
|
+
require_relative '../util/LoggerHolder'
|
5
|
+
class StudentInputFormController
|
6
|
+
def initialize(parent_controller)
|
7
|
+
@parent_controller = parent_controller
|
8
|
+
LoggerHolder.instance.debug('StudentInputFormController: initialized')
|
9
|
+
end
|
10
|
+
|
11
|
+
def set_view(view)
|
12
|
+
@view = view
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def refresh
|
17
|
+
@parent_controller.refresh_data(1,20)
|
18
|
+
end
|
19
|
+
|
20
|
+
def on_view_created
|
21
|
+
begin
|
22
|
+
@student_list = StudentListAdv.new(StudentsListDBAdapter.new(StudentListDB.instance))
|
23
|
+
rescue Mysql2::Error::ConnectionError=>e
|
24
|
+
on_db_conn_error(e)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def process_fields(fields)
|
29
|
+
begin
|
30
|
+
last_name = fields.delete(:last_name)
|
31
|
+
first_name = fields.delete(:first_name)
|
32
|
+
parental_name = fields.delete(:parental_name)
|
33
|
+
|
34
|
+
return if last_name.nil? || first_name.nil? || parental_name.nil?
|
35
|
+
|
36
|
+
student = Student.new(last_name, first_name, parental_name, **fields)
|
37
|
+
|
38
|
+
@student_list.add_student(student)
|
39
|
+
LoggerHolder.instance.debug('StudentInputFormController: adding student to DB')
|
40
|
+
@view.close
|
41
|
+
rescue ArgumentError => e
|
42
|
+
api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
|
43
|
+
LoggerHolder.instance.debug("StudentInputFormController: wrong fields: #{e.message}")
|
44
|
+
api.call(0, e.message, 'Error', 0)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def on_db_conn_error(error)
|
50
|
+
LoggerHolder.instance.debug('StudentInputFormController: DB connection error:')
|
51
|
+
LoggerHolder.instance.error(error.message)
|
52
|
+
api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
|
53
|
+
api.call(0, "No connection to DB", "Error", 0)
|
54
|
+
@view.close
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
require_relative '../repositories/student_list_adv'
|
5
|
+
require_relative '../adapters/student_list_adapter'
|
6
|
+
require_relative '../database/student_list_db'
|
7
|
+
require_relative '../converters/Converter_json'
|
8
|
+
require_relative '../converters/Converter'
|
9
|
+
require_relative '../repositories/Student_list'
|
10
|
+
require_relative '../controllers/student_edit_form_controller'
|
11
|
+
require_relative '../controllers/student_input_form_controller'
|
12
|
+
require_relative '../../../../Lab2/student_input_form'
|
13
|
+
require 'win32api'
|
14
|
+
require 'mysql2'
|
15
|
+
require_relative '../util/LoggerHolder'
|
16
|
+
class StudentListController
|
17
|
+
def initialize(view)
|
18
|
+
LoggerHolder.instance.debug('StudentListController: init start')
|
19
|
+
@view = view
|
20
|
+
@data_list = DataListStudentShort.new
|
21
|
+
@data_list.add_listener(@view)
|
22
|
+
LoggerHolder.instance.debug('StudentListController: init done')
|
23
|
+
end
|
24
|
+
|
25
|
+
def on_view_created
|
26
|
+
begin
|
27
|
+
@student_list = StudentListAdv.new(StudentsListDBAdapter.new(StudentListDB.instance))
|
28
|
+
LoggerHolder.instance.debug('StudentListController: created student repository')
|
29
|
+
rescue Mysql2::Error::ConnectionError=>e
|
30
|
+
on_db_conn_error(e)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def delete_selected(current_page, per_page, selected_row)
|
35
|
+
begin
|
36
|
+
LoggerHolder.instance.debug('StudentListController: deleting selected student')
|
37
|
+
student_num = (current_page - 1) * per_page + selected_row
|
38
|
+
@data_list.select_element(student_num)
|
39
|
+
student_id = @data_list.selected_id
|
40
|
+
@student_list.remove_student(student_id)
|
41
|
+
rescue => e
|
42
|
+
on_db_conn_error(e)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def show_modal_add
|
47
|
+
LoggerHolder.instance.debug('StudentListController: showing modal (add)')
|
48
|
+
controller = StudentInputFormController.new(self)
|
49
|
+
view = StudentInputForm.new(controller)
|
50
|
+
controller.set_view(view)
|
51
|
+
view.create.show
|
52
|
+
end
|
53
|
+
|
54
|
+
def show_modal_edit(current_page, per_page, selected_row)
|
55
|
+
LoggerHolder.instance.debug('StudentListController: showing modal (edit)')
|
56
|
+
student_num = (current_page - 1) * per_page + selected_row
|
57
|
+
@data_list.select_element(student_num)
|
58
|
+
student_id = @data_list.selected_id
|
59
|
+
controller = StudentEditFormController.new(self, student_id)
|
60
|
+
view = StudentInputForm.new(controller)
|
61
|
+
controller.set_view(view)
|
62
|
+
view.create.show
|
63
|
+
end
|
64
|
+
|
65
|
+
def refresh_data(page, per_page)
|
66
|
+
begin
|
67
|
+
@data_list = @student_list.get_students_pag(page, per_page, @data_list)
|
68
|
+
@view.update_student_count(@student_list.count)
|
69
|
+
rescue => e
|
70
|
+
on_db_conn_error(e)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
def on_db_conn_error(error)
|
76
|
+
LoggerHolder.instance.error('TabStudentsController: DB connection error:')
|
77
|
+
LoggerHolder.instance.error(error.message)
|
78
|
+
api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
|
79
|
+
api.call(0, "No connection to DB", "Error", 0)
|
80
|
+
# TODO: Возможность переключения на JSON помимо exit
|
81
|
+
exit(false)
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'json'
|
2
|
+
require_relative '../converters/Converter'
|
3
|
+
class ConverterJSON < Converter
|
4
|
+
public_class_method :new
|
5
|
+
|
6
|
+
def read_file(file_content)
|
7
|
+
JSON.parse(file_content, {symbolize_names: true})
|
8
|
+
end
|
9
|
+
|
10
|
+
def write_file(hash_students)
|
11
|
+
JSON.pretty_generate(hash_students)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative '../verter'
|
2
|
+
class ConverterTxt < Converter
|
3
|
+
public_class_method :new
|
4
|
+
|
5
|
+
def read_file(data)
|
6
|
+
result = []
|
7
|
+
file_content = data.split("\n")
|
8
|
+
file_content.each do |line|
|
9
|
+
hash = {}
|
10
|
+
puts(line)
|
11
|
+
pairs = line.split(',').map{|pair| pair.gsub(/\s+/, '').split(':')}
|
12
|
+
pairs.each do |pair|
|
13
|
+
hash[pair[0].to_sym]=(pair[0]=="id") ? pair[1].to_i : pair[1]
|
14
|
+
end
|
15
|
+
result << hash
|
16
|
+
end
|
17
|
+
result
|
18
|
+
end
|
19
|
+
|
20
|
+
def write_file(hash_students)
|
21
|
+
string_arr = hash_students.map do |hash|
|
22
|
+
hash.map{|k,v| "#{k}:#{v}"}.join(',')
|
23
|
+
end.join("\n")
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require_relative '../verter'
|
3
|
+
class ConverterYAML < Converter
|
4
|
+
public_class_method :new
|
5
|
+
|
6
|
+
def read_file(file_content)
|
7
|
+
YAML.safe_load(file_content).map{ |h| h.transform_keys(&:to_sym)}
|
8
|
+
end
|
9
|
+
|
10
|
+
def write_file(hash_students)
|
11
|
+
YAML.dump(hash_students.map{ |h| h.transform_keys(&:to_s)})
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
CREATE TABLE IF NOT EXISTS student(
|
2
|
+
id INT NOT NULL auto_increment PRIMARY KEY,
|
3
|
+
last_name CHAR(255) NOT NULL,
|
4
|
+
first_name CHAR(255) NOT NULL,
|
5
|
+
parental_name CHAR(255) NOT NULL,
|
6
|
+
git CHAR(255),
|
7
|
+
phone CHAR(255),
|
8
|
+
email CHAR(255),
|
9
|
+
telegram CHAR(255)
|
10
|
+
);
|
@@ -0,0 +1,4 @@
|
|
1
|
+
INSERT INTO student (last_name, first_name, parental_name, git, phone, email, telegram) VALUES
|
2
|
+
ROW("Худокормов", "Дмитрий", "Александрович", "@KiTTeNqz", NULL, NULL, "@KiTTeNqzi"),
|
3
|
+
ROW("Александров", "Александр", "Александрович", "@AleXxx", "89094494499", NULL, NULL),
|
4
|
+
ROW("Ермаков", "Ермак", "Ермакович", "@yaErmak1", NULL, "ERMAK@yandex.ru", NULL)
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative './students_db.rb'
|
3
|
+
|
4
|
+
class StudentListDB
|
5
|
+
attr_accessor :database
|
6
|
+
|
7
|
+
@@instance = nil
|
8
|
+
|
9
|
+
def self.instance
|
10
|
+
@@instance ||= StudentListDB.new
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
self.database = StudentDB.new()
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_student(id)
|
19
|
+
Student.from_hash(database.select_by_id(id).transform_keys(&:to_sym))
|
20
|
+
end
|
21
|
+
|
22
|
+
def remove_student(id)
|
23
|
+
database.remove_by_id(id)
|
24
|
+
end
|
25
|
+
|
26
|
+
def replace_student(id, student)
|
27
|
+
database.replace_by_id(id, student)
|
28
|
+
end
|
29
|
+
|
30
|
+
def add_student(student)
|
31
|
+
database.add_student(student.to_hash)
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_students_pag(k, n, data)
|
35
|
+
database.get_students_pag(k, n, data)
|
36
|
+
end
|
37
|
+
|
38
|
+
def count
|
39
|
+
database.count
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# #encoding: UTF-8
|
3
|
+
require 'mysql2'
|
4
|
+
require_relative '../containers/Data_list_student_short'
|
5
|
+
require_relative '../containers/Data_list'
|
6
|
+
require_relative '../model/Student'
|
7
|
+
require_relative '../model/Student_short'
|
8
|
+
class StudentDB
|
9
|
+
|
10
|
+
attr_accessor :db_connection
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
self.db_connection = Mysql2::Client.new(:host => "localhost", :username => "dimas", :password => "qwe123")
|
14
|
+
self.db_connection.query('CREATE DATABASE IF NOT EXISTS stud_db')
|
15
|
+
self.db_connection.query('USE stud_db')
|
16
|
+
self.db_connection.query('DROP TABLE IF EXISTS student')
|
17
|
+
self.db_connection.query(File.read('C:/Users/Dmitry/RubymineProjects/RubyLabs/mc_gem/lib/source/database/scripts/create_table.sql'))
|
18
|
+
self.insert_data
|
19
|
+
end
|
20
|
+
|
21
|
+
def insert_data
|
22
|
+
db_connection.query(File.read('C:/Users/Dmitry/RubymineProjects/RubyLabs/mc_gem/lib/source/database/scripts/insert_data.sql'))
|
23
|
+
end
|
24
|
+
|
25
|
+
def select_by_id(id)
|
26
|
+
db_connection.query("SELECT * FROM student WHERE id = #{id}").map{|x| x}[0]
|
27
|
+
end
|
28
|
+
|
29
|
+
def remove_by_id(id)
|
30
|
+
db_connection.query("DELETE FROM student WHERE id = #{id}")
|
31
|
+
end
|
32
|
+
|
33
|
+
def parseNil(attr)
|
34
|
+
if attr == nil
|
35
|
+
"NULL"
|
36
|
+
else
|
37
|
+
attr
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def add_student(student_data)
|
42
|
+
|
43
|
+
db_connection.query("""
|
44
|
+
INSERT INTO student (last_name, first_name, parental_name, git, phone, email, telegram) VALUES
|
45
|
+
ROW(
|
46
|
+
\"#{parseNil(student_data[:last_name])}\",
|
47
|
+
\"#{parseNil(student_data[:first_name])}\",
|
48
|
+
\"#{parseNil(student_data[:parental_name])}\",
|
49
|
+
\"#{parseNil(student_data[:git])}\",
|
50
|
+
\"#{parseNil(student_data[:phone])}\",
|
51
|
+
\"#{parseNil(student_data[:email])}\",
|
52
|
+
\"#{parseNil(student_data[:telegram])}\"
|
53
|
+
)
|
54
|
+
""")
|
55
|
+
end
|
56
|
+
|
57
|
+
def replace_by_id(id, student_data)
|
58
|
+
self.remove_by_id(id)
|
59
|
+
self.add_student(student_data.to_hash)
|
60
|
+
end
|
61
|
+
|
62
|
+
def get_students_pag(k, n, existing_data = nil)
|
63
|
+
rows = db_connection.query("SELECT * FROM student ORDER BY id LIMIT #{n} OFFSET #{(k-1)*n}")
|
64
|
+
data_list = DataListStudentShort.new
|
65
|
+
rows.each do |row|
|
66
|
+
row_sym = row.transform_keys{|key| key.to_sym}
|
67
|
+
data_list.append(StudentShort.from_student_class(Student.from_hash(row_sym)))
|
68
|
+
end
|
69
|
+
return data_list if existing_data.nil?
|
70
|
+
existing_data.replace_objects(data_list.objects)
|
71
|
+
existing_data
|
72
|
+
end
|
73
|
+
|
74
|
+
def count
|
75
|
+
result = db_connection.query("SELECT count(*) FROM student")
|
76
|
+
result.first.values.first
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'json'
|
2
|
+
require_relative '../model/StudentBase'
|
3
|
+
class Student < StudentBase
|
4
|
+
|
5
|
+
public_class_method :new
|
6
|
+
attr_writer :id
|
7
|
+
public
|
8
|
+
attr_reader :last_name, :first_name, :parental_name, :id, :phone, :git, :telegram, :email
|
9
|
+
|
10
|
+
def initialize(last_name, first_name, parental_name, options = {})
|
11
|
+
super(last_name, first_name, parental_name)
|
12
|
+
self.id = options[:id]
|
13
|
+
self.phone = options[:phone]
|
14
|
+
self.git = options[:git]
|
15
|
+
self.telegram = options[:telegram]
|
16
|
+
self.email = options[:email]
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_s
|
20
|
+
[
|
21
|
+
"#{last_name} #{first_name} #{parental_name}",
|
22
|
+
id ? "id: #{id}" : nil,
|
23
|
+
git ? "git: #{git}" : nil,
|
24
|
+
phone ? "phone: #{phone}" : nil,
|
25
|
+
telegram ? "telegram: #{telegram}" : nil,
|
26
|
+
email ? "email: #{email}" : nil
|
27
|
+
].compact.join(' ')
|
28
|
+
end
|
29
|
+
|
30
|
+
#Setters
|
31
|
+
def phone=(phone)
|
32
|
+
raise ArgumentError, "ERROR phone=#{phone}" unless phone.nil? || StudentBase.validate_phone?(phone)
|
33
|
+
@phone = phone
|
34
|
+
end
|
35
|
+
|
36
|
+
def telegram=(tg_name)
|
37
|
+
raise ArgumentError, "ERROR telegram=#{tg_name}" unless telegram.nil? || StudentBase.validate_git_tg?(tg_name)
|
38
|
+
@telegram=tg_name
|
39
|
+
end
|
40
|
+
|
41
|
+
def git=(git_name)
|
42
|
+
raise ArgumentError, "ERROR git=#{git_name}" unless git.nil? || StudentBase.validate_git_tg?(git_name)
|
43
|
+
@git=git_name
|
44
|
+
end
|
45
|
+
|
46
|
+
def email=(email1)
|
47
|
+
raise ArgumentError, "ERROR email=#{email1}" unless email.nil? || StudentBase.validate_email?(email1)
|
48
|
+
@email=email1
|
49
|
+
end
|
50
|
+
|
51
|
+
def set_contacts(contacts={})
|
52
|
+
self.phone=contacts[:phone]
|
53
|
+
self.telegram=contacts[:telegram]
|
54
|
+
self.email=contacts[:email]
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.from_str(string)
|
58
|
+
stud = string.split(',')
|
59
|
+
.map{|v| v.split(':')}
|
60
|
+
.map{|v| [v[0].to_sym, v[1]]}
|
61
|
+
.to_h
|
62
|
+
last_name, first_name, parental_name = stud[:fio].split(' ')
|
63
|
+
Student.new(last_name, first_name, parental_name, stud)
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.from_hash(hash)
|
67
|
+
raise ArgumentError,"Missing req fields" unless hash.key?(:last_name) && hash.key?(:first_name) && hash.key?(:parental_name)
|
68
|
+
#переделал этот метод после тестов. Теперь вынимаю обязательные параметры из хэша, не удаляя из него
|
69
|
+
Student.new(hash[:last_name], hash[:first_name], hash[:parental_name], hash)
|
70
|
+
end
|
71
|
+
|
72
|
+
def get_short_fio
|
73
|
+
"fio:#{last_name} #{first_name[0]}. #{parental_name[0]}."
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
def get_short_contact
|
78
|
+
contact = %i[telegram phone email].find{|cont| send(cont)}
|
79
|
+
{type: contact, val: send(contact)} if contact
|
80
|
+
end
|
81
|
+
|
82
|
+
def get_git
|
83
|
+
"git:#{git}"
|
84
|
+
end
|
85
|
+
|
86
|
+
def get_info
|
87
|
+
"#{get_short_fio},#{get_short_contact[:type]}:#{get_short_contact[:val]},#{get_git}"
|
88
|
+
end
|
89
|
+
|
90
|
+
def get_info_hash
|
91
|
+
info={}
|
92
|
+
info[:fio] = get_short_fio
|
93
|
+
info[:contact] = get_short_contact
|
94
|
+
info[:git] = git
|
95
|
+
info
|
96
|
+
end
|
97
|
+
|
98
|
+
def to_hash
|
99
|
+
fields = {}
|
100
|
+
%i[last_name first_name parental_name id phone git telegram email].each do |field|
|
101
|
+
field_val = send(field)
|
102
|
+
fields[field] = field_val unless field_val.nil?
|
103
|
+
end
|
104
|
+
fields
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'json'
|
2
|
+
class StudentBase
|
3
|
+
private_class_method :new
|
4
|
+
|
5
|
+
def self.validate_phone?(phone)
|
6
|
+
return phone.match(/^\+?[7,8] ?\(?\d{3}\)? ?\d{3}-?\d{2}-?\d{2}$/)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.validate_email?(email)
|
10
|
+
return email.match(/^[A-Za-z0-9\-_]+@[A-Za-z]+(\.[A-Za-z]+)?\.[A-Za-z]+$/)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.validate_git_tg?(acc_name)
|
14
|
+
return acc_name.match(/^@[A-Za-z0-9\-_]+$/)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.validate_name?(prof_name)
|
18
|
+
return prof_name.match(/(^[А-Я][а-я]+$)|(^[A-Z][a-z]+$)/)
|
19
|
+
end
|
20
|
+
|
21
|
+
def valid_cont?
|
22
|
+
!email.nil? || !telegram.nil? || !phone.nil?
|
23
|
+
end
|
24
|
+
|
25
|
+
def validate?
|
26
|
+
!git.nil? && valid_cont?
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize(last_name, first_name, parental_name)
|
30
|
+
self.last_name = last_name
|
31
|
+
self.first_name = first_name
|
32
|
+
self.parental_name = parental_name
|
33
|
+
end
|
34
|
+
|
35
|
+
def first_name=(first_name1)
|
36
|
+
raise ArgumentError, "ERROR first_name=#{first_name1}" unless StudentBase.validate_name?(first_name1)
|
37
|
+
@first_name=first_name1
|
38
|
+
end
|
39
|
+
|
40
|
+
def last_name=(last_name1)
|
41
|
+
raise ArgumentError, "ERROR last_name=#{last_name1}" unless StudentBase.validate_name?(last_name1)
|
42
|
+
@last_name=last_name1
|
43
|
+
end
|
44
|
+
|
45
|
+
def parental_name=(parental_name1)
|
46
|
+
raise ArgumentError, "ERROR parental_name=#{parental_name1}" unless StudentBase.validate_name?(parental_name1)
|
47
|
+
@parental_name=parental_name1
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require_relative '../model/StudentBase'
|
2
|
+
class StudentShort < StudentBase
|
3
|
+
|
4
|
+
public_class_method :new
|
5
|
+
|
6
|
+
#У нас уже есть некоторые гет\сет в базе, зачем же ещё?
|
7
|
+
private
|
8
|
+
attr_writer :fio, :contact, :git, :id
|
9
|
+
|
10
|
+
public
|
11
|
+
attr_reader :fio, :contact, :git, :id
|
12
|
+
|
13
|
+
def self.from_student_class(student)
|
14
|
+
StudentShort.new(student.id, student.get_info)
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(id, str)
|
18
|
+
info_short = str.split(',')
|
19
|
+
.map{|x| x.split(':')}
|
20
|
+
.map{|x| [x[0].to_sym, x[1]]}
|
21
|
+
.to_h
|
22
|
+
raise ArgumentError, 'Missing fields: fio' if info_short[:fio].nil?
|
23
|
+
print(info_short)
|
24
|
+
self.id=id
|
25
|
+
self.fio = info_short[:fio]
|
26
|
+
self.git = info_short[:git]
|
27
|
+
info_short.delete_if{|k,v| k==:fio||k==:git}
|
28
|
+
self.contact = info_short.values.first
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
def to_s
|
33
|
+
|
34
|
+
[
|
35
|
+
"#{id}, #{fio}, #{git}, #{contact}"
|
36
|
+
].compact.join(' ')
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|