Class: ShnaiderCode::ViewController

Inherits:
Object
  • Object
show all
Defined in:
lib/source/controllers/view_controller.rb,
lib/source/controllers/view_controller.rb

Overview

StudentsListViewDelegate

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, &on_update_data) ⇒ ViewController

Returns a new instance of ViewController.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/source/controllers/view_controller.rb', line 21

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

Instance Attribute Details

#data_listObject

Returns the value of attribute data_list.



10
11
12
# File 'lib/source/controllers/view_controller.rb', line 10

def data_list
  @data_list
end

#on_add_student_clickedObject

Returns the value of attribute on_add_student_clicked.



16
17
18
# File 'lib/source/controllers/view_controller.rb', line 16

def on_add_student_clicked
  @on_add_student_clicked
end

#on_edit_student_clickedObject

Returns the value of attribute on_edit_student_clicked.



17
18
19
# File 'lib/source/controllers/view_controller.rb', line 17

def on_edit_student_clicked
  @on_edit_student_clicked
end

#on_update_dataObject

Returns the value of attribute on_update_data.



14
15
16
# File 'lib/source/controllers/view_controller.rb', line 14

def on_update_data
  @on_update_data
end

#selected_pageObject

Returns the value of attribute selected_page.



12
13
14
# File 'lib/source/controllers/view_controller.rb', line 12

def selected_page
  @selected_page
end

Instance Method Details

#add_student(student) ⇒ Object



91
92
93
94
95
# File 'lib/source/controllers/view_controller.rb', line 91

def add_student(student)
    self.logger.info("new student added")
    model.add_student(student)
    puts model.count
end

#get_student(id) ⇒ Object



97
98
99
# File 'lib/source/controllers/view_controller.rb', line 97

def get_student(id)
    model.get_student(id)
end

#last_pageObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/source/controllers/view_controller.rb', line 55

def last_page
    if self.selected_page != 0
        self.selected_page -= 1
    end

    puts self.selected_page

    
    update_table()
end

#next_pageObject



45
46
47
48
49
50
51
52
53
# File 'lib/source/controllers/view_controller.rb', line 45

def next_page
    if self.selected_page != self.pages_count - 1
        self.selected_page += 1
    end

    puts self.selected_page

    update_table()
end

#open_student_creation_windowObject



75
76
77
78
# File 'lib/source/controllers/view_controller.rb', line 75

def open_student_creation_window()
    self.logger.info('creating student window open')
    on_add_student_clicked.call()
end

#open_student_editing_window(id) ⇒ Object



80
81
82
83
# File 'lib/source/controllers/view_controller.rb', line 80

def open_student_editing_window(id)
    self.logger.info('editing student window open')
    on_edit_student_clicked.call(id)
end

#pages_countObject



37
38
39
# File 'lib/source/controllers/view_controller.rb', line 37

def pages_count
    (model.count / 10.0).ceil
end

#remove_student(id) ⇒ Object



85
86
87
88
89
# File 'lib/source/controllers/view_controller.rb', line 85

def remove_student(id)
    self.logger.info("student with id #{id} deleted")
    model.remove_student(id)
    update_table()
end

#replace_student(id, student) ⇒ Object



101
102
103
104
# File 'lib/source/controllers/view_controller.rb', line 101

def replace_student(id, student)
    self.logger.info("student with id #{id} updated")
    model.replace_student(id, student)
end

#table_columnsObject



41
42
43
# File 'lib/source/controllers/view_controller.rb', line 41

def table_columns
    self.data_list.get_names
end

#update_tableObject



66
67
68
69
70
71
72
73
# File 'lib/source/controllers/view_controller.rb', line 66

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