money 3.1.0.pre3 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,44 +1,77 @@
1
1
  require 'money/bank/base'
2
- require 'json'
3
- require 'yaml'
2
+ autoload :JSON, 'json'
3
+ autoload :YAML, 'yaml'
4
4
 
5
5
  class Money
6
6
  module Bank
7
- # Thrown by VariableExchange#export_rates and VariableExchange#import_rates
8
- # when an unknown rate format is requested.
7
+ # Thrown when an unknown rate format is requested.
9
8
  class UnknownRateFormat < StandardError; end
10
9
 
11
- # Class for aiding in exchanging money between different currencies.
12
- # By default, the Money class uses an object of this class (accessible through
13
- # Money#bank) for performing currency exchanges.
10
+ # Class for aiding in exchanging money between different currencies. By
11
+ # default, the +Money+ class uses an object of this class (accessible
12
+ # through +Money#bank+) for performing currency exchanges.
14
13
  #
15
- # By default, Bank::VariableExchange has no knowledge about conversion rates.
16
- # One must manually specify them with +add_rate+, after which one can perform
17
- # exchanges with +exchange+. For example:
14
+ # By default, +Money::Bank::VariableExchange+ has no knowledge about
15
+ # conversion rates. One must manually specify them with +add_rate+, after
16
+ # which one can perform exchanges with +#exchange_with+.
18
17
  #
18
+ # @example
19
19
  # bank = Money::Bank::VariableExchange.new
20
20
  # bank.add_rate("USD", "CAD", 1.24515)
21
21
  # bank.add_rate("CAD", "USD", 0.803115)
22
22
  #
23
- # # Exchange 100 CAD to USD:
24
- # bank.exchange(100_00, "CAD", "USD") # => 124
23
+ # c1 = 100_00.to_money("USD")
24
+ # c2 = 100_00.to_money("CAD")
25
+ #
25
26
  # # Exchange 100 USD to CAD:
26
- # bank.exchange(100_00, "USD", "CAD") # => 80
27
+ # bank.exchange_with(c1, "CAD") #=> #<Money @cents=1245150>
27
28
  #
29
+ # # Exchange 100 CAD to USD:
30
+ # bank.exchange_with(c2, "USD") #=> #<Money @cents=803115>
28
31
  class VariableExchange < Base
29
32
 
33
+ # Available formats for importing/exporting rates.
30
34
  RATE_FORMATS = [:json, :ruby, :yaml]
31
35
 
36
+ # Setup rates hash and mutex for rates locking
37
+ #
38
+ # @return [self]
32
39
  def setup
33
40
  @rates = {}
34
41
  @mutex = Mutex.new
42
+ self
35
43
  end
36
44
 
37
-
38
45
  # Exchanges the given +Money+ object to a new +Money+ object in
39
- # +to_currency+. Returns a new +Money+ object.
46
+ # +to_currency+.
47
+ #
48
+ # @param [Money] from The +Money+ object to exchange.
49
+ # @param [Currency, String, Symbol] to_currency The currency to exchange
50
+ # to.
51
+ #
52
+ # @yield [n] Optional block to use when rounding after exchanging one
53
+ # currency for another.
54
+ # @yieldparam [Float] n The resulting float after exchanging one currency
55
+ # for another.
56
+ # @yieldreturn [Integer]
57
+ #
58
+ # @return [Money]
59
+ #
60
+ # @raise +Money::Bank::UnknownRate+ if the conversion rate is unknown.
40
61
  #
41
- # Raises <tt>Money::Bank::UnknownRate</tt> if the conversion rate is unknown.
62
+ # @example
63
+ # bank = Money::Bank::VariableExchange.new
64
+ # bank.add_rate("USD", "CAD", 1.24515)
65
+ # bank.add_rate("CAD", "USD", 0.803115)
66
+ #
67
+ # c1 = 100_00.to_money("USD")
68
+ # c2 = 100_00.to_money("CAD")
69
+ #
70
+ # # Exchange 100 USD to CAD:
71
+ # bank.exchange_with(c1, "CAD") #=> #<Money @cents=1245150>
72
+ #
73
+ # # Exchange 100 CAD to USD:
74
+ # bank.exchange_with(c2, "USD") #=> #<Money @cents=803115>
42
75
  def exchange_with(from, to_currency, &block)
