Class: StudentListBase

Inherits:
Object
  • Object
show all
Defined in:
lib/source/repositories/data_sources/student_list_base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_type) ⇒ StudentListBase

конструктор



9
10
11
12
13
# File 'lib/source/repositories/data_sources/student_list_base.rb', line 9

def initialize(data_type)
  self.students = []
  self.cur_id = 1
  self.data_type = data_type
end

Instance Attribute Details

#data_type=(value) ⇒ Object

Sets the attribute data_type

Parameters:

  • value

    the value to set the attribute data_type to.



6
7
8
# File 'lib/source/repositories/data_sources/student_list_base.rb', line 6

def data_type=(value)
  @data_type = value
end

Instance Method Details

#add_student(student) ⇒ Object

добавление студента



53
54
55
56
57
# File 'lib/source/repositories/data_sources/student_list_base.rb', line 53

def add_student(student)
  student.id = cur_id
  students << student
  self.cur_id += 1
end

#k_n_student_short_list(page, n, data_list) ⇒ Object

Получить page по счету count элементов (страница начинается с 1)



39
40
41
42
43
44
45
# File 'lib/source/repositories/data_sources/student_list_base.rb', line 39

def k_n_student_short_list(page, n, data_list)
  page_list = students[(page-1)*n, n].map{|st| StudentShort.new(st)}
  return DataListStudentShort.new(page_list) if data_list.nil?

  data_list.replace_objects(page_list)
  data_list
end

#load_from_file(file_path) ⇒ Object

загрузка из файла



16
17
18
19
20
# File 'lib/source/repositories/data_sources/student_list_base.rb', line 16

def load_from_file(file_path)
  list = data_type.str_to_list(File.read(file_path))
  self.students = list.map { |h| Student.from_hash(h) }
  update_cur_id
end

#remove_student(student_id) ⇒ Object

отчисление студента))



66
67
68
# File 'lib/source/repositories/data_sources/student_list_base.rb', line 66

def remove_student(student_id)
  students.reject! { |s| s.id == student_id }
end

#replace_student(student_id, student) ⇒ Object

замена студента



60
61
62
63
# File 'lib/source/repositories/data_sources/student_list_base.rb', line 60

def replace_student(student_id, student)
  idx = students.find_index { |s| s.id == student_id }
  students[idx] = student
end

#save_to_file(file_path) ⇒ Object

выгрузка в файл



23
24
25
26
# File 'lib/source/repositories/data_sources/student_list_base.rb', line 23

def save_to_file(file_path)
  list = students.map(&:to_hash)
  File.write(file_path, data_type.list_to_str(list))
end

#sortedObject

сортировка



48
49
50
# File 'lib/source/repositories/data_sources/student_list_base.rb', line 48

def sorted
  students.sort_by(&:last_name_and_initials)
end

#student_by_id(student_id) ⇒ Object

найти студента по айди



29
30
31
# File 'lib/source/repositories/data_sources/student_list_base.rb', line 29

def student_by_id(student_id)
  students.detect { |s| s.id == student_id }
end

#student_by_name(student_name) ⇒ Object

найти студента по фамилии и инциалам



34
35
36
# File 'lib/source/repositories/data_sources/student_list_base.rb', line 34

def student_by_name(student_name)
  students.filter { |s| s.last_name_and_initials == student_name }
end

#student_countObject

число студентов



71
72
73
# File 'lib/source/repositories/data_sources/student_list_base.rb', line 71

def student_count
  students.size
end