Class: Course::Assessment::Question::Programming::Java::JavaPackageService

Inherits:
LanguagePackageService
  • Object
show all
Defined in:
app/services/course/assessment/question/programming/java/java_package_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ JavaPackageService

Returns a new instance of JavaPackageService


5
6
7
# File 'app/services/course/assessment/question/programming/java/java_package_service.rb', line 5

def initialize(params)
  @test_params = test_params params if params.present?
end

Instance Method Details

#extract_meta(attachment, template_files) ⇒ Object


42
43
44
45
46
47
48
49
50
# File 'app/services/course/assessment/question/programming/java/java_package_service.rb', line 42

def extract_meta(attachment, template_files)
  return @meta if @meta.present? && attachment == @attachment

  # attachment will be nil if the question is not autograded, in that case the meta data will be
  # generated from the template files in the database.
  return generate_non_autograded_meta(template_files) if attachment.nil?

  extract_autograded_meta(attachment)
end

#generate_package(old_attachment) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/course/assessment/question/programming/java/java_package_service.rb', line 26

def generate_package(old_attachment)
  return nil if @test_params.blank?
  @tmp_dir = Dir.mktmpdir
  @old_meta = old_attachment.present? ? extract_meta(old_attachment, nil) : nil
  data_files_to_keep = old_attachment.present? ? find_files_to_keep('data_files', old_attachment) : []
  submission_files_to_keep = old_attachment.present? ? find_files_to_keep('submission_files', old_attachment) : []
  solution_files_to_keep = old_attachment.present? ? find_files_to_keep('solution_files', old_attachment) : []
  @meta = generate_meta(data_files_to_keep, submission_files_to_keep, solution_files_to_keep)

  return nil if @meta == @old_meta

  @attachment = generate_zip_file(data_files_to_keep, submission_files_to_keep, solution_files_to_keep)
  FileUtils.remove_entry @tmp_dir if @tmp_dir.present?
  @attachment
end

#submission_templatesObject


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/services/course/assessment/question/programming/java/java_package_service.rb', line 9

def submission_templates
  if submit_as_file?
    templates = []
    @test_params[:submission_files].map do |file|
      template_file = { filename: file.original_filename, content: File.read(file.tempfile.path) }
      templates.push(template_file)
    end

    templates
  else
    [
      filename: 'template',
      content: @test_params[:submission] || ''
    ]
  end
end