1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-08 06:55:21 +02:00

Use resolver for trade builder

This commit is contained in:
Zach Gollwitzer 2025-05-22 11:58:06 -04:00
parent 4a72b854d7
commit 2183232aec
3 changed files with 11 additions and 13 deletions

View file

@ -1,6 +1,6 @@
class Security::Resolver class Security::Resolver
def initialize(symbol, exchange_operating_mic: nil, country_code: nil) def initialize(symbol, exchange_operating_mic: nil, country_code: nil)
@symbol = symbol @symbol = validate_symbol!(symbol)
@exchange_operating_mic = exchange_operating_mic @exchange_operating_mic = exchange_operating_mic
@country_code = country_code @country_code = country_code
end end
@ -22,6 +22,11 @@ class Security::Resolver
private private
attr_reader :symbol, :exchange_operating_mic, :country_code attr_reader :symbol, :exchange_operating_mic, :country_code
def validate_symbol!(symbol)
raise ArgumentError, "Symbol is required and cannot be blank" if symbol.blank?
symbol.strip.upcase
end
def offline_security def offline_security
security = Security.find_or_initialize_by( security = Security.find_or_initialize_by(
ticker: symbol, ticker: symbol,

View file

@ -1,8 +1,6 @@
class TradeBuilder class TradeBuilder
include ActiveModel::Model include ActiveModel::Model
Error = Class.new(StandardError)
attr_accessor :account, :date, :amount, :currency, :qty, attr_accessor :account, :date, :amount, :currency, :qty,
:price, :ticker, :manual_ticker, :type, :transfer_account_id :price, :ticker, :manual_ticker, :type, :transfer_account_id
@ -131,13 +129,9 @@ class TradeBuilder
def security def security
ticker_symbol, exchange_operating_mic = ticker.present? ? ticker.split("|") : [ manual_ticker, nil ] ticker_symbol, exchange_operating_mic = ticker.present? ? ticker.split("|") : [ manual_ticker, nil ]
unless ticker_symbol.present? Security::Resolver.new(
raise Error, "Ticker symbol is required to create a trade" ticker_symbol,
end
Security.find_or_create_by!(
ticker: ticker_symbol,
exchange_operating_mic: exchange_operating_mic exchange_operating_mic: exchange_operating_mic
) ).resolve
end end
end end

View file

@ -72,8 +72,7 @@ class Security::ResolverTest < ActiveSupport::TestCase
end end
test "returns nil when symbol blank" do test "returns nil when symbol blank" do
assert_no_difference "Security.count" do assert_raises(ArgumentError) { Security::Resolver.new(nil).resolve }
assert_nil Security::Resolver.new(nil).resolve assert_raises(ArgumentError) { Security::Resolver.new("").resolve }
end
end end
end end