Class: CrazyMoney
Defined Under Namespace
Modules: Configuration
Class Method Summary collapse
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #cents(ratio = 100) ⇒ Object
-
#initialize(amount) ⇒ CrazyMoney
constructor
A new instance of CrazyMoney.
- #inspect ⇒ Object
- #negative? ⇒ Boolean
- #opposite ⇒ Object
- #positive? ⇒ Boolean
- #to_s(decimal_places: 2) ⇒ Object
-
#with_currency(iso_code) ⇒ Object
FIXME: needs polishing.
- #zero? ⇒ Boolean
Constructor Details
#initialize(amount) ⇒ CrazyMoney
Returns a new instance of CrazyMoney.
18 19 20 |
# File 'lib/crazy_money.rb', line 18 def initialize amount @amount = BigDecimal.new(amount.to_s) end |
Class Method Details
.zero ⇒ Object
14 15 16 |
# File 'lib/crazy_money.rb', line 14 def self.zero new 0 end |
Instance Method Details
#*(other) ⇒ Object
66 |
# File 'lib/crazy_money.rb', line 66 def * other; self.class.new(@amount * BigDecimal.new(other.to_s)); end |
#+(other) ⇒ Object
63 |
# File 'lib/crazy_money.rb', line 63 def + other; self.class.new(@amount + BigDecimal.new(other.to_s)); end |
#-(other) ⇒ Object
64 |
# File 'lib/crazy_money.rb', line 64 def - other; self.class.new(@amount - BigDecimal.new(other.to_s)); end |
#/(other) ⇒ Object
65 |
# File 'lib/crazy_money.rb', line 65 def / other; self.class.new(@amount / BigDecimal.new(other.to_s)); end |
#<=>(other) ⇒ Object
39 40 41 |
# File 'lib/crazy_money.rb', line 39 def <=> other @amount <=> BigDecimal.new(other.to_s) end |
#==(other) ⇒ Object Also known as: eql?
34 35 36 |
# File 'lib/crazy_money.rb', line 34 def == other @amount == BigDecimal.new(other.to_s) end |
#cents(ratio = 100) ⇒ Object
59 60 61 |
# File 'lib/crazy_money.rb', line 59 def cents(ratio = 100) @amount * BigDecimal.new(ratio.to_s) end |
#inspect ⇒ Object
30 31 32 |
# File 'lib/crazy_money.rb', line 30 def inspect "#<CrazyMoney amount=#{to_s}>" end |
#negative? ⇒ Boolean
47 48 49 |
# File 'lib/crazy_money.rb', line 47 def negative? @amount < 0 end |
#opposite ⇒ Object
55 56 57 |
# File 'lib/crazy_money.rb', line 55 def opposite self.class.new(self * -1) end |
#positive? ⇒ Boolean
43 44 45 |
# File 'lib/crazy_money.rb', line 43 def positive? @amount > 0 end |
#to_s(decimal_places: 2) ⇒ Object
22 23 24 25 26 27 28 |
# 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) ⇒ Object
FIXME: needs polishing
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/crazy_money.rb', line 69 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? ⇒ Boolean
51 52 53 |
# File 'lib/crazy_money.rb', line 51 def zero? @amount.zero? end |