Class: RubyTestStudentGrader::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_test_student_grader/reporter.rb

Constant Summary collapse

TEMPLATE =
File.join(File.dirname(__FILE__), "template.html.erb")

Instance Method Summary collapse

Constructor Details

#initialize(total_score, max_score, failed_tests, feedback, **opts) ⇒ Reporter

total_score: Numeric max_score: Numeric failed_tests: Array feedback: String opts: { course_name?: String }



20
21
22
23
24
25
26
27
28
# File 'lib/ruby_test_student_grader/reporter.rb', line 20

def initialize(total_score, max_score, failed_tests, feedback, **opts)
  @total_score = total_score
  @max_score = max_score
  @failed_tests = failed_tests || []
  @feedback = feedback.to_s
  @timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
  @course_name = opts[:course_name] || ENV["COURSE_NAME"] || "Curso"
  @feedback_class = feedback_class
end

Instance Method Details

#feedback_classObject



90
91
92
93
94
95
96
# File 'lib/ruby_test_student_grader/reporter.rb', line 90

def feedback_class
  case @total_score
  when 5.5..6.0 then "success"
  when 4.0..5.4 then "warning"
  else "danger"
  end
end

#generate(output_path = "test_report.html") ⇒ Object

Conserva compatibilidad: genera HTML por defecto



31
32
33
# File 'lib/ruby_test_student_grader/reporter.rb', line 31

def generate(output_path = "test_report.html")
  generate_html(output_path)
end

#generate_csv(output_path = "test_report.csv") ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ruby_test_student_grader/reporter.rb', line 43

def generate_csv(output_path = "test_report.csv")
  data = {
    course: @course_name,
    total_score: @total_score,
    max_score: @max_score,
    percentage: (@max_score.to_f.zero? ? 0.0 : (@total_score.to_f / @max_score * 100)).round(2),
    failed_tests_count: @failed_tests.size,
    failed_tests: @failed_tests.join("; "),
    feedback: @feedback,
    timestamp: @timestamp
  }

  CSV.open(output_path, "w") do |csv|
    csv << data.keys
    csv << data.values
  end
  puts "✅ CSV report generated at: #{output_path}"
  output_path
end

#generate_html(output_path = "test_report.html") ⇒ Object



35
36
37
38
39
40
41
# File 'lib/ruby_test_student_grader/reporter.rb', line 35

def generate_html(output_path = "test_report.html")
  template = File.read(TEMPLATE)
  html = ERB.new(template).result(binding)
  File.write(output_path, html)
  puts "✅ HTML report generated at: #{output_path}"
  output_path
end

#generate_pdf(output_path = "test_report.pdf") ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ruby_test_student_grader/reporter.rb', line 63

def generate_pdf(output_path = "test_report.pdf")
  unless defined?(Prawn)
    warn "Prawn is not available. Make sure the dependency is installed."
    return nil
  end

  Prawn::Document.generate(output_path) do |pdf|
    pdf.text "Reporte de Tests - #{@course_name}", size: 22, style: :bold, align: :center
    pdf.move_down 20
    pdf.text "Puntaje: #{@total_score} / #{@max_score}", size: 16
    percentage = @max_score.to_f.zero? ? 0.0 : (@total_score.to_f / @max_score * 100)
    pdf.text "Porcentaje: #{percentage.round(2)}%", size: 12
    pdf.move_down 10
    pdf.text "Feedback:", style: :bold
    pdf.text @feedback.to_s
    unless @failed_tests.empty?
      pdf.move_down 10
      pdf.text "Tests Fallidos:", style: :bold
      @failed_tests.each { |t| pdf.text "#{t}" }
    end
    pdf.move_down 20
    pdf.text "Generado el: #{@timestamp}", size: 10, align: :right
  end
  puts "✅ PDF report generated at: #{output_path}"
  output_path
end