Class: Diecut::ReportFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/diecut/report.rb

Overview

Adopted gratefully from Xavier Shay’s Cane

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reports) ⇒ ReportFormatter

Returns a new instance of ReportFormatter.



7
8
9
# File 'lib/diecut/report.rb', line 7

def initialize(reports)
  @reports = reports
end

Instance Attribute Details

#reportsObject (readonly)

Returns the value of attribute reports.



10
11
12
# File 'lib/diecut/report.rb', line 10

def reports
  @reports
end

Instance Method Details

#context(renderer) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/diecut/report.rb', line 48

def context(renderer)
  bad_color = proc{|text,render| Paint[renderer.render(text), :red]}
  good_color =  proc{|text,render| Paint[renderer.render(text), :green]}
  warn_color = proc{|text,render| Paint[renderer.render(text), :yellow]}

  context = {
    reports: reports.map(&:context),
    passed: passed?,
    total_items: reports.inject(0){|sum, report| sum + report.length},
    total_fails: fail_count,
    status_color: passed? ? good_color : bad_color
  }
  context[:reports].each do |report|
    report[:status_color] =
      case report[:status]
      when /ok/i
        good_color
      when /fail/i
        bad_color
      else
        warn_color
      end
  end
  context
end

#fail_countObject



44
45
46
# File 'lib/diecut/report.rb', line 44

def fail_count
  reports.inject(0){|sum, report| sum + (report.passed ? 0 : 1)}
end

#passed?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/diecut/report.rb', line 40

def passed?
  fail_count == 0
end

#rejection_fieldsObject



12
13
14
# File 'lib/diecut/report.rb', line 12

def rejection_fields
  %i(file_and_line label value)
end

#templateObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/diecut/report.rb', line 16

def template
  (<<-EOT).gsub(/^      /, '')
  {{#reports}}{{#status_color}}{{name}}: {{status}} {{#length}} {{length}}{{/length}}
  {{/status_color}}
  {{#summary}}{{summary}}
  {{/summary}}{{^empty  }}{{#headers}}{{it}}    {{/headers}}
  {{/empty  }}{{#rejects}} {{#reject }}{{it}}    {{/reject}}
  {{/rejects}}{{#advice}}
  {{advice}}
  {{/advice}}
  {{/reports}}
  {{#status_color}}Total QA report items: {{total_items}}
  Total QA failing reports: {{total_fails}}
  {{/status_color}}
  EOT
end

#to_s(widths = nil) ⇒ Object



33
34
35
36
37
38
# File 'lib/diecut/report.rb', line 33

def to_s(widths=nil)
  renderer = Mustache.new

  # require 'pp'; puts "\n#{__FILE__}:#{__LINE__} => {context(renderer).pretty_inspect}"
  renderer.render(template, context(renderer))
end