2024-02-20 09:07:55 -05:00
|
|
|
class Trend
|
2024-03-01 17:17:34 -05:00
|
|
|
attr_reader :current, :previous, :type
|
2024-02-20 09:07:55 -05:00
|
|
|
|
2024-03-06 09:56:59 -05:00
|
|
|
def initialize(current: nil, previous: nil, type: "asset")
|
2024-03-01 17:17:34 -05:00
|
|
|
@current = current
|
|
|
|
@previous = previous
|
2024-03-04 08:31:22 -05:00
|
|
|
@type = type # asset means positive trend is good, liability means negative trend is good
|
2024-03-01 17:17:34 -05:00
|
|
|
end
|
2024-02-20 09:07:55 -05:00
|
|
|
|
2024-03-01 17:17:34 -05:00
|
|
|
def direction
|
2024-03-06 09:56:59 -05:00
|
|
|
return "flat" if @current == @previous
|
|
|
|
return "up" if @previous.nil? || (@current && @current > @previous)
|
|
|
|
"down"
|
2024-03-01 17:17:34 -05:00
|
|
|
end
|
2024-02-20 09:07:55 -05:00
|
|
|
|
2024-03-01 17:17:34 -05:00
|
|
|
def amount
|
|
|
|
return 0 if @previous.nil?
|
|
|
|
@current - @previous
|
|
|
|
end
|
2024-02-20 09:07:55 -05:00
|
|
|
|
2024-03-01 17:17:34 -05:00
|
|
|
def percent
|
|
|
|
return 0 if @previous.nil?
|
|
|
|
return Float::INFINITY if @previous == 0
|
2024-03-06 09:56:59 -05:00
|
|
|
((@current - @previous).abs / @previous.abs.to_f * 100).round(1)
|
2024-03-01 17:17:34 -05:00
|
|
|
end
|
2024-02-20 09:07:55 -05:00
|
|
|
end
|