lib/teuton/report/report.rb in teuton-2.3.11 vs lib/teuton/report/report.rb in teuton-2.4.0

- old
+ new

@@ -1,36 +1,16 @@ -# frozen_string_literal: true - -require "terminal-table" require_relative "../application" -require_relative "formatter/formatter_factory" -require_relative "show" -require_relative "close" +require_relative "formatter/formatter" -## -# This class maintain the results of every case, in a structured way. -# * report/show.rb -# * report/close.rb class Report - # @!attribute id - # @return [Integer] It is the [Case] number. Zero indicates Resume Report. attr_accessor :id, :filename, :output_dir - # @!attribute head - # @return [Hash] Report head information. attr_accessor :head - # @!attribute lines - # @return [Array] Report body information. attr_accessor :lines - # @!attribute tail - # @return [Hash] Report tail information. attr_accessor :tail - # @!attribute format - # @return [Symbol] Indicate export format. - attr_reader :format + attr_accessor :format attr_reader :history - ## - # Class constructor + def initialize(id = "00") @id = id @filename = "case-#{@id}" @output_dir = Application.instance.output_basedir @head = {} @@ -40,32 +20,69 @@ # For example: "..F." means: good, good, fail and good # I will use this in the future stats manager. @history = "" end - ## - # Export [Case] data to specified format. - # @param format [Symbol] Select export format. Default value is :txt. - def export(format = :txt) - @format = format - filepath = File.join(@output_dir, @filename + "." \ - + FormatterFactory.ext(@format)) + def clone + report = Report.new + attrs = %i[id filename output_dir head lines tail format] + attrs.each do |attr| + attr_set = "#{attr}=".to_sym + report.send(attr_set, send(attr).clone) + end - formatter = FormatterFactory.get(self, @format, filepath) - formatter.process + report end - ## - # Export resumed data from all Cases, to specified format. - # @param format [Symbol] Select export format. Default value is :txt. - def export_resume(format = :txt) + def export(options) + filepath = File.join(@output_dir, @filename) + Formatter.call(self, options, filepath) + end + + def export_resume(options) + format = options[:format] @format = "resume_#{format}".to_sym - filepath = File.join(@output_dir, @filename + "." \ - + FormatterFactory.ext(@format)) - formatter = FormatterFactory.get(self, @format, filepath) - formatter.process + options[:format] = @format + filepath = File.join(@output_dir, @filename) + Formatter.call(self, options, filepath) - filepath = File.join(@output_dir, "moodle.csv") - formatter = FormatterFactory.get(self, :moodle_csv, filepath) - formatter.process + filepath = File.join(@output_dir, "moodle") + Formatter.call(self, {format: :moodle_csv}, filepath) + end + + ## + # Calculate final values: + # * grade + # * max_weight + # * good_weight,d + # * fail_weight + # * fail_counter + def close + app = Application.instance + max = 0.0 + good = 0.0 + fail = 0.0 + fail_counter = 0 + @lines.each do |i| + next unless i.instance_of? Hash + + max += i[:weight] if i[:weight].positive? + if i[:check] + good += i[:weight] + @history += app.letter[:good] + else + fail += i[:weight] + fail_counter += 1 + @history += app.letter[:bad] + end + end + @tail[:max_weight] = max + @tail[:good_weight] = good + @tail[:fail_weight] = fail + @tail[:fail_counter] = fail_counter + + i = good.to_f / max + i = 0 if i.nan? + @tail[:grade] = (100.0 * i).round + @tail[:grade] = 0 if @tail[:unique_fault].positive? end end