From 3bfd41eba9dff830bd7a0e22727fe999e92cffea Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Sat, 17 May 2025 16:14:28 -0400 Subject: [PATCH] Materializers --- app/models/balance/{syncer.rb => materializer.rb} | 10 +++++----- app/models/holding/{syncer.rb => materializer.rb} | 6 ++++-- .../balance/{syncer_test.rb => materializer_test.rb} | 8 ++++---- .../holding/{syncer_test.rb => materializer_test.rb} | 6 +++--- 4 files changed, 16 insertions(+), 14 deletions(-) rename app/models/balance/{syncer.rb => materializer.rb} (89%) rename app/models/holding/{syncer.rb => materializer.rb} (87%) rename test/models/balance/{syncer_test.rb => materializer_test.rb} (82%) rename test/models/holding/{syncer_test.rb => materializer_test.rb} (78%) diff --git a/app/models/balance/syncer.rb b/app/models/balance/materializer.rb similarity index 89% rename from app/models/balance/syncer.rb rename to app/models/balance/materializer.rb index 890bb5f9..75a98ffd 100644 --- a/app/models/balance/syncer.rb +++ b/app/models/balance/materializer.rb @@ -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 diff --git a/app/models/holding/syncer.rb b/app/models/holding/materializer.rb similarity index 87% rename from app/models/holding/syncer.rb rename to app/models/holding/materializer.rb index 345f2a3f..e4931128 100644 --- a/app/models/holding/syncer.rb +++ b/app/models/holding/materializer.rb @@ -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") diff --git a/test/models/balance/syncer_test.rb b/test/models/balance/materializer_test.rb similarity index 82% rename from test/models/balance/syncer_test.rb rename to test/models/balance/materializer_test.rb index 648f6b3a..4a5ac439 100644 --- a/test/models/balance/syncer_test.rb +++ b/test/models/balance/materializer_test.rb @@ -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 diff --git a/test/models/holding/syncer_test.rb b/test/models/holding/materializer_test.rb similarity index 78% rename from test/models/holding/syncer_test.rb rename to test/models/holding/materializer_test.rb index 19e191c8..41ae8ff6 100644 --- a/test/models/holding/syncer_test.rb +++ b/test/models/holding/materializer_test.rb @@ -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