Class: ColdStorage::Report

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

Overview

Human readable status of every archivable model: what its rule is, how many rows are waiting, how many are already archived, when it last ran.

Constant Summary collapse

HEADERS =
['Model', 'Rule', 'Pending', 'Archived', 'Last run'].freeze

Instance Method Summary collapse

Constructor Details

#initialize(models: nil, counts: true) ⇒ Report

Returns a new instance of Report.



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

def initialize(models: nil, counts: true)
  @models = models || ColdStorage.models
  @counts = counts
end

Instance Method Details

#rowsObject



14
15
16
# File 'lib/cold_storage/report.rb', line 14

def rows
  @rows ||= @models.map { |model| row_for(model) }
end

#to_sObject



18
19
20
21
22
23
24
25
26
# File 'lib/cold_storage/report.rb', line 18

def to_s
  table = [HEADERS, *rows]
  widths = HEADERS.each_index.map { |i| table.map { |row| row[i].to_s.length }.max }
  separator = widths.map { |width| '-' * width }.join('-+-')

  lines = table.map { |row| row.each_with_index.map { |cell, i| cell.to_s.ljust(widths[i]) }.join(' | ') }
  lines.insert(1, separator)
  lines.join("\n")
end