Class: Economy::Money

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, currency) ⇒ Money

Returns a new instance of Money.



9
10
11
12
13
14
15
16
# File 'lib/economy/money.rb', line 9

def initialize(amount, currency)
  if amount.is_a?(BigDecimal)
    @amount = amount
  else
    @amount = BigDecimal(amount.to_s)
  end
  @currency = normalize_currency(currency)
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



5
6
7
# File 'lib/economy/money.rb', line 5

def amount
  @amount
end

#currencyObject (readonly)

Returns the value of attribute currency.



5
6
7
# File 'lib/economy/money.rb', line 5

def currency
  @currency
end

Instance Method Details

#%(value) ⇒ Object Also known as: modulo



120
121
122
# File 'lib/economy/money.rb', line 120

def %(value)
  divmod(value)[1]
end

#*(value) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/economy/money.rb', line 84

def *(value)
  case value
  when Money
    amount * value.exchange_to(currency).amount
  when Numeric
    Money.new (amount * value), currency
  else
    raise "Can't multiply Money by #{value.class.name}"
  end
end

#+(other) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/economy/money.rb', line 66

def +(other)
  if other.is_a?(Money)
    other = other.exchange_to(currency)
    Money.new (amount + other.amount), currency
  else
    raise "Can't add #{other.class.name} to Money"
  end
end

#-(other) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/economy/money.rb', line 75

def -(other)
  if other.is_a?(Money)
    other = other.exchange_to(currency)
    Money.new (amount - other.amount), currency
  else
    raise "Can't subtract #{other.class.name} from Money"
  end
end

#-@Object



27
28
29
# File 'lib/economy/money.rb', line 27

def -@
  Money.new -amount, currency
end

#/(value) ⇒ Object Also known as: div



95
96
97
98
99
100
101
102
103
104
# File 'lib/economy/money.rb', line 95

def /(value)
  case value
  when Money
    amount / value.exchange_to(currency).amount
  when Numeric
    Money.new (amount / value), currency
  else
    raise "Can't divide Money by #{value.class.name}"
  end
end

#<=>(other) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/economy/money.rb', line 55

def <=>(other)
  if other.is_a?(Numeric) && other == 0
    amount <=> other
  elsif other.is_a?(Money)
    other = other.exchange_to(currency)
    amount <=> other.amount
  else
    raise "Can't compare #{other.class.name} with Money"
  end
end

#===(other) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/economy/money.rb', line 47

def ===(other)
  if other.is_a?(Money)
    amount == other.amount && currency == other.currency
  else
    raise "Can't compare #{other.class.name} with Money"
  end
end

#absObject Also known as: magnitude



22
23
24
# File 'lib/economy/money.rb', line 22

def abs
  Money.new amount.abs, currency
end

#coerce(other) ⇒ Object



18
19
20
# File 'lib/economy/money.rb', line 18

def coerce(other)
  [self, other]
end

#divmod(value) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/economy/money.rb', line 107

def divmod(value)
  case value
  when Money
    quotient, modulo = amount.divmod(value.exchange_to(currency).amount)
    [quotient, Money.new(modulo, currency)]
  when Numeric
    quotient, modulo = amount.divmod(value)
    [Money.new(quotient, currency), Money.new(modulo, currency)]
  else
    raise "Can't divide Money by #{value.class.name}"
  end
end

#exchange_to(new_currency) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/economy/money.rb', line 136

def exchange_to(new_currency)
  new_currency = normalize_currency(new_currency)
  if currency != new_currency
    if rate = Economy.rate(currency, new_currency)
      Money.new (amount * BigDecimal(rate)), new_currency
    else
      raise "Rate #{currency.iso_code} => #{new_currency.iso_code} not found"
    end
  else
    self
  end
end

#negative?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/economy/money.rb', line 35

def negative?
  amount < 0
end

#nonzero?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/economy/money.rb', line 43

def nonzero?
  amount != 0
end

#positive?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/economy/money.rb', line 31

def positive?
  amount > 0
end

#remainder(value) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/economy/money.rb', line 125

def remainder(value)
  case value
  when Money
    Money.new amount.remainder(value.exchange_to(currency).amount), currency
  when Numeric
    Money.new amount.remainder(value), currency
  else
    raise "Can't divide Money by #{value.class.name}"
  end
end

#to_json(options = {}) ⇒ Object Also known as: as_json



149
150
151
# File 'lib/economy/money.rb', line 149

def to_json(options={})
  "%.#{currency.decimals}f" % amount
end

#to_s(precision = nil) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/economy/money.rb', line 154

def to_s(precision=nil)
  ActiveSupport::NumberHelper.number_to_currency(
    amount,
    unit: currency.symbol,
    precision: (precision || currency.decimals)
  )
end

#zero?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/economy/money.rb', line 39

def zero?
  amount == 0
end