Class: StudentsList
- Inherits:
-
Object
- Object
- StudentsList
- Defined in:
- lib/source/models/student_list/student_list.rb
Instance Method Summary collapse
- #add_student(student) ⇒ Object
-
#get_k_n_student_short_list(page, count, existing_data_list: nil) ⇒ Object
Получить page по счету count элементов (страница начинается с 1).
- #get_student_short_count ⇒ Object
-
#initialize(dt) ⇒ StudentsList
constructor
A new instance of StudentsList.
- #read_from_txt(file_path) ⇒ Object
- #remove(student_id) ⇒ Object
- #replace(student_id, student) ⇒ Object
- #sorted ⇒ Object
- #student_by_id(student_id) ⇒ Object
- #write_to_txt(file_path) ⇒ Object
Constructor Details
#initialize(dt) ⇒ StudentsList
Returns a new instance of StudentsList.
6 7 8 9 |
# File 'lib/source/models/student_list/student_list.rb', line 6 def initialize(dt) self.students = [] self.transformer = dt end |
Instance Method Details
#add_student(student) ⇒ Object
41 42 43 44 45 |
# File 'lib/source/models/student_list/student_list.rb', line 41 def add_student(student) maxID = students.max_by(&:id) student.id = maxID != nil ? maxID.id + 1 : 0 students << student end |
#get_k_n_student_short_list(page, count, existing_data_list: nil) ⇒ Object
Получить page по счету count элементов (страница начинается с 1)
61 62 63 64 65 66 67 68 |
# File 'lib/source/models/student_list/student_list.rb', line 61 def get_k_n_student_short_list(page, count, existing_data_list: nil) offset = (page - 1) * count slice = students[offset, count].map { |s| StudentShort.from_student(s) } return DataListStudentShort.new(list: slice) if existing_data_list.nil? existing_data_list.append(slice) end |
#get_student_short_count ⇒ Object
56 57 58 |
# File 'lib/source/models/student_list/student_list.rb', line 56 def get_student_short_count students.count end |
#read_from_txt(file_path) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/source/models/student_list/student_list.rb', line 11 def read_from_txt(file_path) begin File.foreach(file_path) do |line| students << transformer.parse_to_student(line) end rescue => exception raise "File not found at the given address #{file_path}. Exception: #{exception.}" end end |
#remove(student_id) ⇒ Object
52 53 54 |
# File 'lib/source/models/student_list/student_list.rb', line 52 def remove(student_id) students.reject! { |s| s.id == student_id } end |
#replace(student_id, student) ⇒ Object
47 48 49 50 |
# File 'lib/source/models/student_list/student_list.rb', line 47 def replace(student_id, student) idx = student.find_index { |s| s.id == student_id } students[idx] = student end |
#sorted ⇒ Object
37 38 39 |
# File 'lib/source/models/student_list/student_list.rb', line 37 def sorted students.sort_by(&:fio) end |
#student_by_id(student_id) ⇒ Object
33 34 35 |
# File 'lib/source/models/student_list/student_list.rb', line 33 def student_by_id(student_id) students.detect { |s| s.id == student_id } end |
#write_to_txt(file_path) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/source/models/student_list/student_list.rb', line 21 def write_to_txt(file_path) begin File.open(file_path, 'w') do |file| students.each do |student| file.puts transformer.puts_student(student) end end rescue => exception raise "Error writing to file at the given address #{file_path}. Exception: #{exception.}" end end |