Sha256: fd949446eb81415ea1948b0a79723c79c686a38139fc4f2c4f1c248d170d8512

Contents?: true

Size: 1.37 KB

Versions: 14

Compression:

Stored size: 1.37 KB

Contents

class StudentList
  private
	attr_accessor :students, :gen_id, :typer

  public
	def initialize(typer)
		self.students = []
		self.gen_id = students.count + 1
		self.typer = typer
	end

	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

	def write_file(file_path)
		hash_students = students.map(&:to_hash)
		File.write(file_path, typer.write_file(hash_students))
	end

	def get_student(stud_id)
		students.find{|s| s.id == stud_id}
	end

	def sorted
		students.sort_by(&:fio)
	end

	def add_student(student)
		students << student
		student.id = gen_id
		nextId
	end

	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

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

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

  def count
		self.students.count
	end

	def nextId
		self.gen_id=students.max_by(&:id).id + 1
	end

end

Version data entries

14 entries across 14 versions & 5 rubygems

Version Path
mc_gem3-1.0.2 lib/source/Student_list.rb
mc_gem3-1.0.0 lib/source/Student_list.rb
mc_gem2-1.1.5 lib/source/Student_list.rb
mc_gem2-1.1.4 lib/source/Student_list.rb
mvcStudentXD-1.2.3 lib/source/repositories/Student_list.rb
mc_gem-0.0.1 mc_gem/lib/source/repositories/Student_list.rb
mvcStudentXD-1.2.2 lib/source/repositories/Student_list.rb
mvcStudentXD-1.2.1 mvcStudentXD/lib/source/repositories/Student_list.rb
mvcStudentXD-1.2.0 lib/source/repositories/Student_list.rb
mvcStudentXD-1.1.3 lib/source/repositories/Student_list.rb
mvcStudentXD-1.1.2 lib/repositories/Student_list.rb
mvcStudentXD-1.1.1 lib/Student_list.rb
mvcStudentXD-1.0.1 lib/Student_list.rb
model_XD-0.1.0 lib/Student_list.rb