class CrazyMoney
Public Class Methods
new(amount)
click to toggle source
# File lib/crazy_money.rb, line 18 def initialize(amount) @amount = BigDecimal.new(amount.to_s) end
zero()
click to toggle source
# File lib/crazy_money.rb, line 14 def self.zero new 0 end
Public Instance Methods
*(other)
click to toggle source
# File lib/crazy_money.rb, line 87 def *(other); self.class.new(@amount * BigDecimal.new(other.to_s)); end
+(other)
click to toggle source
# File lib/crazy_money.rb, line 81 def +(other); self.class.new(@amount + BigDecimal.new(other.to_s)); end
-(other)
click to toggle source
# File lib/crazy_money.rb, line 83 def -(other); self.class.new(@amount - BigDecimal.new(other.to_s)); end
/(other)
click to toggle source
# File lib/crazy_money.rb, line 85 def /(other); self.class.new(@amount / BigDecimal.new(other.to_s)); end
<=>(other)
click to toggle source
# File lib/crazy_money.rb, line 53 def <=>(other) @amount <=> BigDecimal.new(other.to_s) end
==(other)
click to toggle source
# File lib/crazy_money.rb, line 48 def ==(other) @amount == BigDecimal.new(other.to_s) end
Also aliased as: eql?
cents(ratio = 100)
click to toggle source
# File lib/crazy_money.rb, line 77 def cents(ratio = 100) @amount * BigDecimal.new(ratio.to_s) end
inspect()
click to toggle source
# File lib/crazy_money.rb, line 44 def inspect "#<CrazyMoney amount=#{self}>" end
negative?()
click to toggle source
# File lib/crazy_money.rb, line 61 def negative? @amount < 0 end
opposite()
click to toggle source
# File lib/crazy_money.rb, line 69 def opposite self.class.new(self * -1) end
positive?()
click to toggle source
# File lib/crazy_money.rb, line 57 def positive? @amount > 0 end
round(*args)
click to toggle source
# File lib/crazy_money.rb, line 73 def round(*args) self.class.new(@amount.round(*args)) end
to_i()
click to toggle source
# File lib/crazy_money.rb, line 30 def to_i @amount.to_i end
to_k()
click to toggle source
# File lib/crazy_money.rb, line 34 def to_k if @amount.abs < 1E3 amount, suffix = @amount, "" else amount, suffix = (@amount / 1E3), "k" end "#{amount.round.to_i}#{suffix}".freeze end
to_s(decimal_places: 2)
click to toggle source
# File lib/crazy_money.rb, line 22 def to_s(decimal_places: 2) if current_currency = Configuration.current_currency decimal_places = currency(current_currency).decimal_places end sprintf("%.#{decimal_places}f", @amount) end
with_currency(iso_code)
click to toggle source
FIXME: needs polishing
# File lib/crazy_money.rb, line 90 def with_currency(iso_code) currency = currency(iso_code) || raise(ArgumentError, "Unknown currency: #{iso_code.inspect}") left, right = to_s(decimal_places: currency.decimal_places).split(".") decimal_mark = right.nil? ? "" : currency.decimal_mark sign = left.slice!("-") left = left.reverse.scan(/.{1,3}/).map(&:reverse).reverse # split every 3 digits right-to-left .join(thousands_separator) formatted = [sign, left, decimal_mark, right].join if currency.symbol_first [currency.prefered_symbol, formatted] else [formatted, " ", currency.prefered_symbol] end.join end
zero?()
click to toggle source
# File lib/crazy_money.rb, line 65 def zero? @amount.zero? end
Private Instance Methods
currency(iso_code)
click to toggle source
# File lib/crazy_money.rb, line 118 def currency(iso_code) ::CurrencyData.find(iso_code) end
thousands_separator()
click to toggle source
# File lib/crazy_money.rb, line 111 def thousands_separator default = " ".freeze I18n.t("number.currency.format.thousands_separator", default: default) rescue I18n::InvalidLocale default end