Class: StudentListDbAdapter
- Inherits:
-
Object
- Object
- StudentListDbAdapter
- Defined in:
- lib/source/repositories/student_list_db_adapter.rb
Instance Method Summary collapse
-
#add_student(student) ⇒ Object
добавление студента.
- #from_array_to_hash(arr) ⇒ Object
-
#initialize ⇒ StudentListDbAdapter
constructor
новый конструктор.
- #k_n_student_short_list(k, n, data_list = nil) ⇒ Object
-
#remove_student(student_id) ⇒ Object
отчисление студента.
-
#replace_student(student_id, student) ⇒ Object
обновление студента.
-
#student_by_id(student_id) ⇒ Object
вернуть студента по ид.
- #student_count ⇒ Object
Constructor Details
#initialize ⇒ StudentListDbAdapter
новый конструктор
7 8 9 |
# File 'lib/source/repositories/student_list_db_adapter.rb', line 7 def initialize self.client = DBUniversity.instance end |
Instance Method Details
#add_student(student) ⇒ Object
добавление студента
47 48 49 |
# File 'lib/source/repositories/student_list_db_adapter.rb', line 47 def add_student(student) stmt = client.prepare_exec('insert into students (first_name, last_name, paternal_name, phone, telegram, email, git) VALUES (?, ?, ?, ?, ?, ?, ?)', *student_fields(student)) end |
#from_array_to_hash(arr) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/source/repositories/student_list_db_adapter.rb', line 11 def from_array_to_hash(arr) attrs = {} i=0 i[id last_name first_name paternal_name phone telegram email git].each do |attr| attrs[attr] = arr[i] unless arr[i].nil? i=i+1 end attrs end |
#k_n_student_short_list(k, n, data_list = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/source/repositories/student_list_db_adapter.rb', line 31 def k_n_student_short_list(k,n, data_list=nil) offset = (k - 1) * n students = client.prepare_exec('SELECT * FROM students LIMIT ?, ?', offset, n) slice = students.map { |h| h = h.transform_keys(&:to_sym) StudentShort.new(Student.from_hash(h)) } return DataListStudentShort.new(slice) if data_list.nil? data_list.replace_objects(slice) data_list end |
#remove_student(student_id) ⇒ Object
отчисление студента
52 53 54 |
# File 'lib/source/repositories/student_list_db_adapter.rb', line 52 def remove_student(student_id) client.prepare_exec('DELETE FROM students WHERE id = ?', student_id) end |
#replace_student(student_id, student) ⇒ Object
обновление студента
57 58 59 60 61 |
# File 'lib/source/repositories/student_list_db_adapter.rb', line 57 def replace_student(student_id, student) template = 'UPDATE students SET first_name=?, last_name=?, paternal_name=?, phone=?, telegram=?, email=?, git=? WHERE id=?' client.prepare_exec(template, *student_fields(student), student_id) end |
#student_by_id(student_id) ⇒ Object
вернуть студента по ид
22 23 24 25 26 27 28 29 |
# File 'lib/source/repositories/student_list_db_adapter.rb', line 22 def student_by_id(student_id) hash = client.prepare_exec('SELECT * FROM students WHERE id = ?',student_id).first hash = from_array_to_hash(hash) return nil if hash.nil? Student.from_hash(hash) end |
#student_count ⇒ Object
63 64 65 |
# File 'lib/source/repositories/student_list_db_adapter.rb', line 63 def student_count client.query('SELECT COUNT(id) FROM students').next[0] end |