Sha256: 82531e91eaa190cc4c360b22eb5dee8f5cf1265278e46f5ba28e626b849a2557

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

# coding: utf-8
module LoanCreator
  class Timetable
    attr_reader :loan, :terms, :starting_index #, :interests_start_date

    delegate :starts_on, :period, to: :loan

    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

    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

    def to_csv(header: true)
      output = []
      output << terms.first.to_h.keys.join(',') if header
      terms.each { |t| output << t.to_csv }
      output
    end

    def term(index)
      @terms.find { |term| term.index == index }
    end

    def next_index
      @current_index.nil? ? @starting_index : @current_index + 1
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
loan_creator-0.10.0 lib/loan_creator/timetable.rb
loan_creator-0.9.1 lib/loan_creator/timetable.rb
loan_creator-0.9.0 lib/loan_creator/timetable.rb
loan_creator-0.8.2 lib/loan_creator/timetable.rb
loan_creator-0.8.1 lib/loan_creator/timetable.rb