Sha256: 483c785347191f09f5e9f6a9cc7cd6829a44a16e18f8c8a462920fcd24f1d4ff
Contents?: true
Size: 1.59 KB
Versions: 2
Compression:
Stored size: 1.59 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_at, :period def initialize(starts_at:, period:) @terms = [] @starts_at = (Date === starts_at ? starts_at : Date.parse(starts_at)) 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.date = autoincrement_date @terms << term self end def reset_indexes_and_dates @autoincrement_index = 0 @autoincrement_date = @starts_at @terms.each do |term| term[:index] = autoincrement_index term[:date] = 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 date of timetable term is the starts_at given date def autoincrement_date @autoincrement_date ||= @starts_at date = @autoincrement_date @autoincrement_date = @autoincrement_date.advance(PERIODS.fetch(@period)) date end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
loan_creator-0.2.0 | lib/loan_creator/timetable.rb |
loan_creator-0.2.1 | lib/loan_creator/timetable.rb |