mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-04 21:15:19 +02:00
Refactor Money
This commit is contained in:
parent
03cee1f121
commit
31caf7056d
1 changed files with 52 additions and 31 deletions
85
lib/money.rb
85
lib/money.rb
|
@ -1,9 +1,11 @@
|
||||||
class Money
|
class Money
|
||||||
include Comparable
|
include Comparable, Arithmetic
|
||||||
include Arithmetic
|
include ActiveModel::Validations
|
||||||
|
|
||||||
attr_reader :amount, :currency
|
attr_reader :amount, :currency
|
||||||
|
|
||||||
|
validate :source_must_be_of_known_type
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def default_currency
|
def default_currency
|
||||||
@default ||= Money::Currency.new(:usd)
|
@default ||= Money::Currency.new(:usd)
|
||||||
|
@ -15,59 +17,78 @@ class Money
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(obj, currency = Money.default_currency)
|
def initialize(obj, currency = Money.default_currency)
|
||||||
unless obj.is_a?(Money) || obj.is_a?(Numeric) || obj.is_a?(BigDecimal)
|
@source = obj
|
||||||
raise ArgumentError, "obj must be an instance of Money, Numeric, or BigDecimal"
|
|
||||||
end
|
|
||||||
|
|
||||||
@amount = obj.is_a?(Money) ? obj.amount : BigDecimal(obj.to_s)
|
@amount = obj.is_a?(Money) ? obj.amount : BigDecimal(obj.to_s)
|
||||||
@currency = obj.is_a?(Money) ? obj.currency : Money::Currency.new(currency)
|
@currency = obj.is_a?(Money) ? obj.currency : Money::Currency.new(currency)
|
||||||
|
|
||||||
|
validate!
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: Replace with injected rate store
|
# TODO: Replace with injected rate store
|
||||||
def exchange_to(other_currency, date = Date.current)
|
def exchange_to(other_currency, date = Date.current)
|
||||||
return self if @currency == Money::Currency.new(other_currency)
|
if currency == Money::Currency.new(other_currency)
|
||||||
rate = ExchangeRate.find_rate(from: @currency, to: other_currency, date: date)
|
self
|
||||||
return nil if rate.nil?
|
elsif rate = ExchangeRate.find_rate(from: currency, to: other_currency, date: date)
|
||||||
Money.new(@amount * rate.rate, other_currency)
|
Money.new(amount * rate.rate, other_currency)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def cents_str(precision = @currency.default_precision)
|
def cents_str(precision = currency.default_precision)
|
||||||
format_str = "%.#{precision}f"
|
format_str = "%.#{precision}f"
|
||||||
amount_str = format_str % @amount
|
amount_str = format_str % amount
|
||||||
parts = amount_str.split(@currency.separator)
|
parts = amount_str.split(currency.separator)
|
||||||
|
|
||||||
return "" if parts.length < 2
|
|
||||||
|
|
||||||
|
if parts.length < 2
|
||||||
|
""
|
||||||
|
else
|
||||||
parts.last.ljust(precision, "0")
|
parts.last.ljust(precision, "0")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Basic formatting only. Use the Rails number_to_currency helper for more advanced formatting.
|
|
||||||
alias to_s format
|
|
||||||
def format
|
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Use `format` for basic formatting only.
|
||||||
|
# Use the Rails number_to_currency helper for more advanced formatting.
|
||||||
|
def format
|
||||||
|
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)
|
||||||
|
end
|
||||||
|
alias_method :to_s, :format
|
||||||
|
|
||||||
def as_json
|
def as_json
|
||||||
{ amount: @amount, currency: @currency.iso_code }.as_json
|
{ amount: amount, currency: currency.iso_code }.as_json
|
||||||
end
|
end
|
||||||
|
|
||||||
def <=>(other)
|
def <=>(other)
|
||||||
raise TypeError, "Money can only be compared with other Money objects except for 0" unless other.is_a?(Money) || other.eql?(0)
|
raise TypeError, "Money can only be compared with other Money objects except for 0" unless other.is_a?(Money) || other.eql?(0)
|
||||||
return @amount <=> other if other.is_a?(Numeric)
|
|
||||||
amount_comparison = @amount <=> other.amount
|
if other.is_a?(Numeric)
|
||||||
return amount_comparison unless amount_comparison == 0
|
amount <=> other
|
||||||
@currency <=> other.currency
|
else
|
||||||
|
amount_comparison = amount <=> other.amount
|
||||||
|
|
||||||
|
if amount_comparison == 0
|
||||||
|
currency <=> other.currency
|
||||||
|
else
|
||||||
|
amount_comparison
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def default_format_options
|
def default_format_options
|
||||||
{
|
{
|
||||||
unit: @currency.symbol,
|
unit: currency.symbol,
|
||||||
precision: @currency.default_precision,
|
precision: currency.default_precision,
|
||||||
delimiter: @currency.delimiter,
|
delimiter: currency.delimiter,
|
||||||
separator: @currency.separator
|
separator: currency.separator
|
||||||
}
|
}
|
||||||
end
|
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
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue