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

конструктор



8
9
10
11
12
# File 'lib/source/strategy/student_list_base.rb', line 8

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.



5
6
7
# File 'lib/source/strategy/student_list_base.rb', line 5

def data_type=(value)
  @data_type = value
end

Instance Method Details

#add_student(student) ⇒ Object



38
39
40
41
42
# File 'lib/source/strategy/student_list_base.rb', line 38

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

#delete_student(id) ⇒ Object



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

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

#get_student_short_count ⇒ Object



56
57
58
# File 'lib/source/strategy/student_list_base.rb', line 56

def get_student_short_count
  students.size
end

#k_n_student_short_list(k, n, data_list) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/source/strategy/student_list_base.rb', line 60

def k_n_student_short_list(k, n, data_list)
  move = (k - 1) * n
  piece = students[move, n].map {|stud| StudentShort.new(stud) }
  return DataListStudentShort.new(piece) if data_list.nil?

  #data_list.append(slice)
  data_list.replace_objects(slice)
  data_list
end

#load_from_file(file_address) ⇒ Object



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

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



44
45
46
47
48
49
50
# File 'lib/source/strategy/student_list_base.rb', line 44

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



22
23
24
25
# File 'lib/source/strategy/student_list_base.rb', line 22

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

#sort_students ⇒ Object



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

def sort_students
  students.sort_by(&:short_name)
end

#student_by_id(student_id) ⇒ Object



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

def student_by_id(student_id)
  r = students.detect { |student_i| student_i.id == student_id}
  puts r.class
  puts 888888
  r
end