lib/roi/rentability.rb in roi_calculator-1.0.0 vs lib/roi/rentability.rb in roi_calculator-2.0.0
- old
+ new
@@ -1,12 +1,14 @@
-require "memoist"
-require "roi/financial_result"
-require "roi/rentability_periods"
-require "math_util"
-require "active_support"
-require "active_support/core_ext"
+# frozen_string_literal: true
+require 'memoist'
+require 'roi/financial_result'
+require 'roi/rentability_periods'
+require 'math_util'
+require 'active_support'
+require 'active_support/core_ext'
+
module ROI
class Rentability
extend Memoist
include RentabilityPeriods
attr_accessor :rentabilities
@@ -18,18 +20,18 @@
@end_date = options[:end_date] || Date.today - 1
@skip_on_gap = false
post_initialize(options)
end
- def post_initialize(options)
- end
+ def post_initialize(options); end
def calculate
parse_rentabilities
- if @rentabilities.count > 0
+ if @rentabilities.count.positive?
dates = @rentabilities.keys
- @start_date, @end_date = [dates[0], dates[-1]]
+ @start_date = dates[0]
+ @end_date = dates[-1]
end
self
end
def financial
@@ -49,45 +51,47 @@
end
filtered.map(&:rentability).compact
end
def volatility(months_ago = nil)
- _start = months_ago ?
- (end_date - months_ago.months + 1.month).beginning_of_month : nil
- (volatility_in_window(nil, _start).first || [])[1]
+ if months_ago
+ start = (end_date - months_ago.months + 1.month).beginning_of_month
+ end
+ (volatility_in_window(nil, start).first || [])[1]
end
def volatility_in_window(days = nil, start_date = nil)
filtered = rentabilities_array(start_date)
days ||= filtered.size
0.upto(filtered.size - days).map do |low|
series = filtered.slice(low, days + 1)
- vol = MathUtil.volatility(series)
- date = dates_with_position.fetch(low + days) { dates_with_position.last }
- [date, vol] if date
+ date =
+ dates_with_position.fetch(low + days) { dates_with_position.last }
+ [date, MathUtil.volatility(series)] if date
end
end
def to_a
dates_with_position.map do |date|
if @rentabilities[date]
- [date.strftime("%Y-%m-%d"), @rentabilities[date].share - 1]
+ [date.strftime('%Y-%m-%d'), @rentabilities[date].quota - 1]
end
end.compact
end
protected
- def before_parse_rentabilities
- end
+ def before_parse_rentabilities; end
+
def work_days
start_date && end_date ? WorkDay.between(start_date, end_date) : []
end
memoize :work_days
private
+
def parse_rentabilities
@rentabilities = {}
before_parse_rentabilities
work_days.each_cons(2) do |last_date, date|
parse(date, last_date, @rentabilities)
@@ -97,13 +101,13 @@
def parse(this_date, last_date, rentabilities)
this_value = rentabilities_source(this_date)
last_value = rentabilities_source(last_date)
return rentabilities unless this_value && last_value
- last_share = rentabilities[last_date].try(:share) || 1
+ last_quota = rentabilities[last_date].try(:quota) || 1
rentability = (this_value / last_value - 1).round(8)
- new_share = (last_share * (1 + rentability)).round(8)
- line = RentabilityLine.new(this_date, rentability, nil, new_share, true)
+ new_quota = (last_quota * (1 + rentability)).round(8)
+ line = RentabilityLine.new(this_date, rentability, new_quota, true)
after_parse(this_date, last_date, rentabilities, line)
end
end
end