Class: Podrpt::ReportGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/podrpt/report_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(pods_analysis, options) ⇒ ReportGenerator

Returns a new instance of ReportGenerator.



3
4
5
6
# File 'lib/podrpt/report_generator.rb', line 3

def initialize(pods_analysis, options)
  @pods = pods_analysis
  @options = options
end

Instance Method Details

#build_report_textObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/podrpt/report_generator.rb', line 8

def build_report_text
  outdated_count = @pods.count { |p| is_outdated?(p.current_version, p.latest_version) }
  total_pods_in_report = @options.total_pods_count 
  header = "_Generated: #{Time.now.utc.iso8601}_\n" \
           "_Outdated: #{outdated_count}/#{total_pods_in_report}_"
  
  h_pod, h_ver, h_risk, h_owners = "Pod", "Versions", "Risk", "Owners"
  pod_names = @pods.map(&:name); versions = @pods.map { |p| versions_cell(p) }; risks = @pods.map { |p| risk_cell(p) }; owners = @pods.map { |p| p.owners.empty? ? "" : p.owners.join(', ') }
  w_pod = [h_pod.length, pod_names.map(&:length).max || 0].max; w_ver = [h_ver.length, versions.map(&:length).max || 0].max; w_risk = [h_risk.length, risks.map(&:length).max || 0].max; w_owners = [h_owners.length, owners.map(&:length).max || 0].max
  
  row_formatter = ->(c1, c2, c3, c4) { "| #{c1.ljust(w_pod)} | #{c2.ljust(w_ver)} | #{c3.rjust(w_risk)} | #{c4.ljust(w_owners)} |" }
  
  lines = [header, ""]; lines << row_formatter.call(h_pod, h_ver, h_risk, h_owners)
  sep_pod = ":-" + ("-" * (w_pod - 1)); sep_ver = ":-" + ("-" * (w_ver - 1)); sep_risk = ("-" * (w_risk - 1)) + ":"; sep_owners = ":-" + ("-" * (w_owners - 1))
  lines << "|#{sep_pod}|#{sep_ver}|#{sep_risk}|#{sep_owners}|"
  @pods.each_with_index { |_, i| lines << row_formatter.call(pod_names[i], versions[i], risks[i], owners[i]) }
  
  lines.join("\n")
end