1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-23 23:29:39 +02:00
Maybe/app/models/time_series/value.rb
Zach Gollwitzer f904d9d062
Add the ability to "rollup" values in a time series (#554)
* Clean up time series models

* Add value group rollup class for summarizing hierarchical data

* Integrate new classes

* Update UI to use new patterns

* Update D3 charts to expect new data format

* Clean up account model

* More cleanup

* Money improvements

* Use new money fields

* Remove invalid fixture data to avoid orphaned accountables

* Update time series to work better with collections

* Fix tests and UI bugs
2024-03-19 09:10:40 -04:00

32 lines
779 B
Ruby

class TimeSeries::Value
include Comparable
attr_accessor :trend
attr_reader :value, :date, :original
def initialize(obj)
@original = obj[:original] || obj
if obj.is_a?(Hash)
@date = obj[:date]
@value = obj[:value]
else
@date = obj.date
@value = obj.value
end
validate_input
end
def <=>(other)
result = date <=> other.date
result = value <=> other.value if result == 0
result
end
private
def validate_input
raise ArgumentError, "Date is required" unless @date.is_a?(Date)
raise ArgumentError, "Money or Numeric value is required" unless @value.is_a?(Money) || @value.is_a?(Numeric)
end
end