Class: Tcelfer::CLI::Report

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

Overview

Generate reports with day/week/month/year granularity.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReport

Returns a new instance of Report.



25
26
27
# File 'lib/tcelfer/cli/report.rb', line 25

def initialize
  @store = Tcelfer::Storage.new
end

Class Method Details

.legendTerminal::Table

Formats Tcelfer::RATING_TO_COLOR_MAP into a pretty legend Terminal::Table

Returns:

  • (Terminal::Table)


59
60
61
62
63
64
# File 'lib/tcelfer/cli/report.rb', line 59

def self.legend
  leg = Tcelfer::RATING_TO_COLOR_MAP.map do |rating, color|
    [Paint['    ', :inverse, color], rating]
  end
  Terminal::Table.new(rows: leg, headings: %w[Color Rating].map { |head| Paint[head, :bold] })
end

Instance Method Details

#generate_month_report(month, year = Date.today.year) ⇒ Terminal::Table

Takes a month and year and returns a pretty formatted Terminal::Table with days colored according to Tcelfer::RATING_TO_COLOR_MAP

Parameters:

  • month (Integer)
  • year (Integer) (defaults to: Date.today.year)

Returns:

  • (Terminal::Table)


34
35
36
37
38
39
40
41
42
43
# File 'lib/tcelfer/cli/report.rb', line 34

def generate_month_report(month, year = Date.today.year)
  mon_by_wday = @store.by_month(month, year).group_by { |day| day.date.wday }
  if mon_by_wday.length < 7
    raise ReportError, "Unable to process #{Date::MONTHNAMES[month]}, please have at least 7 days of data first"
  end

  pad_month_start!(mon_by_wday)
  weeks = color_month(mon_by_wday)
  Terminal::Table.new(rows: weeks, style: { all_separators: true }, headings: Date::ABBR_DAYNAMES)
end

#month_with_legend(month, year) ⇒ Terminal::Table

Same as generate_month_report except it includes a color legend

Parameters:

  • month (Integer)
  • year (Integer)

Returns:

  • (Terminal::Table)


49
50
51
52
53
54
55
# File 'lib/tcelfer/cli/report.rb', line 49

def month_with_legend(month, year)
  tab = generate_month_report(month, year)
  Terminal::Table.new(rows: [[tab, Report.legend]]).tap do |combined|
    combined.style    = { border_x: '', border_y: '', border_i: '', width: `tput cols`.to_i }
    combined.headings = ["== #{Date::MONTHNAMES[month]} - #{year} ==", '== Legend ==']
  end
end