Class: StudentListBase

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_type) ⇒ StudentListBase

конструктор



6
7
8
9
10
# File 'lib/source/strategy/student_list_base.rb', line 6

def initialize(data_type)
  self.students = []
  self.this_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.



3
4
5
# File 'lib/source/strategy/student_list_base.rb', line 3

def data_type=(value)
  @data_type = value
end

Instance Method Details

#add_student(student) ⇒ Object



34
35
36
37
38
# File 'lib/source/strategy/student_list_base.rb', line 34

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

#delete_student(id) ⇒ Object



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

def delete_student(id)
  students.reject!{|stud| stud.id == id}
end

#get_k_n_student_short_list(page, n, data_list) ⇒ Object



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

def get_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_address) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/source/strategy/student_list_base.rb', line 12

def load_from_file(file_address)
  list = data_type.string_to_list(File.read(file_address))
  self.students = list.map{ |i|
    Student.new(**i)
  }
  new_this_id
end

#replace_student(id, student) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/source/strategy/student_list_base.rb', line 40

def replace_student(id, student)
  this_student = students.find_index {
    |stud|
    stud.id == id
  }
  students[this_student] = student
end

#save_to_file(file_address) ⇒ Object



20
21
22
23
# File 'lib/source/strategy/student_list_base.rb', line 20

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

#sort_studentsObject



30
31
32
# File 'lib/source/strategy/student_list_base.rb', line 30

def sort_students
  students.sort_by(&:short_name)
end

#student_by_id(student_id) ⇒ Object



25
26
27
# File 'lib/source/strategy/student_list_base.rb', line 25

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

#student_countObject



59
60
61
# File 'lib/source/strategy/student_list_base.rb', line 59

def student_count
  students.size
end