Class: LoanCreator::Timetable

Inherits:
Object
  • Object
show all
Defined in:
lib/loan_creator/timetable.rb

Constant Summary collapse

PERIODS =

Used to calculate next term’s date (see ActiveSupport#advance)

{
  month:    {months: 1},
  quarter:  {months: 3},
  semester: {months: 6},
  year:     {years: 1}
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(starts_on:, period:, interests_start_date: nil, starting_index: 1) ⇒ Timetable

Returns a new instance of Timetable.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/loan_creator/timetable.rb', line 14

def initialize(starts_on:, period:, interests_start_date: nil, starting_index: 1)
  raise ArgumentError.new(:period) unless PERIODS.keys.include?(period)

  @terms          = []
  @starts_on      = (starts_on.is_a?(Date) ? starts_on : Date.parse(starts_on))
  @period         = period
  @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

Instance Attribute Details

#periodObject (readonly)

, :interests_start_date



12
13
14
# File 'lib/loan_creator/timetable.rb', line 12

def period
  @period
end

#starting_indexObject (readonly)

, :interests_start_date



12
13
14
# File 'lib/loan_creator/timetable.rb', line 12

def starting_index
  @starting_index
end

#starts_onObject (readonly)

, :interests_start_date



12
13
14
# File 'lib/loan_creator/timetable.rb', line 12

def starts_on
  @starts_on
end

#termsObject (readonly)

, :interests_start_date



12
13
14
# File 'lib/loan_creator/timetable.rb', line 12

def terms
  @terms
end

Instance Method Details

#<<(term) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
# File 'lib/loan_creator/timetable.rb', line 27

def <<(term)
  raise ArgumentError.new('LoanCreator::Term expected') unless term.is_a?(LoanCreator::Term)
  term.index  ||= autoincrement_index
  term.due_on ||= date_for(term.index)
  @terms << term
  self
end

#term(index) ⇒ Object



42
43
44
# File 'lib/loan_creator/timetable.rb', line 42

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

#to_csv(header: true) ⇒ Object



35
36
37
38
39
40
# File 'lib/loan_creator/timetable.rb', line 35

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