Class: LoanCreator::Timetable

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Timetable.



8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#loanObject (readonly)

, :interests_start_date



4
5
6
# File 'lib/loan_creator/timetable.rb', line 4

def loan
  @loan
end

#starting_indexObject (readonly)

, :interests_start_date



4
5
6
# File 'lib/loan_creator/timetable.rb', line 4

def starting_index
  @starting_index
end

#termsObject (readonly)

, :interests_start_date



4
5
6
# File 'lib/loan_creator/timetable.rb', line 4

def terms
  @terms
end

Instance Method Details

#<<(term) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
# 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

#current_indexObject



44
45
46
# File 'lib/loan_creator/timetable.rb', line 44

def current_index
  @current_index.nil? ? @starting_index - 1 : @current_index
end

#next_indexObject



40
41
42
# File 'lib/loan_creator/timetable.rb', line 40

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

#term(index) ⇒ Object



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

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

#to_csv(header: true) ⇒ Object



29
30
31
32
33
34
# 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