class QuickExam::Export

Constants

RUN_ARGS

Attributes

analyzer[R]
dest[R]
file[R]
object_qna[R]
records[R]

Public Class Methods

run(file_path, options={}) click to toggle source
# File lib/quick_exam/export.rb, line 10
def run(file_path, options={})
  @file = File.new(file_path)
  arg = validate_arguments!(options)
  count = arg[:count].__presence || 2

  check_path(arg[:dest], file_path)
  proccess_analyze(file_path, arg)
  @f_ques = question_mark(arg[:f_ques])
  @records = mixes(count, arg)
  process_export_files
end

Private Class Methods

alphabets() click to toggle source
# File lib/quick_exam/export.rb, line 111
def alphabets
  @alphabets ||= ('A'..'Z').to_a
end
answers(data) click to toggle source
# File lib/quick_exam/export.rb, line 102
def answers(data)
  str_answer = ''
  data.each_with_index do |str, i|
    str = "#{alphabets[i]}. #{str}"
    str_answer += analyzer.html? ? "<p>#{str}</p>" : "#{str}\n"
  end
  str_answer
end
check_path(dest_path, file_path) click to toggle source
# File lib/quick_exam/export.rb, line 40
def check_path(dest_path, file_path)
  if dest_path.__blank?
    @dest = File.dirname(file_path) + "/#{FOLDER_NAME_EXPORT}/"
  else
    @dest = dest_path + "/#{FOLDER_NAME_EXPORT}/"
  end

  return raise ErrorExport.new('No such file') unless File.exist?(file_path)
  FileUtils.mkdir_p(dest) unless Dir.exist? dest
end
export_answer_sheet(path_filename) click to toggle source
# File lib/quick_exam/export.rb, line 75
def export_answer_sheet(path_filename)
  File.open(path_filename, 'w') do |f|
    object_qna.each_with_index do |ticket, i|
      ans = ticket.correct_indexes.map { |ci| alphabets[ci] }.join(', ')
      str = "#{@f_ques}#{i + 1}. #{ans}"
      if analyzer.html?
        f.write("<p>#{str}</p>")
      else
        f.write(str)
        f.write("\n")
      end
    end
  end
end
export_question_sheet(path_filename) click to toggle source
# File lib/quick_exam/export.rb, line 65
def export_question_sheet(path_filename)
  File.open(path_filename, 'w') do |f|
    object_qna.each_with_index do |ticket, i|
      f.write question(ticket.question, i + 1)
      f.write answers(ticket.answers)
      analyzer.html? ? f.write("<br />") : f.write("\n")
    end
  end
end
mixes(count, arg) click to toggle source
# File lib/quick_exam/export.rb, line 31
def mixes(count, arg)
  analyzer.records.mixes(
    count,
    shuffle_question: arg[:shuffle_question],
    shuffle_answer: arg[:shuffle_answer],
    same_answer: arg[:same_answer]
  )
end
path_filename(index_order, extra_name: '') click to toggle source
# File lib/quick_exam/export.rb, line 90
def path_filename(index_order, extra_name: '')
  basename = File.basename(file.path, '.*')
  basename = ("#{basename}_%.3i" % index_order) + extra_name
  extname = analyzer.docx? ? '.docx.txt' : File.extname(file.path)
  "#{dest}#{basename}#{extname}"
end
proccess_analyze(file_path, arg) click to toggle source
# File lib/quick_exam/export.rb, line 26
def proccess_analyze(file_path, arg)
  @analyzer = QuickExam::Analyzer.new(file_path, f_ques: arg[:f_ques] , f_corr: arg[:f_corr])
  analyzer.analyze
end
process_export_files() click to toggle source
# File lib/quick_exam/export.rb, line 51
def process_export_files
  path_dest_files = []
  records.each_with_index do |object_qna, i|
    @object_qna = object_qna
    i = i + 1
    file_path_question = path_filename(i)
    file_path_answer = path_filename(i, extra_name: '_answers')
    export_question_sheet(file_path_question)
    export_answer_sheet(file_path_answer)
    path_dest_files << file_path_question << file_path_answer
  end
  path_dest_files
end
question(str, index) click to toggle source
# File lib/quick_exam/export.rb, line 97
def question(str, index)
  str = "#{@f_ques}#{index}. #{str}"
  analyzer.html? ? "<p>#{str}</p>" : "#{str}\n"
end
validate_arguments!(args) click to toggle source
# File lib/quick_exam/export.rb, line 115
def validate_arguments!(args)
  invalid_arg = args.detect{ |arg, _| !RUN_ARGS.include?(arg.to_s) }
  raise ArgumentError.new("unknow keyword #{invalid_arg[0]}") if invalid_arg.__present?
  RUN_ARGS.each_with_object({}) do |arg, memo|
    arg = arg.to_sym
    memo[arg] = args[arg]
  end
end