1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-23 15:19:38 +02:00
Maybe/app/models/trend.rb

27 lines
582 B
Ruby
Raw Normal View History

class Trend
attr_reader :current, :previous
def initialize(current, previous)
@current = current
@previous = previous
end
def direction
return "flat" unless @previous
return "up" if @current > @previous
return "down" if @current < @previous
"flat"
end
def amount
return 0 if @previous.nil?
@current - @previous
end
def percent
return 0 if @previous.nil?
return Float::INFINITY if @previous == 0
((@current - @previous).abs / @previous.to_f * 100).round(1)
end
end