class Diecut::ReportFormatter

Adopted gratefully from Xavier Shay’s Cane

Attributes

reports[R]

Public Class Methods

new(reports) click to toggle source
# File lib/diecut/report.rb, line 7
def initialize(reports)
  @reports = reports
end

Public Instance Methods

context(renderer) click to toggle source
# File lib/diecut/report.rb, line 57
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[:name] = report[:name].rjust(report_name_width)
    report[:status_color] =
      case report[:status]
      when /ok/i
        good_color
      when /fail/i
        bad_color
      else
        warn_color
      end
  end
  context
end
fail_count() click to toggle source
# File lib/diecut/report.rb, line 45
def fail_count
  reports.inject(0){|sum, report| sum + (report.passed ? 0 : 1)}
end
passed?() click to toggle source
# File lib/diecut/report.rb, line 41
def passed?
  fail_count == 0
end
rejection_fields() click to toggle source
# File lib/diecut/report.rb, line 12
def rejection_fields
  %i(file_and_line label value)
end
report_name_width() click to toggle source
# File lib/diecut/report.rb, line 49
def report_name_width
  @report_name_width ||= reports.map(&:name).map(&:size).max
end
sized_name(name) click to toggle source
# File lib/diecut/report.rb, line 53
def sized_name(name)
  array.take(widths.length).zip(widths).map{|item, width| { it: item.to_s.ljust(width)}}
end
template() click to toggle source
# 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) click to toggle source
# File lib/diecut/report.rb, line 34
def to_s(widths=nil)
  renderer = Mustache.new

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