Class: StudentInputFormController

Inherits:
Object
  • Object
show all
Defined in:
lib/source/student_input_form_controller.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent_controller) ⇒ StudentInputFormController

Returns a new instance of StudentInputFormController.



6
7
8
9
# File 'lib/source/student_input_form_controller.rb', line 6

def initialize(parent_controller)
  @parent_controller = parent_controller
  LoggerHolder.instance.debug('StudentInputFormController: initialized')
end

Instance Method Details

#on_db_conn_error(error) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/source/student_input_form_controller.rb', line 49

def on_db_conn_error(error)
  LoggerHolder.instance.debug('StudentInputFormController: DB connection error:')
  LoggerHolder.instance.error(error.message)
  api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
  api.call(0, "No connection to DB", "Error", 0)
  @view.close
end

#on_view_createdObject



20
21
22
23
24
25
26
# File 'lib/source/student_input_form_controller.rb', line 20

def on_view_created
  begin
    @student_list = StudentListAdv.new(StudentsListDBAdapter.new(StudentListDB.instance))
  rescue Mysql2::Error::ConnectionError=>e
    on_db_conn_error(e)
  end
end

#process_fields(fields) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/source/student_input_form_controller.rb', line 28

def process_fields(fields)
  begin
    last_name = fields.delete(:last_name)
    first_name = fields.delete(:first_name)
    parental_name = fields.delete(:parental_name)

    return if last_name.nil? || first_name.nil? || parental_name.nil?

    student = Student.new(last_name, first_name, parental_name, **fields)

    @student_list.add_student(student)
    LoggerHolder.instance.debug('StudentInputFormController: adding student to DB')
    @view.close
  rescue ArgumentError => e
    api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
    LoggerHolder.instance.debug("StudentInputFormController: wrong fields: #{e.message}")
    api.call(0, e.message, 'Error', 0)
  end
end

#refreshObject



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

def refresh
  @parent_controller.refresh_data(1,20)
end

#set_view(view) ⇒ Object



11
12
13
# File 'lib/source/student_input_form_controller.rb', line 11

def set_view(view)
  @view = view
end