Class: BlendSpreadsheetLoanGenerator::Loan

Inherits:
Object
  • Object
show all
Defined in:
lib/blend_spreadsheet_loan_generator/loan.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount:, duration:, period_duration:, rate:, due_on:, deferred_and_capitalized:, deferred:, type:, interests_type:, starting_capitalized_interests:, fees_rate:, starting_capitalized_fees:) ⇒ Loan

Returns a new instance of Loan.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 16

def initialize(
  amount:,
  duration:,
  period_duration:,
  rate:,
  due_on:,
  deferred_and_capitalized:,
  deferred:,
  type:,
  interests_type:,
  starting_capitalized_interests:,
  fees_rate:,
  starting_capitalized_fees:)
  @amount = amount.to_f
  @duration = duration.to_i
  @period_duration = period_duration.to_i
  @rate = rate.to_f
  @due_on = due_on.is_a?(Date) ? due_on : Date.parse(due_on)
  @deferred_and_capitalized = deferred_and_capitalized.to_i
  @deferred = deferred.to_i
  @type = type
  @interests_type = interests_type
  @fees_rate = fees_rate.to_f
  @starting_capitalized_interests = starting_capitalized_interests.to_f
  @starting_capitalized_fees = starting_capitalized_fees.to_f

  print_validation_errors
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



3
4
5
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 3

def amount
  @amount
end

#deferredObject

Returns the value of attribute deferred.



3
4
5
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 3

def deferred
  @deferred
end

#deferred_and_capitalizedObject

Returns the value of attribute deferred_and_capitalized.



3
4
5
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 3

def deferred_and_capitalized
  @deferred_and_capitalized
end

#due_onObject

Returns the value of attribute due_on.



3
4
5
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 3

def due_on
  @due_on
end

#durationObject

Returns the value of attribute duration.



3
4
5
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 3

def duration
  @duration
end

#fees_rateObject

Returns the value of attribute fees_rate.



3
4
5
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 3

def fees_rate
  @fees_rate
end

#interests_typeObject

Returns the value of attribute interests_type.



3
4
5
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 3

def interests_type
  @interests_type
end

#period_durationObject

Returns the value of attribute period_duration.



3
4
5
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 3

def period_duration
  @period_duration
end

#rateObject

Returns the value of attribute rate.



3
4
5
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 3

def rate
  @rate
end

#starting_capitalized_feesObject

Returns the value of attribute starting_capitalized_fees.



3
4
5
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 3

def starting_capitalized_fees
  @starting_capitalized_fees
end

#starting_capitalized_interestsObject

Returns the value of attribute starting_capitalized_interests.



3
4
5
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 3

def starting_capitalized_interests
  @starting_capitalized_interests
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 3

def type
  @type
end

Instance Method Details

#bullet?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 111

def bullet?
  fully_deferred? && deferred_and_capitalized == total_deferred_duration
end

#bullet_term?(term) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 115

def bullet_term?(term)
  deferred_and_capitalized >= term || bullet? # for last term
end

#fully_deferred?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 107

def fully_deferred?
  duration > 1 && non_deferred_duration == 1
end

#in_fine?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 119

def in_fine?
  fully_deferred? && deferred == total_deferred_duration
end

#interests_formulaObject



57
58
59
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 57

def interests_formula
  "BlendSpreadsheetLoanGenerator::#{interests_type.classify}Interests".constantize.new(loan: self)
end

#loan_type_formulaObject



53
54
55
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 53

def loan_type_formula
  "BlendSpreadsheetLoanGenerator::#{type.classify}".constantize.new(loan: self)
end

#nameObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 91

def name
  args = []
  args += ['realistic'] if interests_type == 'realistic'
  args += [
    name_type,
    name_period_duration,
    amount,
    (rate * 100).to_s,
    duration.to_s,
    name_deferred,
    name_due_on
  ]

  args.join('_')
end

#name_deferredObject



81
82
83
84
85
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 81

def name_deferred
  return '0' if fully_deferred?

  total_deferred_duration
end

#name_due_onObject



87
88
89
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 87

def name_due_on
  due_on.strftime('%Y%m%d')
end

#name_period_durationObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 68

def name_period_duration
  if period_duration.in?([1, 3, 6, 12])
    {
      '1' => 'month',
      '3' => 'quarter',
      '6' => 'semester',
      '12' => 'year'
    }[period_duration.to_s]
  else
    period_duration.to_s
  end
end

#name_typeObject



61
62
63
64
65
66
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 61

def name_type
  return 'bullet' if bullet?
  return 'in_fine' if in_fine?

  type
end

#non_deferred_durationObject



123
124
125
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 123

def non_deferred_duration
  duration - total_deferred_duration
end


45
46
47
48
49
50
51
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 45

def print_validation_errors
  puts 'amount < 0' if amount < 0
  puts 'deferred & deferred_and_capitalized >= duration' if total_deferred_duration >= duration
  if type == 'standard' && interests_type == 'realistic' && !fully_deferred?
    puts 'standard & realistic interests do not work together'
  end
end

#total_deferred_durationObject



127
128
129
# File 'lib/blend_spreadsheet_loan_generator/loan.rb', line 127

def total_deferred_duration
  deferred_and_capitalized + deferred
end