mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-08 15:05:22 +02:00
Use resolver for trade builder
This commit is contained in:
parent
4a72b854d7
commit
2183232aec
3 changed files with 11 additions and 13 deletions
|
@ -1,6 +1,6 @@
|
|||
class Security::Resolver
|
||||
def initialize(symbol, exchange_operating_mic: nil, country_code: nil)
|
||||
@symbol = symbol
|
||||
@symbol = validate_symbol!(symbol)
|
||||
@exchange_operating_mic = exchange_operating_mic
|
||||
@country_code = country_code
|
||||
end
|
||||
|
@ -22,6 +22,11 @@ class Security::Resolver
|
|||
private
|
||||
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
|
||||
security = Security.find_or_initialize_by(
|
||||
ticker: symbol,
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
class TradeBuilder
|
||||
include ActiveModel::Model
|
||||
|
||||
Error = Class.new(StandardError)
|
||||
|
||||
attr_accessor :account, :date, :amount, :currency, :qty,
|
||||
:price, :ticker, :manual_ticker, :type, :transfer_account_id
|
||||
|
||||
|
@ -131,13 +129,9 @@ class TradeBuilder
|
|||
def security
|
||||
ticker_symbol, exchange_operating_mic = ticker.present? ? ticker.split("|") : [ manual_ticker, nil ]
|
||||
|
||||
unless ticker_symbol.present?
|
||||
raise Error, "Ticker symbol is required to create a trade"
|
||||
end
|
||||
|
||||
Security.find_or_create_by!(
|
||||
ticker: ticker_symbol,
|
||||
Security::Resolver.new(
|
||||
ticker_symbol,
|
||||
exchange_operating_mic: exchange_operating_mic
|
||||
)
|
||||
).resolve
|
||||
end
|
||||
end
|
||||
|
|
|
@ -72,8 +72,7 @@ class Security::ResolverTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
test "returns nil when symbol blank" do
|
||||
assert_no_difference "Security.count" do
|
||||
assert_nil Security::Resolver.new(nil).resolve
|
||||
end
|
||||
assert_raises(ArgumentError) { Security::Resolver.new(nil).resolve }
|
||||
assert_raises(ArgumentError) { Security::Resolver.new("").resolve }
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue