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