Class: Amortizy::CsvFormatter

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

Constant Summary collapse

HEADERS =
[
  'Payment Number', 'Date', 'Days in Period', 'Principal Payment',
  'Interest Payment', 'Additional Fee Payment', 'Total Payment',
  'Principal Balance Remaining', 'Accrued Interest', 'Total Balance',
  'Payment Type', 'Grace Interest Capitalized'
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(schedule_obj) ⇒ CsvFormatter

Returns a new instance of CsvFormatter.



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

def initialize(schedule_obj)
  @data = schedule_obj.schedule
end

Instance Method Details

#render(csv_path) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/amortizy/csv_formatter.rb', line 18

def render(csv_path)
  CSV.open(csv_path, 'w') do |csv|
    csv << HEADERS

    @data.each do |row|
      csv << [
        row[:payment_number],
        row[:date].strftime('%Y-%m-%d'),
        row[:days_in_period],
        format('%.2f', row[:principal_payment] || 0),
        format('%.2f', row[:interest_payment] || 0),
        format('%.2f', row[:additional_fee_payment] || 0),
        format('%.2f', row[:total_payment] || 0),
        format('%.2f', row[:principal_balance]),
        format('%.2f', row[:accrued_interest] || 0),
        format('%.2f', row[:total_balance]),
        row[:payment_type],
        row[:grace_interest_capitalized] ? format('%.2f', row[:grace_interest_capitalized]) : ''
      ]
    end
  end

  puts "CSV file generated: #{csv_path}"
end