Class: Interests::Calculator
- Inherits:
-
Object
- Object
- Interests::Calculator
- Defined in:
- lib/interests/calculator.rb
Class Method Summary collapse
- .config_rates(scope = :default) ⇒ Object
- .interests(amount = 0.0, from_date = BEGINNING_OF_COMPUTER_ERA, to_date = Date.today) ⇒ Object
- .rates(till = Date.today) ⇒ Object
- .total_interests(amount = 0.0, from_date = BEGINNING_OF_COMPUTER_ERA, to_date = Date.today) ⇒ Object
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
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/interests/calculator.rb', line 33 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| date_beg = [from_date + 1.day, rate.from_date].max date_end = [to_date, rate.to_date].min OpenStruct.new( date_beg: date_beg, date_end: date_end, days: (date_beg..date_end).count, day_rate: rate.day_rate, interests: amount * (date_beg..date_end).count * rate.day_rate ) end end |
.rates(till = Date.today) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# 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| if i > 0 from_date = prev_rate.date to_date = rate.date - 1.day days = (to_date - from_date).to_i day_rate = prev_rate.rate.to_f / 365 @rates << OpenStruct.new(from_date: from_date, to_date: to_date, days: days, rate: prev_rate.rate, day_rate: day_rate) end prev_rate = rate end @rates end |
.total_interests(amount = 0.0, from_date = BEGINNING_OF_COMPUTER_ERA, to_date = Date.today) ⇒ Object
47 48 49 |
# File 'lib/interests/calculator.rb', line 47 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 |