Class: Amortizy::AmortizationSchedule
- Inherits:
-
Object
- Object
- Amortizy::AmortizationSchedule
- Defined in:
- lib/amortizy/engine.rb
Instance Attribute Summary collapse
-
#additional_fee_label ⇒ Object
readonly
Returns the value of attribute additional_fee_label.
-
#annual_rate ⇒ Object
readonly
Returns the value of attribute annual_rate.
-
#frequency ⇒ Object
readonly
Returns the value of attribute frequency.
-
#principal ⇒ Object
readonly
Returns the value of attribute principal.
-
#start_date ⇒ Object
readonly
Returns the value of attribute start_date.
-
#term_months ⇒ Object
readonly
Returns the value of attribute term_months.
Instance Method Summary collapse
- #end_date ⇒ Object
- #generate(output: :console, csv_path: nil) ⇒ Object
-
#initialize(start_date:, principal:, annual_rate:, frequency:, term_months: nil, num_payments: nil, origination_fee: 0, additional_fee: 0, additional_fee_label: 'Additional Fee', additional_fee_treatment: :distributed, bank_days_only: false, interest_only_periods: 0, grace_period_days: 0, interest_method: :simple) ⇒ AmortizationSchedule
constructor
A new instance of AmortizationSchedule.
- #payment_amount ⇒ Object
- #schedule ⇒ Object
- #summary ⇒ Object
- #total_interest ⇒ Object
- #total_paid ⇒ Object
Constructor Details
#initialize(start_date:, principal:, annual_rate:, frequency:, term_months: nil, num_payments: nil, origination_fee: 0, additional_fee: 0, additional_fee_label: 'Additional Fee', additional_fee_treatment: :distributed, bank_days_only: false, interest_only_periods: 0, grace_period_days: 0, interest_method: :simple) ⇒ AmortizationSchedule
Returns a new instance of AmortizationSchedule.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/amortizy/engine.rb', line 21 def initialize(start_date:, principal:, annual_rate:, frequency:, term_months: nil, num_payments: nil, origination_fee: 0, additional_fee: 0, additional_fee_label: 'Additional Fee', additional_fee_treatment: :distributed, bank_days_only: false, interest_only_periods: 0, grace_period_days: 0, interest_method: :simple) @start_date = Date.parse(start_date.to_s) @principal = principal.to_f @annual_rate = annual_rate.to_f / 100.0 @frequency = frequency.to_sym @origination_fee = origination_fee.to_f @additional_fee = additional_fee.to_f @additional_fee_label = additional_fee_label.to_s @additional_fee_treatment = additional_fee_treatment.to_sym @bank_days_only = bank_days_only @interest_only_periods = interest_only_periods.to_i @grace_period_days = grace_period_days.to_i @interest_method = interest_method.to_sym validate_principal! validate_frequency! validate_term_input!(term_months, num_payments) validate_fee_treatment! validate_interest_only_periods! validate_interest_method! end |
Instance Attribute Details
#additional_fee_label ⇒ Object (readonly)
Returns the value of attribute additional_fee_label.
19 20 21 |
# File 'lib/amortizy/engine.rb', line 19 def additional_fee_label @additional_fee_label end |
#annual_rate ⇒ Object (readonly)
Returns the value of attribute annual_rate.
19 20 21 |
# File 'lib/amortizy/engine.rb', line 19 def annual_rate @annual_rate end |
#frequency ⇒ Object (readonly)
Returns the value of attribute frequency.
19 20 21 |
# File 'lib/amortizy/engine.rb', line 19 def frequency @frequency end |
#principal ⇒ Object (readonly)
Returns the value of attribute principal.
19 20 21 |
# File 'lib/amortizy/engine.rb', line 19 def principal @principal end |
#start_date ⇒ Object (readonly)
Returns the value of attribute start_date.
19 20 21 |
# File 'lib/amortizy/engine.rb', line 19 def start_date @start_date end |
#term_months ⇒ Object (readonly)
Returns the value of attribute term_months.
19 20 21 |
# File 'lib/amortizy/engine.rb', line 19 def term_months @term_months end |
Instance Method Details
#end_date ⇒ Object
78 79 80 |
# File 'lib/amortizy/engine.rb', line 78 def end_date schedule.last[:date] end |
#generate(output: :console, csv_path: nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/amortizy/engine.rb', line 46 def generate(output: :console, csv_path: nil) case output when :console ConsoleFormatter.new(self).render when :csv raise ArgumentError, 'csv_path required for CSV output' unless csv_path CsvFormatter.new(self).render(csv_path) else raise ArgumentError, 'Output must be :console or :csv' end end |
#payment_amount ⇒ Object
90 91 92 |
# File 'lib/amortizy/engine.rb', line 90 def payment_amount calculate_payment end |
#schedule ⇒ Object
59 60 61 |
# File 'lib/amortizy/engine.rb', line 59 def schedule @schedule ||= generate_schedule_data.each(&:freeze).freeze end |
#summary ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/amortizy/engine.rb', line 63 def summary schedule { start_date: @start_date, end_date: end_date, principal: @principal, total_payments: total_payments, frequency: @frequency, annual_rate: @annual_rate * 100.0, payment_amount: payment_amount, total_interest: total_interest, total_paid: total_paid } end |
#total_interest ⇒ Object
82 83 84 |
# File 'lib/amortizy/engine.rb', line 82 def total_interest schedule.sum { |row| row[:interest_payment] || 0 } end |
#total_paid ⇒ Object
86 87 88 |
# File 'lib/amortizy/engine.rb', line 86 def total_paid schedule.sum { |row| row[:total_payment] || 0 } end |