Class: CrazyMoney

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/crazy_money.rb

Defined Under Namespace

Modules: Configuration

Class Method Summary collapse

Instance Method Summary collapse

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

.zeroObject



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

#inspectObject



30
31
32
# File 'lib/crazy_money.rb', line 30

def inspect
  "#<CrazyMoney amount=#{to_s}>"
end

#negative?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/crazy_money.rb', line 47

def negative?
  @amount < 0
end

#oppositeObject



55
56
57
# File 'lib/crazy_money.rb', line 55

def opposite
  self.class.new(self * -1)
end

#positive?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


51
52
53
# File 'lib/crazy_money.rb', line 51

def zero?
  @amount.zero?
end