Sha256: 3d53fc74013cf56c3a059c20cf7045bec5eca0aae1f8c18fd614f8e0f850a3b4
Contents?: true
Size: 1.61 KB
Versions: 1
Compression:
Stored size: 1.61 KB
Contents
# coding: utf-8 module LoanCreator class Timetable # Used to calculate next term's date (see ActiveSupport#advance) PERIODS = { month: { months: 1 }, quarter: { months: 3 }, semester: { months: 6 }, year: { years: 1 } } attr_reader :terms, :starts_on, :period def initialize(starts_on:, period:) @terms = [] @starts_on = (Date === starts_on ? starts_on : Date.parse(starts_on)) raise ArgumentError.new(:period) unless PERIODS.keys.include?(period) @period = period end def <<(term) raise ArgumentError.new('LoanCreator::Term expected') unless LoanCreator::Term === term term.index = autoincrement_index term.due_on = autoincrement_date @terms << term self end def reset_indexes_and_due_on_dates @autoincrement_index = 0 @autoincrement_date = @starts_on @terms.each do |term| term[:index] = autoincrement_index term[:due_on] = autoincrement_date end 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 private # First term index of a timetable term is 1 def autoincrement_index @autoincrement_index ||= 0 @autoincrement_index += 1 end # First term due_on date of timetable term is the starts_on given date def autoincrement_date @autoincrement_date ||= @starts_on date = @autoincrement_date @autoincrement_date = @autoincrement_date.advance(PERIODS.fetch(@period)) date end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
loan_creator-0.2.3 | lib/loan_creator/timetable.rb |