Class: QuickPep

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

Overview

Quick Personal Expenses Planner - for people too lazy to use a

spreadsheet or sfinance app

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s, balance: 0, currency: '', today: Date.today, debug: false) ⇒ QuickPep

Returns a new instance of QuickPep.



16
17
18
19
20
21
22
23
24
25
# File 'lib/quickpep.rb', line 16

def initialize(s, balance: 0, currency: '', today: Date.today, debug: false)

  @balance, @currency, @debug = balance, currency, debug
  @today = today
  @warnings = []
  @to_s = calc_expenses(s)

  warnings() if @warnings.any?

end

Instance Attribute Details

#to_dxObject (readonly)

Returns the value of attribute to_dx.



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

def to_dx
  @to_dx
end

#to_sObject (readonly)

Returns the value of attribute to_s.



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

def to_s
  @to_s
end

Instance Method Details

#annual_costs(perx = :year, per: perx) ⇒ Object

options: year, month, weel, day



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/quickpep.rb', line 29

def annual_costs(perx=:year, per: perx)

  rows = @date_events.map do |date, title|

    amount = @h[title].amount

    prefix = amount[0] == '+' ? '' : '-'
    amountx = (prefix + amount.gsub(/\D/,'')).to_f

    [date, title, amountx]

  end

  a = rows.group_by {|date,title, amount| title }\
      .map {|key, rows| [key, rows.map(&:last).sum]}.sort_by(&:last)

  a.map do |title, total|

    amount = case per.to_sym
    when :year
      total
    when :month
      total / (12 - @today.month)
    when :week
      total / (52 - @today.cweek )
    when :day
      total / (365 - @today.yday)
    end

    [title, amount.round(2)]
  end

end

#to_html(title: "Personal Budget #{@today.year}") ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/quickpep.rb', line 63

def to_html(title: "Personal Budget #{@today.year}")

  t = to_dx().to_table
  t.labels =  %w(date title debit: credit: balance: uid:)
  table = t.display markdown: true

  "<html>
    <head>
      <meta name='viewport' content='width=device-width, initial-scale=1'>
      <title>#{title}</title>
      <style>
        table {border-collapse: collapse; width: 700px}
        tr:nth-child(even) {
          background-color: #aca;
        }
        @media print {

          tr:nth-child(even) {
            background-color: #ccc;
          }

        }
      </style>
    </head>
    <body>
      <h1>#{title}</h1>
      #{RDiscount.new(table).to_html}
      <hr/>
      <footer>Generated by QuickPEP</footer>
    </body>
  </html>"
end

#warningsObject



96
97
98
# File 'lib/quickpep.rb', line 96

def warnings()
  @warnings.each {|warning| puts warning.warn }
end