class Depralyzer::Generator

Constants

DEPRECATION_FORMAT
ONE_FIX

Attributes

input[RW]

Public Class Methods

new(input) click to toggle source
# File lib/depralyzer/generator.rb, line 14
def initialize(input)
  @input = File.read(input)
end

Public Instance Methods

deannotate(details) click to toggle source
# File lib/depralyzer/generator.rb, line 72
def deannotate(details)
  unless details.keys[0]
    return [nil, {}]
  end
  guess, _    = details.keys[0].split('. (', 2)
  new_details = {}
  details.each do |key, value|
    if key.start_with?(guess)
      _, rest                 = key.split('. (', 2)
      new_details["(#{rest}"] = value
    else
      return [nil, details]
    end
  end
  [guess, new_details]
end
fix_time(details) click to toggle source
# File lib/depralyzer/generator.rb, line 46
def fix_time(details)
  seconds_to_string(details * ONE_FIX)
end
occurrence(summary) click to toggle source
# File lib/depralyzer/generator.rb, line 89
def occurrence(summary)
  i = 0
  summary.each do |_, detail|
    i += detail.size
  end
  i
end
pluralize(number) click to toggle source
# File lib/depralyzer/generator.rb, line 67
def pluralize number
  return "s" unless number == 1
  ""
end
pretty_output(summary) click to toggle source
# File lib/depralyzer/generator.rb, line 24
def pretty_output(summary)
  puts "Total of #{summary.size} deprecation types with #{occurrence(summary)} occurrences"
  m1 = summary.sort_by { |_, value| -value.values.inject(0) { |sum, x| sum + x } }.to_h
  m1.each do |top, details|
    puts
    puts "### #{top.gsub('_', '\_')} (#{details.size}) - _est: #{fix_time(details.size)}_"

    annotation, new_details = deannotate(details)

    if annotation
      puts "##### #{annotation.gsub('_', '\_')}"
    end

    m = new_details.sort_by { |_, value| -value }.to_h
    m.each do |note, cnt|
      puts sprintf("- %10d %s", cnt, note.gsub('_', '\_'))
    end
  end
end
process() click to toggle source
# File lib/depralyzer/generator.rb, line 18
def process
  warnings = input.scan(DEPRECATION_FORMAT)
  summary  = summarize warnings
  pretty_output summary
end
seconds_to_string(s) click to toggle source
# File lib/depralyzer/generator.rb, line 50
def seconds_to_string(s)

  # d = days, h = hours, m = minutes, s = seconds
  m = (s / 60).floor
  s = s % 60
  h = (m / 60).floor
  m = m % 60
  d = (h / 24).floor
  h = h % 24

  output = "#{m} minute#{pluralize(m)}" if (m > 0)
  output = "#{h} hour#{pluralize(h)}, #{m} minute#{pluralize(m)}" if (h > 0)
  output = "#{d} day#{pluralize(d)}, #{h} hour#{pluralize(h)}, #{m} minute#{pluralize(m)}" if (d > 0)

  output.gsub(', 0 minutes','').gsub(', 0 hours','')
end
summarize(warnings) click to toggle source
# File lib/depralyzer/generator.rb, line 97
def summarize(warnings)
  summary = {}
  warnings.each do |warning|
    content                = warning.sub('DEPRECATION WARNING: ', '')
    type, details          = content.split('. ', 2)
    summary[type]          = {} unless summary.key?(type)
    summary[type][details] = 0 unless summary[type].key?(details)
    summary[type][details] += 1
  end
  summary
end