mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-08 06:55:21 +02:00
Materializers
This commit is contained in:
parent
e75dbc978b
commit
3bfd41eba9
4 changed files with 16 additions and 14 deletions
|
@ -1,4 +1,4 @@
|
|||
class Balance::Syncer
|
||||
class Balance::Materializer
|
||||
attr_reader :account, :strategy
|
||||
|
||||
def initialize(account, strategy:)
|
||||
|
@ -6,9 +6,9 @@ class Balance::Syncer
|
|||
@strategy = strategy
|
||||
end
|
||||
|
||||
def sync_balances
|
||||
def materialize_balances
|
||||
Balance.transaction do
|
||||
sync_holdings
|
||||
materialize_holdings
|
||||
calculate_balances
|
||||
|
||||
Rails.logger.info("Persisting #{@balances.size} balances")
|
||||
|
@ -23,8 +23,8 @@ class Balance::Syncer
|
|||
end
|
||||
|
||||
private
|
||||
def sync_holdings
|
||||
@holdings = Holding::Syncer.new(account, strategy: strategy).sync_holdings
|
||||
def materialize_holdings
|
||||
@holdings = Holding::Materializer.new(account, strategy: strategy).materialize_holdings
|
||||
end
|
||||
|
||||
def update_account_info
|
|
@ -1,10 +1,12 @@
|
|||
class Holding::Syncer
|
||||
# "Materializes" holdings (similar to a DB materialized view, but done at the app level)
|
||||
# into a series of records we can easily query and join with other data.
|
||||
class Holding::Materializer
|
||||
def initialize(account, strategy:)
|
||||
@account = account
|
||||
@strategy = strategy
|
||||
end
|
||||
|
||||
def sync_holdings
|
||||
def materialize_holdings
|
||||
calculate_holdings
|
||||
|
||||
Rails.logger.info("Persisting #{@holdings.size} holdings")
|
|
@ -1,6 +1,6 @@
|
|||
require "test_helper"
|
||||
|
||||
class Balance::SyncerTest < ActiveSupport::TestCase
|
||||
class Balance::MaterializerTest < ActiveSupport::TestCase
|
||||
include EntriesTestHelper
|
||||
|
||||
setup do
|
||||
|
@ -14,7 +14,7 @@ class Balance::SyncerTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
test "syncs balances" do
|
||||
Holding::Syncer.any_instance.expects(:sync_holdings).returns([]).once
|
||||
Holding::Materializer.any_instance.expects(:materialize_holdings).returns([]).once
|
||||
|
||||
@account.expects(:start_date).returns(2.days.ago.to_date)
|
||||
|
||||
|
@ -26,7 +26,7 @@ class Balance::SyncerTest < ActiveSupport::TestCase
|
|||
)
|
||||
|
||||
assert_difference "@account.balances.count", 2 do
|
||||
Balance::Syncer.new(@account, strategy: :forward).sync_balances
|
||||
Balance::Materializer.new(@account, strategy: :forward).materialize_balances
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -45,7 +45,7 @@ class Balance::SyncerTest < ActiveSupport::TestCase
|
|||
)
|
||||
|
||||
assert_difference "@account.balances.count", 3 do
|
||||
Balance::Syncer.new(@account, strategy: :forward).sync_balances
|
||||
Balance::Materializer.new(@account, strategy: :forward).materialize_balances
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,6 +1,6 @@
|
|||
require "test_helper"
|
||||
|
||||
class Holding::SyncerTest < ActiveSupport::TestCase
|
||||
class Holding::MaterializerTest < ActiveSupport::TestCase
|
||||
include EntriesTestHelper
|
||||
|
||||
setup do
|
||||
|
@ -14,7 +14,7 @@ class Holding::SyncerTest < ActiveSupport::TestCase
|
|||
|
||||
# Should have yesterday's and today's holdings
|
||||
assert_difference "@account.holdings.count", 2 do
|
||||
Holding::Syncer.new(@account, strategy: :forward).sync_holdings
|
||||
Holding::Materializer.new(@account, strategy: :forward).materialize_holdings
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -23,7 +23,7 @@ class Holding::SyncerTest < ActiveSupport::TestCase
|
|||
Holding.create!(account: @account, security: @aapl, qty: 1, price: 100, amount: 100, currency: "USD", date: Date.current)
|
||||
|
||||
assert_difference "Holding.count", -1 do
|
||||
Holding::Syncer.new(@account, strategy: :forward).sync_holdings
|
||||
Holding::Materializer.new(@account, strategy: :forward).materialize_holdings
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue