Class: LoanCreator::Timetable
- Inherits:
-
Object
- Object
- LoanCreator::Timetable
- Defined in:
- lib/loan_creator/timetable.rb
Instance Attribute Summary collapse
-
#loan ⇒ Object
readonly
, :interests_start_date.
-
#starting_index ⇒ Object
readonly
, :interests_start_date.
-
#terms ⇒ Object
readonly
, :interests_start_date.
Instance Method Summary collapse
- #<<(term) ⇒ Object
- #current_index ⇒ Object
-
#initialize(loan:, interests_start_date: nil, starting_index: 1) ⇒ Timetable
constructor
A new instance of Timetable.
- #next_index ⇒ Object
- #term(index) ⇒ Object
- #to_csv(header: true) ⇒ Object
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
#loan ⇒ Object (readonly)
, :interests_start_date
4 5 6 |
# File 'lib/loan_creator/timetable.rb', line 4 def loan @loan end |
#starting_index ⇒ Object (readonly)
, :interests_start_date
4 5 6 |
# File 'lib/loan_creator/timetable.rb', line 4 def starting_index @starting_index end |
#terms ⇒ Object (readonly)
, :interests_start_date
4 5 6 |
# File 'lib/loan_creator/timetable.rb', line 4 def terms @terms end |
Instance Method Details
#<<(term) ⇒ Object
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.[term.index] @terms << term self end |
#current_index ⇒ Object
44 45 46 |
# File 'lib/loan_creator/timetable.rb', line 44 def current_index @current_index.nil? ? @starting_index - 1 : @current_index end |
#next_index ⇒ Object
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 |