class Transpec::Report

Attributes

conversion_errors[R]
file_errors[R]
records[R]

Public Class Methods

new() click to toggle source
# File lib/transpec/report.rb, line 9
def initialize
  @records = []
  @conversion_errors = []
  @file_errors = []
end

Public Instance Methods

<<(other) click to toggle source
# File lib/transpec/report.rb, line 15
def <<(other)
  records.concat(other.records)
  conversion_errors.concat(other.conversion_errors)
  file_errors.concat(other.file_errors)
  self
end
colored_stats() click to toggle source
# File lib/transpec/report.rb, line 53
def colored_stats
  base_color = if !conversion_errors.empty?
                 :magenta
               elsif annotation_count.nonzero?
                 :yellow
               else
                 :green
               end

  conversion_incomplete_warning_stats(base_color) + error_stats(base_color)
end
colored_summary(options = nil) click to toggle source
# File lib/transpec/report.rb, line 36
def colored_summary(options = nil)
  options ||= { bullet: nil, separate_by_blank_line: false }

  summary = ''

  unique_record_counts.each do |record, count|
    summary << "\n" if options[:separate_by_blank_line] && !summary.empty?
    summary << format_record(record, count, options[:bullet])
  end

  summary
end
stats() click to toggle source
# File lib/transpec/report.rb, line 65
def stats
  without_color { colored_stats }
end
summary(options = nil) click to toggle source
# File lib/transpec/report.rb, line 49
def summary(options = nil)
  without_color { colored_summary(options) }
end
unique_record_counts() click to toggle source
# File lib/transpec/report.rb, line 22
def unique_record_counts
  record_counts = Hash.new(0)

  records.each do |record|
    record_counts[record] += 1
  end

  sorted_record_counts = record_counts.sort_by do |record, count|
    [-count, record]
  end

  Hash[sorted_record_counts]
end

Private Instance Methods

annotation_count() click to toggle source
# File lib/transpec/report.rb, line 140
def annotation_count
  records.count(&:annotation)
end
colorize(string, *args) click to toggle source
# File lib/transpec/report.rb, line 75
def colorize(string, *args)
  rainbow.wrap(string).color(*args)
end
conversion_incomplete_warning_stats(color) click to toggle source
# File lib/transpec/report.rb, line 108
def conversion_incomplete_warning_stats(color)
  text = pluralize(records.count, 'conversion') + ', '
  text << pluralize(conversion_errors.count, 'incomplete') + ', '
  text << pluralize(annotation_count, 'warning') + ', '
  colorize(text, color)
end
error_stats(base_color) click to toggle source
# File lib/transpec/report.rb, line 115
def error_stats(base_color)
  color = if file_errors.empty?
            base_color
          else
            :red
          end

  colorize(pluralize(file_errors.count, 'error'), color)
end
format_record(record, count, bullet = nil) click to toggle source
# File lib/transpec/report.rb, line 87
def format_record(record, count, bullet = nil)
  entry_prefix = bullet ? "#{bullet} " : ''
  text = entry_prefix + colorize(pluralize(count, record.type), :cyan) + "\n"

  justification = entry_prefix.length + 6

  case record.type
  when :conversion
    text << labeled_line('from', record.old_syntax, justification)
    text << labeled_line('to',   record.new_syntax, justification)
  when :addition
    text << labeled_line('of',   record.new_syntax, justification)
  when :removal
    text << labeled_line('of',   record.old_syntax, justification)
  end
end
labeled_line(label, content, justification) click to toggle source
# File lib/transpec/report.rb, line 104
def labeled_line(label, content, justification)
  colorize("#{label.rjust(justification)}: ", :cyan) + content + "\n"
end
pluralize(number, thing, options = {}) click to toggle source
# File lib/transpec/report.rb, line 125
def pluralize(number, thing, options = {})
  text = ''

  if number.zero? && options[:no_for_zero]
    text = 'no'
  else
    text << number.to_s
  end

  text << " #{thing}"
  text << 's' unless number == 1

  text
end
rainbow() click to toggle source
# File lib/transpec/report.rb, line 71
def rainbow
  @rainbow ||= Rainbow.new
end
without_color() { || ... } click to toggle source
# File lib/transpec/report.rb, line 79
def without_color
  original_coloring_state = rainbow.enabled
  rainbow.enabled = false
  value = yield
  rainbow.enabled = original_coloring_state
  value
end