2024-03-18 11:21:00 -04:00
|
|
|
class Money
|
2024-10-03 10:25:38 -04:00
|
|
|
include Comparable, Arithmetic, Formatting
|
2024-04-22 06:30:42 -06:00
|
|
|
include ActiveModel::Validations
|
2024-03-18 11:21:00 -04:00
|
|
|
|
2024-07-08 09:04:59 -04:00
|
|
|
class ConversionError < StandardError
|
2024-08-16 12:13:48 -04:00
|
|
|
attr_reader :from_currency, :to_currency, :date
|
|
|
|
|
|
|
|
def initialize(from_currency:, to_currency:, date:)
|
|
|
|
@from_currency = from_currency
|
|
|
|
@to_currency = to_currency
|
|
|
|
@date = date
|
|
|
|
|
|
|
|
error_message = message || "Couldn't find exchange rate from #{from_currency} to #{to_currency} on #{date}"
|
|
|
|
|
|
|
|
super(error_message)
|
|
|
|
end
|
2024-07-08 09:04:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :amount, :currency, :store
|
2024-03-18 11:21:00 -04:00
|
|
|
|
2024-04-22 06:30:42 -06:00
|
|
|
validate :source_must_be_of_known_type
|
2024-03-18 11:21:00 -04:00
|
|
|
|
2024-04-22 06:30:42 -06:00
|
|
|
class << self
|
|
|
|
def default_currency
|
|
|
|
@default ||= Money::Currency.new(:usd)
|
2024-03-18 11:21:00 -04:00
|
|
|
end
|
|
|
|
|
2024-04-22 06:30:42 -06:00
|
|
|
def default_currency=(object)
|
|
|
|
@default = Money::Currency.new(object)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-08 09:04:59 -04:00
|
|
|
def initialize(obj, currency = Money.default_currency, store: ExchangeRate)
|
2024-04-22 06:30:42 -06:00
|
|
|
@source = obj
|
|
|
|
@amount = obj.is_a?(Money) ? obj.amount : BigDecimal(obj.to_s)
|
|
|
|
@currency = obj.is_a?(Money) ? obj.currency : Money::Currency.new(currency)
|
2024-07-08 09:04:59 -04:00
|
|
|
@store = store
|
2024-03-18 11:21:00 -04:00
|
|
|
|
2024-04-22 06:30:42 -06:00
|
|
|
validate!
|
|
|
|
end
|
|
|
|
|
2024-07-08 09:04:59 -04:00
|
|
|
def exchange_to(other_currency, date: Date.current, fallback_rate: nil)
|
|
|
|
iso_code = currency.iso_code
|
|
|
|
other_iso_code = Money::Currency.new(other_currency).iso_code
|
|
|
|
|
|
|
|
if iso_code == other_iso_code
|
2024-04-22 06:30:42 -06:00
|
|
|
self
|
2024-07-08 09:04:59 -04:00
|
|
|
else
|
2025-03-17 11:54:53 -04:00
|
|
|
exchange_rate = store.find_or_fetch_rate(from: iso_code, to: other_iso_code, date: date)&.rate || fallback_rate
|
2024-07-08 09:04:59 -04:00
|
|
|
|
2024-08-16 12:13:48 -04:00
|
|
|
raise ConversionError.new(from_currency: iso_code, to_currency: other_iso_code, date: date) unless exchange_rate
|
2024-07-08 09:04:59 -04:00
|
|
|
|
|
|
|
Money.new(amount * exchange_rate, other_iso_code)
|
2024-03-18 11:21:00 -04:00
|
|
|
end
|
2024-04-22 06:30:42 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def as_json
|
2025-02-21 11:57:59 -05:00
|
|
|
{ amount: amount, currency: currency.iso_code, formatted: format }.as_json
|
2024-04-22 06:30:42 -06:00
|
|
|
end
|
2024-03-18 11:21:00 -04:00
|
|
|
|
2024-04-22 06:30:42 -06:00
|
|
|
def <=>(other)
|
|
|
|
raise TypeError, "Money can only be compared with other Money objects except for 0" unless other.is_a?(Money) || other.eql?(0)
|
2024-03-18 11:21:00 -04:00
|
|
|
|
2024-04-22 06:30:42 -06:00
|
|
|
if other.is_a?(Numeric)
|
|
|
|
amount <=> other
|
|
|
|
else
|
|
|
|
amount_comparison = amount <=> other.amount
|
2024-03-19 09:10:40 -04:00
|
|
|
|
2024-04-22 06:30:42 -06:00
|
|
|
if amount_comparison == 0
|
|
|
|
currency <=> other.currency
|
|
|
|
else
|
|
|
|
amount_comparison
|
|
|
|
end
|
2024-03-18 11:21:00 -04:00
|
|
|
end
|
2024-04-22 06:30:42 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def source_must_be_of_known_type
|
|
|
|
unless @source.is_a?(Money) || @source.is_a?(Numeric) || @source.is_a?(BigDecimal)
|
|
|
|
errors.add :source, "must be a Money, Numeric, or BigDecimal"
|
|
|
|
end
|
2024-03-18 11:21:00 -04:00
|
|
|
end
|
|
|
|
end
|