require 'source/data_construct_pattern/data_construct_pattarn.rb' require 'source/controllers/create_student_controller.rb' require 'source/controllers/edit_student_controller.rb' require 'source/attr_limited_regex_accessor.rb' require 'logger' module ShnaiderCode class ViewController attr_private_accessor :model attr_accessor :data_list attr_accessor :selected_page attr_accessor :on_update_data attr_accessor :on_add_student_clicked attr_accessor :on_edit_student_clicked attr_private_accessor :logger def initialize(model, &on_update_data) self.model = model self.on_update_data = on_update_data self.selected_page = 0 self.data_list = DataListStudentShort.new(list: [], data_constructor: DefaultDataConstructPattern.new()) self.model.get_students(0, 10, self.data_list) self.logger = Logger.new('controller.log') end end # StudentsListViewDelegate class ViewController def pages_count (model.count / 10.0).ceil end def table_columns self.data_list.get_names end def next_page if self.selected_page != self.pages_count - 1 self.selected_page += 1 end puts self.selected_page update_table() end def last_page if self.selected_page != 0 self.selected_page -= 1 end puts self.selected_page update_table() end def update_table begin model.get_students(self.selected_page * 10, (self.selected_page + 1) * 10, self.data_list) rescue logger.error('error in db connection') end self.on_update_data.call() end def open_student_creation_window() self.logger.info('creating student window open') on_add_student_clicked.call() end def open_student_editing_window(id) self.logger.info('editing student window open') on_edit_student_clicked.call(id) end def remove_student(id) self.logger.info("student with id #{id} deleted") model.remove_student(id) update_table() end def add_student(student) self.logger.info("new student added") model.add_student(student) puts model.count end def get_student(id) model.get_student(id) end def replace_student(id, student) self.logger.info("student with id #{id} updated") model.replace_student(id, student) end end end