mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-03 20:45:21 +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
|
private
|
||||||
def initialize_values(data)
|
def initialize_values(data)
|
||||||
[ nil, *data ].each_cons(2).map do |previous, current|
|
[ nil, *data ].each_cons(2).map do |previous, current|
|
||||||
TimeSeries::Value.new current,
|
TimeSeries::Value.new **current,
|
||||||
previous: (TimeSeries::Value.new(previous) if previous),
|
previous_value: previous.try(:[], :value),
|
||||||
series: self
|
series: self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,10 +7,9 @@ class TimeSeries::Value
|
||||||
validates :date, presence: true
|
validates :date, presence: true
|
||||||
validate :value_must_be_of_known_type
|
validate :value_must_be_of_known_type
|
||||||
|
|
||||||
def initialize(obj, series: nil, previous: nil)
|
def initialize(date:, value:, original: nil, series: nil, previous_value: nil)
|
||||||
@date, @value, @original = parse_object obj
|
@date, @value, @original, @series = date, value, original, series
|
||||||
@series = series
|
@trend = create_trend previous_value
|
||||||
@trend = create_trend previous
|
|
||||||
|
|
||||||
validate!
|
validate!
|
||||||
end
|
end
|
||||||
|
@ -32,24 +31,10 @@ class TimeSeries::Value
|
||||||
private
|
private
|
||||||
attr_reader :series
|
attr_reader :series
|
||||||
|
|
||||||
def parse_object(obj)
|
def create_trend(previous_value)
|
||||||
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)
|
|
||||||
TimeSeries::Trend.new \
|
TimeSeries::Trend.new \
|
||||||
current: value,
|
current: value,
|
||||||
previous: previous&.value,
|
previous: previous_value,
|
||||||
series: series
|
series: series
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -73,9 +73,9 @@ class FamilyTest < ActiveSupport::TestCase
|
||||||
assert_equal @expected_snapshots.count, net_worth_series.values.count
|
assert_equal @expected_snapshots.count, net_worth_series.values.count
|
||||||
|
|
||||||
@expected_snapshots.each_with_index do |row, index|
|
@expected_snapshots.each_with_index do |row, index|
|
||||||
expected_assets = TimeSeries::Value.new({ date: row["date"], value: Money.new(row["assets"].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_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_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_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
|
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