class ROI::GrossProfit

Attributes

end_date[R]
rentabilities[R]
start_date[R]

Public Class Methods

new(rentabilities, start_date, end_date) click to toggle source
# File lib/roi/gross_profit.rb, line 11
def initialize(rentabilities, start_date, end_date)
  @rentabilities = rentabilities
  @start_date = start_date
  @end_date = end_date
end

Public Instance Methods

current_month() click to toggle source
# File lib/roi/gross_profit.rb, line 17
def current_month
  scope_start = WorkDay.next_after(end_date.beginning_of_month, 0)
  format_gross_profit(scope_start, end_date)
end
current_year() click to toggle source
# File lib/roi/gross_profit.rb, line 27
def current_year
  scope_start = WorkDay.next_after(end_date.beginning_of_year, 0)
  format_gross_profit(scope_start, end_date)
end
months(amount) click to toggle source
# File lib/roi/gross_profit.rb, line 22
def months(amount)
  scope_start = (end_date - amount.months + 1.month).beginning_of_month
  check_gaps_and_format(scope_start, end_date)
end
portfolio() click to toggle source
# File lib/roi/gross_profit.rb, line 38
def portfolio
  format_gross_profit(start_date, end_date)
end
years_ago(amount) click to toggle source
# File lib/roi/gross_profit.rb, line 32
def years_ago(amount)
  scope_end = (end_date - amount.year).end_of_year
  scope_start = scope_end.beginning_of_year
  format_gross_profit(scope_start, scope_end)
end

Private Instance Methods

check_gaps_and_format(scope_start, scope_end) click to toggle source
# File lib/roi/gross_profit.rb, line 59
def check_gaps_and_format(scope_start, scope_end)
  return unless scope_start >= start_date

  scoped_rentabilities(scope_start, scope_end).each do |line|
    return nil if line.try(:have_position) == false
  end
  format_gross_profit(scope_start, scope_end)
end
format_gross_profit(scope_start, scope_end) click to toggle source
# File lib/roi/gross_profit.rb, line 50
def format_gross_profit(scope_start, scope_end)
  return unless scope_start && scope_end

  total = scoped_rentabilities(scope_start, scope_end).sum do |line|
    line.try(:gross_profit) || 0
  end.round(2)
  total.zero? ? nil : total
end
scoped_rentabilities(scope_start, scope_end) click to toggle source
# File lib/roi/gross_profit.rb, line 44
def scoped_rentabilities(scope_start, scope_end)
  rentabilities.values.select.each do |line|
    line.reference_date.between?(scope_start, scope_end)
  end
end