Class: Interests::Calculator

Inherits:
Object
  • Object
show all
Defined in:
lib/interests/calculator.rb

Class Method Summary collapse

Class Method Details

.config_rates(scope = :default) ⇒ Object



9
10
11
# File 'lib/interests/calculator.rb', line 9

def config_rates(scope = :default)
  Interests::Config.send(scope).map{ |cfg| OpenStruct.new(date: Date.parse(cfg[:date]), rate: cfg[:rate]) }
end

.interests(amount = 0.0, from_date = BEGINNING_OF_COMPUTER_ERA, to_date = Date.today) ⇒ Object



27
28
29
30
31
# File 'lib/interests/calculator.rb', line 27

def interests(amount = 0.0, from_date = BEGINNING_OF_COMPUTER_ERA, to_date = Date.today)
  rates(to_date).select{ |rate| rate.to_date >= from_date && rate.from_date <= to_date }.map do |rate|
    interest_struct([from_date + 1.day, rate.from_date].max, [to_date, rate.to_date].min, amount, rate)
  end
end

.rates(till = Date.today) ⇒ Object



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

def rates(till = Date.today)
  @rates = []
  return @rates if config_rates.none? || !starts_in_computers_era?

  prev_rate = nil
  config_rates.sort!{ |x, y| x.date <=> y.date }
  ([blank_rate] + config_rates + [blank_rate(till + 1.day, config_rates.last.rate)]).each_with_index do |rate, i|
    add_to_rates(prev_rate, rate, ) if i > 0
    prev_rate = rate
  end

  @rates
end

.total_interests(amount = 0.0, from_date = BEGINNING_OF_COMPUTER_ERA, to_date = Date.today) ⇒ Object



33
34
35
# File 'lib/interests/calculator.rb', line 33

def total_interests(amount = 0.0, from_date = BEGINNING_OF_COMPUTER_ERA, to_date = Date.today)
  interests(amount, from_date, to_date).map(&:interests).inject(0, :+)
end