1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-02 12:05:19 +02:00
Maybe/lib/money.rb

95 lines
2.6 KiB
Ruby
Raw Normal View History

class Money
2024-04-18 21:06:47 -06:00
include Comparable, Arithmetic
include ActiveModel::Validations
2024-04-18 20:01:58 -06:00
attr_reader :amount, :currency
2024-04-18 21:06:47 -06:00
validate :source_must_be_of_known_type
2024-04-18 20:01:58 -06:00
class << self
def default_currency
@default ||= Money::Currency.new(:usd)
end
2024-04-18 20:01:58 -06:00
def default_currency=(object)
@default = Money::Currency.new(object)
end
2024-04-18 20:01:58 -06:00
end
2024-04-18 20:01:58 -06:00
def initialize(obj, currency = Money.default_currency)
2024-04-18 21:06:47 -06:00
@source = obj
2024-04-18 20:01:58 -06:00
@amount = obj.is_a?(Money) ? obj.amount : BigDecimal(obj.to_s)
@currency = obj.is_a?(Money) ? obj.currency : Money::Currency.new(currency)
2024-04-18 21:06:47 -06:00
validate!
2024-04-18 20:01:58 -06:00
end
2024-04-18 20:01:58 -06:00
# TODO: Replace with injected rate store
def exchange_to(other_currency, date = Date.current)
2024-04-18 21:06:47 -06:00
if currency == Money::Currency.new(other_currency)
self
elsif rate = ExchangeRate.find_rate(from: currency, to: other_currency, date: date)
Money.new(amount * rate.rate, other_currency)
end
2024-04-18 20:01:58 -06:00
end
2024-04-18 21:06:47 -06:00
def cents_str(precision = currency.default_precision)
2024-04-18 20:01:58 -06:00
format_str = "%.#{precision}f"
2024-04-18 21:06:47 -06:00
amount_str = format_str % amount
parts = amount_str.split(currency.separator)
2024-04-18 21:06:47 -06:00
if parts.length < 2
""
else
parts.last.ljust(precision, "0")
end
2024-04-18 20:01:58 -06:00
end
2024-04-18 21:06:47 -06:00
# Use `format` for basic formatting only.
# Use the Rails number_to_currency helper for more advanced formatting.
2024-04-18 20:01:58 -06:00
def format
2024-04-18 21:06:47 -06:00
whole_part, fractional_part = sprintf("%.#{currency.default_precision}f", amount).split(".")
whole_with_delimiters = whole_part.chars.to_a.reverse.each_slice(3).map(&:join).join(currency.delimiter).reverse
formatted_amount = "#{whole_with_delimiters}#{currency.separator}#{fractional_part}"
currency.default_format.gsub("%n", formatted_amount).gsub("%u", currency.symbol)
2024-04-18 20:01:58 -06:00
end
2024-04-18 21:06:47 -06:00
alias_method :to_s, :format
2024-04-18 20:01:58 -06:00
def as_json
2024-04-18 21:06:47 -06:00
{ amount: amount, currency: currency.iso_code }.as_json
2024-04-18 20:01:58 -06:00
end
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-04-18 21:06:47 -06:00
if other.is_a?(Numeric)
amount <=> other
else
amount_comparison = amount <=> other.amount
if amount_comparison == 0
currency <=> other.currency
else
amount_comparison
end
end
2024-04-18 20:01:58 -06:00
end
def default_format_options
{
2024-04-18 21:06:47 -06:00
unit: currency.symbol,
precision: currency.default_precision,
delimiter: currency.delimiter,
separator: currency.separator
2024-04-18 20:01:58 -06:00
}
end
2024-04-18 21:06:47 -06:00
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
end
end