Class: StudentListFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/model_gem_source/student_list_format.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(formater) ⇒ StudentListFormat

Returns a new instance of StudentListFormat.



5
6
7
# File 'lib/model_gem_source/student_list_format.rb', line 5

def initialize(formater)
    self.formater = formater
end

Instance Attribute Details

#formaterObject

Returns the value of attribute formater.



3
4
5
# File 'lib/model_gem_source/student_list_format.rb', line 3

def formater
  @formater
end

Instance Method Details

#add_student(student) ⇒ Object



23
24
25
# File 'lib/model_gem_source/student_list_format.rb', line 23

def add_student(student)
    self.students << student
end

#countObject



41
42
43
# File 'lib/model_gem_source/student_list_format.rb', line 41

def count()
    self.students.count
end

#delete_student(id) ⇒ Object



27
28
29
30
# File 'lib/model_gem_source/student_list_format.rb', line 27

def delete_student(id)
    index = students.index(students.detect { |x| x.id == id.to_s })
    self.students.delete_at(index)
end

#get_student(id) ⇒ Object



17
18
19
20
21
# File 'lib/model_gem_source/student_list_format.rb', line 17

def get_student(id)
    self.students.detect { |x|
        x.id == id .to_s
    }
end

#get_students_slice(k, count) ⇒ Object



36
37
38
39
# File 'lib/model_gem_source/student_list_format.rb', line 36

def get_students_slice(k, count)
    from = [k * count, self.students.count].min
    to = [self.students.count, from + count].min
end

#read_from(filename) ⇒ Object



9
10
11
# File 'lib/model_gem_source/student_list_format.rb', line 9

def read_from(filename)
    self.students = formater.read_from(filename)
end

#replace_student(id, student) ⇒ Object



32
33
34
# File 'lib/model_gem_source/student_list_format.rb', line 32

def replace_student(id, student)
    self.students.map! { |x| x.id == id.to_s ? student : x }
end

#sortObject



45
46
47
# File 'lib/model_gem_source/student_list_format.rb', line 45

def sort()
    self.students.sort_by(&:fio_info)
end

#write_to(filename) ⇒ Object



13
14
15
# File 'lib/model_gem_source/student_list_format.rb', line 13

def write_to(filename)
    formater.write_to(filename, self.students)
end