Class: Money

Inherits:
Object
  • Object
show all
Defined in:
lib/coinage/money.rb

Constant Summary collapse

@@default_currency =
:usd
@@default_exchange =
Coinage::Exchange::None.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cents = 0, currency = self.class.default_currency) ⇒ Money

Returns a new instance of Money.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/coinage/money.rb', line 12

def initialize(cents=0, currency=self.class.default_currency)
  @currency = currency
  
  case cents
  when Float
    @cents = cents_from_float(cents)
  when String
    @cents = cents_from_string(cents)
  else
    @cents = cents.to_i
  end
  
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/coinage/money.rb', line 130

def method_missing(method, *arguments)
  if new_currency = extract_currency_from_method(method)
    return exchange_to(new_currency)
  else
    super
  end
end

Instance Attribute Details

#centsObject

Returns the value of attribute cents.



9
10
11
# File 'lib/coinage/money.rb', line 9

def cents
  @cents
end

#currencyObject

Returns the value of attribute currency.



9
10
11
# File 'lib/coinage/money.rb', line 9

def currency
  @currency
end

#exchangeObject



50
51
52
# File 'lib/coinage/money.rb', line 50

def exchange
  @exchange || self.class.default_exchange
end

Instance Method Details

#*(target) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/coinage/money.rb', line 67

def *(target)
  case target
  when Numeric
    self.class.new( cents * target, currency )
  when Money
    self.class.new( cents * target.exchange_to(currency).cents, currency )
  else
    raise ArgumentError
  end
end

#+(target) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/coinage/money.rb', line 89

def +(target)
  case target
  when Numeric
    self.class.new( cents + target, currency )
  when Money
    self.class.new( cents + target.exchange_to(currency).cents, currency )
  else
    raise ArgumentError
  end
end

#-(target) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/coinage/money.rb', line 100

def -(target)
  case target
  when Numeric
    self.class.new( cents - target, currency )
  when Money
    self.class.new( cents - target.exchange_to(currency).cents, currency )
  else
    raise ArgumentError
  end
end

#/(target) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/coinage/money.rb', line 78

def /(target)
  case target
  when Numeric
    self.class.new( cents / target, currency )
  when Money
    self.class.new( cents / target.exchange_to(currency).cents, currency )
  else
    raise ArgumentError
  end
end

#coerce(other) ⇒ Object



138
139
140
# File 'lib/coinage/money.rb', line 138

def coerce(other)
  return self, other
end

#dollarsObject



42
43
44
# File 'lib/coinage/money.rb', line 42

def dollars
  (cents/100).round
end

#eql?(target) ⇒ Boolean Also known as: ==, equal?

Uses == instead of .eql? to perform a type conversion between Float and Fixnum

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/coinage/money.rb', line 112

def eql?(target)
  case target
  when Numeric
    cents == target
  when Money
    if currency == target.currency
      cents == target.cents
    else
      cents == target.exchange_to(currency).cents
    end
  else
    false
  end
end

#exchange_to(target) ⇒ Object



63
64
65
# File 'lib/coinage/money.rb', line 63

def exchange_to(target)
  dup.exchange_to!(target)
end

#exchange_to!(target) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
# File 'lib/coinage/money.rb', line 54

def exchange_to!(target)
  raise ArgumentError, "#{exchange.class} does not support conversions from #{currency.to_s.upcase}" unless exchange.supported_currencies.include?(currency)
  raise ArgumentError, "#{exchange.class} does not support conversions to #{target.to_s.upcase}" unless exchange.supported_currencies.include?(target)
  unless currency == target
    self.cents, self.currency = cents * exchange.rate(currency, target), target
  end
  self
end

#roundObject



46
47
48
# File 'lib/coinage/money.rb', line 46

def round
  (cents.to_f/100).round
end

#to_fObject



38
39
40
# File 'lib/coinage/money.rb', line 38

def to_f
  cents.to_f/100
end

#to_iObject



34
35
36
# File 'lib/coinage/money.rb', line 34

def to_i
  cents
end

#to_moneyObject



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

def to_money
  self
end

#to_sObject



142
143
144
# File 'lib/coinage/money.rb', line 142

def to_s
  to_f.to_s
end

#zero?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/coinage/money.rb', line 26

def zero?
  cents.zero?
end