Class: StudentList
- Inherits:
-
Object
- Object
- StudentList
- Defined in:
- lib/source/Student_list.rb
Instance Method Summary collapse
- #add_student(student) ⇒ Object
- #count ⇒ Object
- #get_student(stud_id) ⇒ Object
- #get_students_pag(k, n, existing_data = nil) ⇒ Object
-
#initialize(typer) ⇒ StudentList
constructor
A new instance of StudentList.
- #nextId ⇒ Object
- #read_file(file_path) ⇒ Object
- #remove_student(student_id) ⇒ Object
- #replace_student(student_id, student) ⇒ Object
- #sorted ⇒ Object
- #write_file(file_path) ⇒ Object
Constructor Details
#initialize(typer) ⇒ StudentList
Returns a new instance of StudentList.
6 7 8 9 10 |
# File 'lib/source/Student_list.rb', line 6 def initialize(typer) self.students = [] self.gen_id = students.count + 1 self.typer = typer end |
Instance Method Details
#add_student(student) ⇒ Object
32 33 34 35 36 |
# File 'lib/source/Student_list.rb', line 32 def add_student(student) students << student student.id = gen_id nextId end |
#count ⇒ Object
57 58 59 |
# File 'lib/source/Student_list.rb', line 57 def count self.students.count end |
#get_student(stud_id) ⇒ Object
24 25 26 |
# File 'lib/source/Student_list.rb', line 24 def get_student(stud_id) students.find{|s| s.id == stud_id} end |
#get_students_pag(k, n, existing_data = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/source/Student_list.rb', line 38 def get_students_pag(k,n,existing_data = nil) skip = (k-1) * n new_data = students[skip, n].map{|s| StudentShort.from_student_class(s)} return DataListStudentShort.new(new_data) if existing_data.nil? existing_data.replace_objects(new_data) existing_data end |
#nextId ⇒ Object
61 62 63 |
# File 'lib/source/Student_list.rb', line 61 def nextId self.gen_id=students.max_by(&:id).id + 1 end |
#read_file(file_path) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/source/Student_list.rb', line 12 def read_file(file_path) raise ArgumentError.new("File not found #{file_path}") unless File.file?(file_path) hash_students = typer.read_file(File.read(file_path)) self.students = hash_students.map{|h| Student.from_hash(h)} nextId end |
#remove_student(student_id) ⇒ Object
53 54 55 |
# File 'lib/source/Student_list.rb', line 53 def remove_student(student_id) self.students.reject! {|s| s.id==student_id} end |
#replace_student(student_id, student) ⇒ Object
48 49 50 51 |
# File 'lib/source/Student_list.rb', line 48 def replace_student(student_id, student) idx = student.find{|s| s.id==student.id} self.students[idx]=student end |
#sorted ⇒ Object
28 29 30 |
# File 'lib/source/Student_list.rb', line 28 def sorted students.sort_by(&:fio) end |
#write_file(file_path) ⇒ Object
19 20 21 22 |
# File 'lib/source/Student_list.rb', line 19 def write_file(file_path) hash_students = students.map(&:to_hash) File.write(file_path, typer.write_file(hash_students)) end |