class LoanCreator::Timetable

Attributes

loan[R]
starting_index[R]
terms[R]

Public Class Methods

new(loan:, interests_start_date: nil, starting_index: 1) click to toggle source
# File lib/loan_creator/timetable.rb, line 8
def initialize(loan:, interests_start_date: nil, starting_index: 1)
  @terms          = []
  @loan           = loan
  @starting_index = starting_index

  if interests_start_date
    @interests_start_date = (interests_start_date.is_a?(Date) ? interests_start_date : Date.parse(interests_start_date))
  end
end

Public Instance Methods

<<(term) click to toggle source
# File lib/loan_creator/timetable.rb, line 18
def <<(term)
  raise ArgumentError.new('LoanCreator::Term expected') unless term.is_a?(LoanCreator::Term)

  @current_index = term.index || next_index

  term.index    = @current_index
  term.due_on ||= loan.timetable_term_dates[term.index]
  @terms << term
  self
end
next_index() click to toggle source
# File lib/loan_creator/timetable.rb, line 40
def next_index
  @current_index.nil? ? @starting_index : @current_index + 1
end
term(index) click to toggle source
# File lib/loan_creator/timetable.rb, line 36
def term(index)
  @terms.find { |term| term.index == index }
end
to_csv(header: true) click to toggle source
# File lib/loan_creator/timetable.rb, line 29
def to_csv(header: true)
  output = []
  output << terms.first.to_h.keys.join(',') if header
  terms.each { |t| output << t.to_csv }
  output
end