43
76
  return from if same_currency?(from.currency, to_currency)
44
77
 
@@ -61,19 +94,54 @@ class Money
61
94
  Money.new(ex, _to_currency_)
62
95
  end
63
96
 
64
-
65
- # Registers a conversion rate. +from+ and +to+ are both currency names or
66
- # +Currency+ objects.
97
+ # Registers a conversion rate and returns it (uses +#set_rate+).
98
+ #
99
+ # @param [Currency, String, Symbol] from Currency to exchange from.
100
+ # @param [Currency, String, Symbol] to Currency to exchange to.
101
+ # @param [Numeric] rate Rate to use when exchanging currencies.
102
+ #
103
+ # @return [Numeric]
104
+ #
105
+ # @example
106
+ # bank = Money::Bank::VariableExchange.new
107
+ # bank.add_rate("USD", "CAD", 1.24515)
108
+ # bank.add_rate("CAD", "USD", 0.803115)
67
109
  def add_rate(from, to, rate)
68
110
  set_rate(from, to, rate)
69
111
  end
70
112
 
71
- # Set the rate for the given currencies.
113
+ # Set the rate for the given currencies. Uses +Mutex+ to synchronize data
114
+ # access.
115
+ #
116
+ # @param [Currency, String, Symbol] from Currency to exchange from.
117
+ # @param [Currency, String, Symbol] to Currency to exchange to.
118
+ # @param [Numeric] rate Rate to use when exchanging currencies.
119
+ #
120
+ # @return [Numeric]
121
+ #
122
+ # @example
123
+ # bank = Money::Bank::VariableExchange.new
124
+ # bank.set_rate("USD", "CAD", 1.24515)
125
+ # bank.set_rate("CAD", "USD", 0.803115)
72
126
  def set_rate(from, to, rate)
73
127
  @mutex.synchronize { @rates[rate_key_for(from, to)] = rate }
74
128
  end
75
129
 
76
- # Retrieve the rate for the given currencies.
130
+ # Retrieve the rate for the given currencies. Uses +Mutex+ to synchronize
131
+ # data access.
132
+ #
133
+ # @param [Currency, String, Symbol] from Currency to exchange from.
134
+ # @param [Currency, String, Symbol] to Currency to exchange to.
135
+ #
136
+ # @return [Numeric]
137
+ #
138
+ # @example
139
+ # bank = Money::Bank::VariableExchange.new
140
+ # bank.set_rate("USD", "CAD", 1.24515)
141
+ # bank.set_rate("CAD", "USD", 0.803115)
142
+ #
143
+ # bank.get_rate("USD", "CAD") #=> 1.24515
144
+ # bank.get_rate("CAD", "USD") #=> 0.803115
77
145
  def get_rate(from, to)
78
146
  @mutex.synchronize { @rates[rate_key_for(from, to)] }
79
147
  end
@@ -82,7 +150,21 @@ class Money
82
150
  # is given will also write the string out to the file specified.
83
151
  # Available formats are +:json+, +:ruby+ and +:yaml+.
84
152
  #
85
- # Raises +Money::Bank::UnknownRateFormat+ if format is unknown.
153
+ # @param [Symbol] format Request format for the resulting string.
154
+ # @param [optional, String] file Optional file location to write the
155
+ # rates to.
156
+ #
157
+ # @return [String]
158
+ #
159
+ # @raise +Money::Bank::UnknownRateFormat+ if format is unknown.
160
+ #
161
+ # @example
162
+ # bank = Money::Bank::VariableExchange.new
163
+ # bank.set_rate("USD", "CAD", 1.24515)
164
+ # bank.set_rate("CAD", "USD", 0.803115)
165
+ #
166
+ # s = bank.export_rates(:json)
167
+ # s #=> "{\"USD_TO_CAD\":1.24515,\"CAD_TO_USD\":0.803115}"
86
168
  def export_rates(format, file=nil)
87
169
  raise Money::Bank::UnknownRateFormat unless
88
170
  RATE_FORMATS.include? format
@@ -91,11 +173,11 @@ class Money
91
173
  @mutex.synchronize {
92
174
  s = case format
93
175
  when :json
94
- @rates.to_json
176
+ JSON.dump(@rates)
95
177
  when :ruby
96
178
  Marshal.dump(@rates)
97
179
  when :yaml
98
- @rates.to_yaml
180
+ YAML.dump(@rates)
99
181
  end
100
182
 
101
183
  unless file.nil?
@@ -108,7 +190,20 @@ class Money
108
190
  # Loads rates provided in +s+ given the specified format. Available
109
191
  # formats are +:json+, +:ruby+ and +:yaml+.
110
192
  #
111
- # Raises +Money::Bank::UnknownRateFormat+ if format is unknown.
193
+ # @param [Symbol] format The format of +s+.
194
+ # @param [String] s The rates string.
195
+ #
196
+ # @return [self]
197
+ #
198
+ # @raise +Money::Bank::UnknownRateFormat+ if format is unknown.
199
+ #
200
+ # @example
201
+ # s = "{\"USD_TO_CAD\":1.24515,\"CAD_TO_USD\":0.803115}"
202
+ # bank = Money::Bank::VariableExchange.new
203
+ # bank.import_rates(:json, s)
204
+ #
205
+ # bank.get_rate("USD", "CAD") #=> 1.24515
206
+ # bank.get_rate("CAD", "USD") #=> 0.803115
112
207
  def import_rates(format, s)
113
208
  raise Money::Bank::UnknownRateFormat unless
114
209
  RATE_FORMATS.include? format
@@ -129,11 +224,17 @@ class Money
129
224
  private
130
225
 
131
226
  # Return the rate hashkey for the given currencies.
227
+ #
228
+ # @param [Currency, String, Symbol] from The currency to exchange from.
229
+ # @param [Currency, String, Symbol] to The currency to exchange to.
230
+ #
231
+ # @return [String]
232
+ #
233
+ # @example
234
+ # rate_key_for("USD", "CAD") #=> "USD_TO_CAD"
132
235
  def rate_key_for(from, to)
133
236
  "#{Currency.wrap(from).iso_code}_TO_#{Currency.wrap(to).iso_code}".upcase
134
237
  end
135
-
136
238
  end
137
-
138
239
  end
139
240
  end
@@ -1,11 +1,17 @@
1
+ # Open +Numeric+ to add new method.
1
2
  class Numeric
2
- # Converts this numeric to a Money object in the default currency. It
3
- # multiplies the numeric value by 100 and treats that as cents.
3
+ # Converts this numeric to a +Money+ object in the default currency.
4
4
  #
5
- # 100.to_money => #<Money @cents=10000>
6
- # 100.37.to_money => #<Money @cents=10037>
5
+ # @param [optional, Money::Currency, String, Symbol] currency The currency to
6
+ # set the resulting +Money+ object to.
7
+ #
8
+ # @return [Money]
9
+ #
10
+ # @example
11
+ # 100.to_money #=> #<Money @cents=10000>
12
+ # 100.37.to_money #=> #<Money @cents=10037>
7
13
  # require 'bigdecimal'
8
- # BigDecimal.new('100').to_money => #<Money @cents=10000>
14
+ # BigDecimal.new('100').to_money #=> #<Money @cents=10000>
9
15
  def to_money(currency = Money.default_currency)
10
16
  currency = Money::Currency.new(currency) unless currency.is_a?(Money::Currency)
11
17
  amt = self * currency.subunit_to_unit
@@ -19,22 +25,28 @@ class Numeric
19
25
  end
20
26
  end
21
27
 
28
+ # Open +String+ to add new methods.
22
29
  class String
23
-
24
- # Parses the current string and converts it to a Money object.
25
- # Excess characters will be discarded.
30
+ # Parses the current string and converts it to a +Money+ object. Excess
31
+ # characters will be discarded.
26
32
  #
27
- # '100'.to_money # => #<Money @cents=10000>
28
- # '100.37'.to_money # => #<Money @cents=10037>
29
- # '100 USD'.to_money # => #<Money @cents=10000, @currency="USD">
30
- # 'USD 100'.to_money # => #<Money @cents=10000, @currency="USD">
31
- # '$100 USD'.to_money # => #<Money @cents=10000, @currency="USD">
32
- # 'hello 2000 world'.to_money # => #<Money @cents=200000 @currency="USD")>
33
+ # @param [optional, Money::Currency, String, Symbol] currency The currency to
34
+ # set the resulting +Money+ object to.
35
+ #
36
+ # @return [Money]
37
+ #
38
+ # @example
39
+ # '100'.to_money #=> #<Money @cents=10000>
40
+ # '100.37'.to_money #=> #<Money @cents=10037>
41
+ # '100 USD'.to_money #=> #<Money @cents=10000, @currency=#<Money::Currency id: usd>>
42
+ # 'USD 100'.to_money #=> #<Money @cents=10000, @currency=#<Money::Currency id: usd>>
43
+ # '$100 USD'.to_money #=> #<Money @cents=10000, @currency=#<Money::Currency id: usd>>
44
+ # 'hello 2000 world'.to_money #=> #<Money @cents=200000 @currency=#<Money::Currency id: usd>>
33
45
  def to_money(currency = nil)
34
46
  # Get the currency.
35
47
  matches = scan /([A-Z]{2,3})/
36
48
  _currency_ = matches[0] ? matches[0][0] : nil
37
-
49
+
38
50
  # check that currency passed and embedded currency are the same, or only
39
51
  # one or the other is present.
40
52
  if currency.nil? and _currency_.nil?
@@ -46,18 +58,28 @@ class String
46
58
  elsif currency != _currency_
47
59
  raise "mismatching currencies"
48
60
  end
49
-
61
+
50
62
  cents = calculate_cents(self)
51
63
  Money.new(cents, currency)
52
64
  end
53
65
 
54
- # Parses the current string and converts it to a Currency object.
66
+ # Parses the current string and converts it to a +Currency+ object.
67
+ #
68
+ # @return [Money::Currency]
69
+ #
70
+ # @example
71
+ # "USD".to_currency #=> #<Money::Currency id: usd>
55
72
  def to_currency
56
73
  Money::Currency.new(self)
57
74
  end
58
75
 
59
76
  private
60
77
 
78
+ # Takes a number string and attempts to massage out the number.
79
+ #
80
+ # @param [String] number The string containing a potential number.
81
+ #
82
+ # @return [Integer]
61
83
  def calculate_cents(number)
62
84
  # remove anything that's not a number, potential delimiter, or minus sign
63
85
  num = number.gsub(/[^\d|\.|,|\'|\s|\-]/, '').strip
@@ -163,5 +185,4 @@ class String
163
185
  # if negative, multiply by -1; otherwise, return positive cents
164
186
  negative ? cents * -1 : cents
165
187
  end
166
-
167
188
  end
@@ -6,26 +6,23 @@ class Money
6
6
  class Currency
7
7
  include Comparable
8
8
 
9
+ # Thrown when an unknown currency is requested.
9
10
  class UnknownCurrency < StandardError; end
10
11
 
11
12
  # List of attributes applicable to a currency object.
12
- # * priority: a numerical value you can use to sort/group the currency list
13
- # * iso_code: the international 3-letter code as defined by the ISO 4217 standard
14
- # * name: the currency name
15
- # * symbol: the currency symbol (UTF-8 encoded)
16
- # * subunit: the name of the fractional monetary unit
17
- # * subunit_to_unit: the proportion between the unit and the subunit
18
13
  ATTRIBUTES = [ :priority, :iso_code, :name, :symbol, :subunit, :subunit_to_unit, :separator, :delimiter ]
19
14
 
15
+ # List of known currencies.
16
+ #
17
+ # == monetary unit
18
+ # The standard unit of value of a currency, as the dollar in the United States or the peso in Mexico.
19
+ # http://www.answers.com/topic/monetary-unit
20
+ # == fractional monetary unit, subunit
21
+ # A monetary unit that is valued at a fraction (usually one hundredth) of the basic monetary unit
22
+ # http://www.answers.com/topic/fractional-monetary-unit-subunit
23
+ #
24
+ # See http://en.wikipedia.org/wiki/List_of_circulating_currencies
20
25
  TABLE = {
21
- # monetary unit
22
- # The standard unit of value of a currency, as the dollar in the United States or the peso in Mexico.
23
- # http://www.answers.com/topic/monetary-unit
24
- # fractional monetary unit, subunit
25
- # A monetary unit that is valued at a fraction (usually one hundredth) of the basic monetary unit
26
- # http://www.answers.com/topic/fractional-monetary-unit-subunit
27
- #
28
- # See http://en.wikipedia.org/wiki/List_of_circulating_currencies
29
26
  :aed => { :priority => 100, :iso_code => "AED", :name => "United Arab Emirates Dirham", :symbol => "د.إ", :subunit => "Fils", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
30
27
  :afn => { :priority => 100, :iso_code => "AFN", :name => "Afghan Afghani", :symbol => "؋", :subunit => "Pul", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
31
28
  :all => { :priority => 100, :iso_code => "ALL", :name => "Albanian Lek", :symbol => "L", :subunit => "Qintar", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
@@ -76,7 +73,7 @@ class Money
76
73
  :fkp => { :priority => 100, :iso_code => "FKP", :name => "Falkland Pound", :symbol => "£", :subunit => "Penny", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
77
74
  :gbp => { :priority => 3, :iso_code => "GBP", :name => "British Pound", :symbol => "£", :subunit => "Penny", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
78
75
  :gel => { :priority => 100, :iso_code => "GEL", :name => "Georgian Lari", :symbol => "ლ", :subunit => "Tetri", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
79
- :ghc => { :priority => 100, :iso_code => "GHC", :name => "Ghanaian Cedi", :symbol => "₵", :subunit => "Pesewa", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
76
+ :ghs => { :priority => 100, :iso_code => "GHS", :name => "Ghanaian Cedi", :symbol => "₵", :subunit => "Pesewa", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
80
77
  :gip => { :priority => 100, :iso_code => "GIP", :name => "Gibraltar Pound", :symbol => "£", :subunit => "Penny", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
81
78
  :gmd => { :priority => 100, :iso_code => "GMD", :name => "Gambian Dalasi", :symbol => "D", :subunit => "Butut", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
82
79
  :gnf => { :priority => 100, :iso_code => "GNF", :name => "Guinean Franc", :symbol => "Fr", :subunit => "Centime", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
@@ -192,10 +189,66 @@ class Money
192
189
 
193
190
  # aliases for BC with documentation before Currency
194
191
  :yen => { :priority => 100, :iso_code => "JPY", :name => "Japanese Yen", :symbol => "¥", :subunit => "Sen", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
192
+
193
+ # kept for backwards compatibility, real entry is :ghs
194
+ :ghc => { :priority => 100, :iso_code => "GHS", :name => "Ghanaian Cedi", :symbol => "₵", :subunit => "Pesewa", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
195
195
  }
196
196
 
197
- attr_reader :id, *ATTRIBUTES
197
+ # The symbol used to identify the currency, usually the lowercase
198
+ # +iso_code+ attribute.
199
+ #
200
+ # @return [Symbol]
201
+ attr_reader :id
202
+
203
+ # A numerical value you can use to sort/group the currency list.
204
+ #
205
+ # @return [Integer]
206
+ attr_reader :priority
207
+
208
+ # The international 3-letter code as defined by the ISO 4217 standard.
209
+ #
210
+ # @return [String]
211
+ attr_reader :iso_code
212
+
213
+ # The currency name.
214
+ #
215
+ # @return [String]
216
+ attr_reader :name
217
+
218
+ # The currency symbol (UTF-8 encoded).
219
+ #
220
+ # @return [String]
221
+ attr_reader :symbol
222
+
223
+ # The name of the fractional monetary unit.
224
+ #
225
+ # @return [String]
226
+ attr_reader :subunit
227
+
228
+ # The proportion between the unit and the subunit
229
+ #
230
+ # @return [Integer]
231
+ attr_reader :subunit_to_unit
232
+
233
+ # The character used to separate the whole unit from the subunit.
234
+ #
235
+ # @return [String]
236
+ attr_reader :separator
237
+
238
+ # The character used to separate thousands grouping of the whole unit.
239
+ #
240
+ # @return [String]
241
+ attr_reader :delimiter
198
242
 
243
+ # Create a new +Currency+ object.
244
+ #
245
+ # @param [String, Symbol, #to_s] id Used to look into +TABLE+ and retrieve
246
+ # the applicable attributes.
247
+ #
248
+ # @return [Money::Currency]
249
+ #
250
+ # @example
251
+ # Money::Currency.new(:usd) #=> #<Money::Currency id: usd ...>
199
252
  def initialize(id)
200
253
  @id = id.to_s.downcase.to_sym
201
254
  data = TABLE[@id] || raise(UnknownCurrency, "Unknown currency `#{id}'")
@@ -204,74 +257,112 @@ class Money
204
257
  end
205
258
  end
206
259
 
207
- # Compares <tt>self</tt> with <tt>other_currency</tt>
208
- # against the value of <tt>priority</tt> attribute.
260
+ # Compares +self+ with +other_currency+ against the value of +priority+
261
+ # attribute.
262
+ #
263
+ # @param [Money::Currency] other_currency The currency to compare to.
264
+ #
265
+ # @return [-1,0,1] -1 if less than, 0 is equal to, 1 if greater than
266
+ #
267
+ # @example
268
+ # c1 = Money::Currency.new(:usd)
269
+ # c2 = Money::Currency.new(:jpy)
270
+ # c1 <=> c2 #=> 1
271
+ # c2 <=> c1 #=> -1
272
+ # c1 <=> c1 #=> 0
209
273
  def <=>(other_currency)
210
274
  self.priority <=> other_currency.priority
211
275
  end
212
276
 
213
- # Returns <tt>true</tt> if <tt>self.iso_code</tt>
214
- # is equal to <tt>other_currency.iso_code</tt>
277
+ # Compares +self+ with +other_currency+ and returns +true+ if the are the
278
+ # same or if their +id+ attributes match.
279
+ #
280
+ # @param [Money::Currency] other_currency The currency to compare to.
281
+ #
282
+ # @return [Boolean]
283
+ #
284
+ # @example
285
+ # c1 = Money::Currency.new(:usd)
286
+ # c2 = Money::Currency.new(:jpy)
287
+ # c1 == c1 #=> true
288
+ # c1 == c2 #=> false
215
289
  def ==(other_currency)
216
290
  self.equal?(other_currency) ||
217
291
  self.id == other_currency.id
218
292
  end
219
293
 
220
- # synonymous with #==
294
+ # Compares +self+ with +other_currency+ and returns +true+ if the are the
295
+ # same or if their +id+ attributes match.
296
+ #
297
+ # @param [Money::Currency] other_currency The currency to compare to.
298
+ #
299
+ # @return [Boolean]
300
+ #
301
+ # @example
302
+ # c1 = Money::Currency.new(:usd)
303
+ # c2 = Money::Currency.new(:jpy)
304
+ # c1.eql? c1 #=> true
305
+ # c1.eql? c2 #=> false
221
306
  def eql?(other_currency)
222
307
  self == other_currency
223
308
  end
224
309
 
225
- # Returns a Fixnum hash value based on the <tt>id</tt> attribute
226
- # in order to use functions like & (intersection), group_by, etc.
310
+ # Returns a Fixnum hash value based on the +id+ attribute in order to use
311
+ # functions like & (intersection), group_by, etc.
227
312
  #
228
- # [Currency.new(:usd), Currency.new(:eur)] & [Currency.new(:usd)]
229
- # # => [Currency.new(:usd)]
313
+ # @return [Fixnum]
230
314
  #
315
+ # @example
316
+ # Money::Currency.new(:usd).hash #=> 428936
231
317
  def hash
232
318
  id.hash
233
319
  end
234
320
 
235
- # Returns a string representation
236
- # corresponding to the upcase <tt>id</tt> attribute.
237
- #
238
- # Currency.new(:usd).to_s
239
- # # => "USD"
240
- # Currency.new(:eur).to_s
241
- # # => "EUR"
321
+ # Returns a string representation corresponding to the upcase +id+
322
+ # attribute.
242
323
  #
243
324
  # -–
244
- # DEV: id.to_s.upcase corresponds to iso_code
245
- # but don't use ISO_CODE for consistency.
325
+ # DEV: id.to_s.upcase corresponds to iso_code but don't use ISO_CODE for consistency.
326
+ #
327
+ # @return [String]
328
+ #
329
+ # @example
330
+ # Money::Currency.new(:usd).to_s #=> "USD"
331
+ # Money::Currency.new(:eur).to_s #=> "EUR"
246
332
  def to_s
247
333
  id.to_s.upcase
248
334
  end
249
335
 
250
336
  # Returns a human readable representation.
251
337
  #
252
- # #<Currency id:usd>
338
+ # @return [String]
253
339
  #
340
+ # @example
341
+ # Money::Currency.new(:usd) #=> #<Currency id: usd ...>
254
342
  def inspect
255
343
  "#<#{self.class.name} id: #{id} #{ATTRIBUTES.map { |a| "#{a}: #{send(a)}" }.join(", ")}>"
256
344
  end
257
345
 
258
-
346
+ # Catches deprecated uses of currency.
259
347
  def method_missing(method, *args, &block)
260
- Money.deprecate "`currency' is now a Currency instance. Call `currency.to_s.#{method}' instead."
348
+ Money.deprecate "`currency` is now a `Currency` instance. Call `currency.to_s.#{method}' instead."
261
349
  iso_code.send(method, *args, &block)
262
350
  end
263
351
 
352
+ # Class Methods
264
353
  class << self
265
354
 
266
- # Lookup a Currency with given <tt>id</tt>
267
- # an returns a <tt>Currency</tt> instance on success,
268
- # <tt>nil</tt> otherwise.
355
+ # Lookup a currency with given +id+ an returns a +Currency+ instance on
356
+ # success, +nil+ otherwise.
357
+ #
358
+ # @param [String, Symbol, #to_s] id Used to look into +TABLE+ and
359
+ # retrieve the applicable attributes.
269
360
  #
270
- # Currency.find(:eur)
271
- # # => <#Currency id: eur ...>
272
- # Currency.find(:foo)
273
- # # => nil
361
+ # @return [Money::Currency]
274
362
  #
363
+ # @example
364
+ # Money::Currency.find(:eur) #=> #<Money::Currency id: eur ...>
365
+ # Money::Currency.find(:foo) #=> nil
275
366
  def find(id)
276
367
  id = id.to_s.downcase.to_sym
277
368
  if data = self::TABLE[id]
@@ -279,15 +370,19 @@ class Money
279
370
  end
280
371
  end
281
372
 
282
- # Wraps the object in a Currency unless it's a Currency.
373
+ # Wraps the object in a +Currency+ unless it's already a +Currency+
374
+ # object.
375
+ #
376
+ # @param [Object] object The object to attempt and wrap as a +Currency+
377
+ # object.
283
378
  #
284
- # Currency.wrap(nil)
285
- # # => nil
286
- # Currency.wrap(Currency.new(:usd))
287
- # # => <#Currency id: usd ...>
288
- # Currency.wrap("usd")
289
- # # => <#Currency id: usd ...>
379
+ # @return [Money::Currency]
290
380
  #
381
+ # @example
382
+ # c1 = Money::Currency.new(:usd)
383
+ # Money::Currency.wrap(nil) #=> nil
384
+ # Money::Currency.wrap(c1) #=> #<Money::Currency id: usd ...>
385
+ # Money::Currency.wrap("usd") #=> #<Money::Currency id: usd ...>
291
386
  def wrap(object)
292
387
  if object.nil?
293
388
  nil
@@ -297,8 +392,6 @@ class Money
297
392
  Currency.new(object)
298
393
  end
299
394
  end
300
-
301
395
  end
302
-
303
396
  end
304
397
  end