students_list_yaml_gem 0.4

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cecf98bee0f334bcec8cbca09eb99ccd0f7b174ae4a1c53cf3834581abb064a6
4
+ data.tar.gz: 89bd1994e943ceece7eebfed6163ec02a1cd0c21a6587f3651ad0ffa95a0c96a
5
+ SHA512:
6
+ metadata.gz: fded4f002e9346b3309532f82cfb4fe7eb850b476d1e7453f1932035f18937931a11953f297c1b7e3b31fd6066bf513c34a821f1cab3fb99c05797be52630323
7
+ data.tar.gz: 1aea7fd10500541eacdc4328b211ca1e2ae25bde578f62fb5bf335ab0c5851be12e47f8cf43458ad0b75c4e6d4a42812df11ada6fffa8c9fbb4a0654b648d0a2
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # StudentsListYamlGem
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/students_list_yaml_gem`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/students_list_yaml_gem.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,45 @@
1
+ require 'students_list_yaml_gem/data_table'
2
+
3
+ module StudentsListYamlGem
4
+ class DataList
5
+ def initialize(obj_arr)
6
+ @selected = []
7
+ @array = obj_arr
8
+ end
9
+
10
+ def select(number)
11
+ @selected << @array[number]
12
+ end
13
+
14
+ def get_selected
15
+ return @selected.map{|x| x.id}
16
+ end
17
+
18
+ def clear_selected
19
+ @selected = []
20
+ end
21
+
22
+ def get_names
23
+ attr_names = ["№ по порядку"] + template_get_names()
24
+ end
25
+
26
+ def get_data
27
+ table = []
28
+ @array.each_with_index do |x, ind|
29
+ row = [ind] + template_get_data(x)
30
+ table << row
31
+ end
32
+ return DataTable.new(table)
33
+ end
34
+
35
+ protected
36
+ def template_get_data
37
+ raise 'NotImplementedError'
38
+ end
39
+
40
+ protected
41
+ def template_get_names
42
+ raise 'NotImplementedError'
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,23 @@
1
+ require 'students_list_yaml_gem/data_list'
2
+ require 'students_list_yaml_gem/student_short'
3
+
4
+ module StudentsListYamlGem
5
+ class DataListStudentShort < DataList
6
+ def array=(arr)
7
+ @array = arr
8
+ end
9
+
10
+ def initialize(arr)
11
+ super
12
+ self.array=arr
13
+ end
14
+
15
+ private def template_get_data(x)
16
+ info = [x.last_name_initials, x.contact || "", x.git || ""]
17
+ end
18
+
19
+ private def template_get_names
20
+ ["Last name initials", "Contact", "git"]
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module StudentsListYamlGem
2
+ class DataTable
3
+ def initialize(data)
4
+ @table = []
5
+ for x in data do
6
+ @table << x
7
+ end
8
+ end
9
+
10
+ def get_element(row, col)
11
+ return @table[row][col].dup
12
+ end
13
+
14
+ def columns
15
+ return @table[0].size
16
+ end
17
+
18
+ def rows
19
+ return @table.size
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,127 @@
1
+ require 'students_list_yaml_gem/student_superclass'
2
+
3
+ module StudentsListYamlGem
4
+ class Student < SuperStudent
5
+ include Comparable
6
+ attr_reader :last_name, :first_name, :patronymic
7
+
8
+ def self.attr_validator(*attrs)
9
+ attrs.each do |at|
10
+ define_singleton_method("valid_#{at}?") do |value|
11
+ case at
12
+ when :name
13
+ value.is_a?(String) && !value.empty? && value.match?(/^[А-ЯA-Z][а-яa-z]+$/)
14
+ when :email
15
+ value.nil? || (value.is_a?(String) && value.match?(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/))
16
+ when :phone
17
+ value.nil? || (value.is_a?(String) && value.match?(/^\+?[0-9]\(?[0-9]{3}\)?[0-9]{3}\-?[0-9]{2}\-?[0-9]{2}$/))
18
+ when :telegram
19
+ value.nil? || (value.is_a?(String) && value.match?(/^@[a-zA-Z0-9_]{5,32}$/))
20
+ when :git
21
+ value.nil? || (value.is_a?(String) && value.match?(%r{^https://\b(github|gitlab)\b\.com/[a-zA-Z0-9\-\_]+$}))
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ def self.student_attr_writer(*attrs)
28
+ attrs.each do |at|
29
+ define_method("#{at}=") do |value|
30
+ case at
31
+ when :patronymic, :last_name, :first_name
32
+ if self.class.valid_name?(value) && !value.nil?
33
+ instance_variable_set("@#{at}", value)
34
+ end
35
+ when :git
36
+ if self.class.valid_git?(value)
37
+ instance_variable_set("@#{at}", value)
38
+ end
39
+ when :contact
40
+ value.each do |key, val|
41
+ self.class.send("valid_#{key}?", val) ? instance_variable_set("@#{key}", val) : raise(ArgumentError, "Invalid Argument")
42
+ end
43
+ else
44
+ instance_variable_set("@#{at}", nil)
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ private_class_method :attr_validator, :student_attr_writer
51
+ attr_validator :name, :email, :phone, :telegram, :git
52
+ student_attr_writer :last_name, :first_name, :patronymic, :git, :contact
53
+
54
+ def initialize(last_name:, first_name:, id: nil, patronymic: nil, phone: nil, telegram: nil, email: nil, git: nil)
55
+ super(id:)
56
+ self.last_name = last_name
57
+ self.first_name = first_name
58
+ self.patronymic = patronymic
59
+ self.git = git
60
+ self.contact={phone: phone, telegram: telegram, email: email}
61
+ end
62
+
63
+ def contact
64
+ contact = @telegram || @email || @phone
65
+ case contact
66
+ when nil
67
+ return nil
68
+ when @phone
69
+ return "phone - #{contact}"
70
+ when @email
71
+ return "email - #{contact}"
72
+ when @telegram
73
+ return "telegram - #{contact}"
74
+ else
75
+ return nil
76
+ end
77
+ end
78
+
79
+ def last_name_initials
80
+ res = "#{@last_name} #{@first_name[0]}."
81
+ if (@patronymic)
82
+ res += " #{@patronymic[0]}."
83
+ end
84
+ res.strip()
85
+ return res
86
+ end
87
+
88
+ def to_s
89
+ super + [@last_name, @first_name, @patronymic, @phone, @telegram, @email].compact
90
+ end
91
+
92
+ def to_hash
93
+ {
94
+ id: @id,
95
+ last_name: @last_name,
96
+ first_name: @first_name,
97
+ patronymic: @patronymic,
98
+ phone: @phone,
99
+ telegram: @telegram,
100
+ email: @email,
101
+ git: @git
102
+ }.compact
103
+ end
104
+
105
+ def <=>(student2)
106
+ stud1 = self.to_s
107
+ if student2.is_a?(Student) then
108
+ res = self.last_name <=> student2.last_name
109
+ if res != 0
110
+ return res
111
+ end
112
+ res = self.first_name <=> student2.first_name
113
+ if res != 0
114
+ return res
115
+ end
116
+ res = self.patronymic <=> student2.patronymic
117
+ if res != 0
118
+ return res
119
+ end
120
+ return 0
121
+ else
122
+ return nil
123
+ end
124
+ end
125
+
126
+ end
127
+ end
@@ -0,0 +1,30 @@
1
+ require 'students_list_yaml_gem/student'
2
+ require 'students_list_yaml_gem/student_superclass'
3
+
4
+ module StudentsListYamlGem
5
+ class StudentShort < SuperStudent
6
+ attr_reader :last_name_initials, :contact
7
+
8
+ def initialize(id:, last_name_initials:, contact: nil, git: nil)
9
+ super(id:)
10
+ @git = git
11
+ @last_name_initials = last_name_initials
12
+ @contact = contact
13
+ end
14
+
15
+ def self.from_student(student)
16
+ if student.id then
17
+ new(
18
+ id: student.id,
19
+ last_name_initials: student.last_name_initials,
20
+ contact: student.contact,
21
+ git: student.git
22
+ )
23
+ end
24
+ end
25
+
26
+ def to_s
27
+ super + [@last_name_initials, @contact].compact
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ module StudentsListYamlGem
2
+ class SuperStudent
3
+ attr_reader :id, :git
4
+
5
+ def initialize(id: nil)
6
+ @id = id
7
+ end
8
+
9
+ def to_s
10
+ info = []
11
+ info << "#{id}" if @id
12
+ info << "#{git}" if @git
13
+ return info
14
+ end
15
+
16
+ def short_info
17
+ res = "#{id ? "Id: #{self.id}," : ""} #{self.last_name_initials}, #{self.contact || 'no contact'}, git: #{@git || 'no git'},"
18
+ return res
19
+ end
20
+
21
+ def has_git?
22
+ return !self.git.nil?
23
+ end
24
+
25
+ def has_contact?
26
+ return !self.contact.nil?
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,107 @@
1
+ require 'students_list_yaml_gem/data_list_student_short'
2
+
3
+ module StudentsListYamlGem
4
+ class StudentList
5
+
6
+ def initialize
7
+ @data = []
8
+ end
9
+
10
+ def read_all(file)
11
+ content = File.read(file)
12
+ data = deserialize(content)
13
+ @data = data.map do |st|
14
+ Student.new(
15
+ id: st[:id],
16
+ last_name: st[:last_name],
17
+ first_name: st[:first_name],
18
+ patronymic: st[:patronymic],
19
+ phone: st[:phone],
20
+ telegram: st[:telegram],
21
+ email: st[:email],
22
+ git: st[:git]
23
+ )
24
+ end
25
+ end
26
+
27
+ def write_all(file)
28
+ write_data = @data.map {|st| st.to_hash}
29
+ File.open(file, 'w') do |f|
30
+ f.write(serialize(write_data))
31
+ end
32
+ end
33
+
34
+ def get_student(id)
35
+ @data.find{|x| x.id == id}
36
+ end
37
+
38
+ def get_k_n_student_short_list(k, n, dl = nil)
39
+ st = n * (k-1)
40
+ en = st + n - 1
41
+
42
+ arr = []
43
+ (st..en).each do |i|
44
+ arr << StudentShort.from_student(@data[i])
45
+ end
46
+
47
+ if dl == nil
48
+ return DataList.new(arr)
49
+ else
50
+ dl.array=arr
51
+ return dl
52
+ end
53
+ end
54
+
55
+ def sort
56
+ @data.sort_by! {|x| x.last_name_initials }
57
+ end
58
+
59
+ def add(st)
60
+ newid = @data.map {|x| x.id}.max + 1
61
+ s = st.to_hash
62
+ student = Student.new(
63
+ id: newid,
64
+ last_name: s[:last_name],
65
+ first_name: s[:first_name],
66
+ patronymic: s[:patronymic],
67
+ email: s[:email],
68
+ phone: s[:phone],
69
+ telegram: s[:telegram],
70
+ git: s[:git]
71
+ )
72
+ if !@data.find {|x| x.to_hash[:email] == s[:email] || x.to_hash[:phone] == s[:phone] || x.to_hash[:telegram] == s[:telegram] || x.git == s[:git]}
73
+ @data << student
74
+ else
75
+ raise "ValueError"
76
+ end
77
+ end
78
+
79
+ def replace(id, st)
80
+ ind = @data.find_index {|x| x.id == id}
81
+ if ind
82
+ @data[ind] = st
83
+ return @data
84
+ end
85
+ return nil
86
+ end
87
+
88
+ def delete(id)
89
+ @data.reject! {|x| x.id == id}
90
+ end
91
+
92
+ def get_student_short_count
93
+ @data.size
94
+ end
95
+
96
+ protected
97
+ def deserialize
98
+ raise 'NotImplementedError'
99
+ end
100
+
101
+ protected
102
+ def serialize
103
+ raise 'NotImplementedError'
104
+ end
105
+
106
+ end
107
+ end
@@ -0,0 +1,16 @@
1
+ require 'yaml'
2
+ require 'students_list_yaml_gem/students_list'
3
+
4
+ module StudentsListYamlGem
5
+ class StudentListYAML < StudentList
6
+
7
+ private def deserialize(content)
8
+ YAML.load(content, symbolize_names: true)
9
+ end
10
+
11
+ private def serialize(write_data)
12
+ YAML.dump(write_data)
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StudentsListYamlGem
4
+ VERSION = "0.4"
5
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "students_list_yaml_gem/version"
4
+ require "students_list_yaml_gem/student_superclass"
5
+ require "students_list_yaml_gem/student"
6
+ require "students_list_yaml_gem/student_short"
7
+ require "students_list_yaml_gem/data_table"
8
+ require "students_list_yaml_gem/data_list"
9
+ require "students_list_yaml_gem/data_list_student_short"
10
+ require "students_list_yaml_gem/students_list"
11
+ require "students_list_yaml_gem/students_list_yaml"
12
+
13
+ module StudentsListYamlGem
14
+ class Error < StandardError; end
15
+ # Your code goes here...
16
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: students_list_yaml_gem
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.4'
5
+ platform: ruby
6
+ authors:
7
+ - YaZ
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-12-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yaml
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Serializing/deserializing Student and StudentShort class objects into/from
28
+ yaml
29
+ email:
30
+ - aroslavzabelin@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - README.md
36
+ - lib/students_list_yaml_gem.rb
37
+ - lib/students_list_yaml_gem/data_list.rb
38
+ - lib/students_list_yaml_gem/data_list_student_short.rb
39
+ - lib/students_list_yaml_gem/data_table.rb
40
+ - lib/students_list_yaml_gem/student.rb
41
+ - lib/students_list_yaml_gem/student_short.rb
42
+ - lib/students_list_yaml_gem/student_superclass.rb
43
+ - lib/students_list_yaml_gem/students_list.rb
44
+ - lib/students_list_yaml_gem/students_list_yaml.rb
45
+ - lib/students_list_yaml_gem/version.rb
46
+ homepage:
47
+ licenses:
48
+ - MIT
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 3.2.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.5.3
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Serializing/deserializing yaml
69
+ test_files: []