mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-31 02:59:39 +02:00
Remove object parsing in TimeSeries::Value
We're only every passing hashes
This commit is contained in:
parent
31caf7056d
commit
ee920f359c
3 changed files with 10 additions and 25 deletions
|
@ -49,8 +49,8 @@ class TimeSeries
|
|||
private
|
||||
def initialize_values(data)
|
||||
[ nil, *data ].each_cons(2).map do |previous, current|
|
||||
TimeSeries::Value.new current,
|
||||
previous: (TimeSeries::Value.new(previous) if previous),
|
||||
TimeSeries::Value.new **current,
|
||||
previous_value: previous.try(:[], :value),
|
||||
series: self
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,10 +7,9 @@ class TimeSeries::Value
|
|||
validates :date, presence: true
|
||||
validate :value_must_be_of_known_type
|
||||
|
||||
def initialize(obj, series: nil, previous: nil)
|
||||
@date, @value, @original = parse_object obj
|
||||
@series = series
|
||||
@trend = create_trend previous
|
||||
def initialize(date:, value:, original: nil, series: nil, previous_value: nil)
|
||||
@date, @value, @original, @series = date, value, original, series
|
||||
@trend = create_trend previous_value
|
||||
|
||||
validate!
|
||||
end
|
||||
|
@ -32,24 +31,10 @@ class TimeSeries::Value
|
|||
private
|
||||
attr_reader :series
|
||||
|
||||
def parse_object(obj)
|
||||
if obj.is_a?(Hash)
|
||||
date = obj[:date]
|
||||
value = obj[:value]
|
||||
original = obj.fetch(:original, obj)
|
||||
else
|
||||
date = obj.date
|
||||
value = obj.value
|
||||
original = obj
|
||||
end
|
||||
|
||||
[ date, value, original ]
|
||||
end
|
||||
|
||||
def create_trend(previous)
|
||||
def create_trend(previous_value)
|
||||
TimeSeries::Trend.new \
|
||||
current: value,
|
||||
previous: previous&.value,
|
||||
previous: previous_value,
|
||||
series: series
|
||||
end
|
||||
|
||||
|
|
|
@ -73,9 +73,9 @@ class FamilyTest < ActiveSupport::TestCase
|
|||
assert_equal @expected_snapshots.count, net_worth_series.values.count
|
||||
|
||||
@expected_snapshots.each_with_index do |row, index|
|
||||
expected_assets = TimeSeries::Value.new({ date: row["date"], value: Money.new(row["assets"].to_d) })
|
||||
expected_liabilities = TimeSeries::Value.new({ date: row["date"], value: Money.new(row["liabilities"].to_d) })
|
||||
expected_net_worth = TimeSeries::Value.new({ date: row["date"], value: Money.new(row["net_worth"].to_d) })
|
||||
expected_assets = TimeSeries::Value.new(date: row["date"], value: Money.new(row["assets"].to_d))
|
||||
expected_liabilities = TimeSeries::Value.new(date: row["date"], value: Money.new(row["liabilities"].to_d))
|
||||
expected_net_worth = TimeSeries::Value.new(date: row["date"], value: Money.new(row["net_worth"].to_d))
|
||||
|
||||
assert_in_delta expected_assets.value.amount, Money.new(asset_series.values[index].value).amount, 0.01
|
||||
assert_in_delta expected_liabilities.value.amount, Money.new(liability_series.values[index].value).amount, 0.01
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue