money 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,9 @@
1
+ == Money 2.3.0
2
+ * Currency is now represented by a Currency Object instead of a string.
3
+
4
+ == Money 2.2.0
5
+ * Can now divide two Money objects by one another [#/]
6
+ * Can now convert a Money object to a float [#to_f]
7
+ * Users can now specify Seperators and Delimiters [#format]
8
+ * Support for Brazilian Real [Money.new(1_00, :BRL)]
9
+ * Migrated to Jeweler
data/README.rdoc CHANGED
@@ -4,10 +4,14 @@ This library aids one in handling money and different currencies. Features:
4
4
 
5
5
  - Provides a Money class which encapsulates all information about an certain
6
6
  amount of money, such as its value and its currency.
7
+ - Provies a Currency class which encapsulates all information about
8
+ a monerary unit.
7
9
  - Represents monetary values as integers, in cents. This avoids floating point
8
10
  rounding errors.
11
+ - Represents currency as Currency instances providing an high level of flexibility.
9
12
  - Provides APIs for exchanging money from one currency to another.
10
- - Has the ability to parse a money string into a Money object.
13
+ - Has the ability to parse a money and currency strings
14
+ into the corresponding Money/Currency object.
11
15
 
12
16
  Resources:
13
17
 
@@ -36,12 +40,98 @@ The development version (hosted on Github) can be installed with:
36
40
  # 10.00 USD
37
41
  money = Money.new(1000, "USD")
38
42
  money.cents # => 1000
39
- money.currency # => "USD"
43
+ money.currency # => Currency.new("USD")
40
44
 
41
45
  Money.new(1000, "USD") == Money.new(1000, "USD") # => true
42
46
  Money.new(1000, "USD") == Money.new(100, "USD") # => false
43
47
  Money.new(1000, "USD") == Money.new(1000, "EUR") # => false
44
48
 
49
+ === Currency
50
+
51
+ Currencies are consistently represented as instances of Money::Currency.
52
+ The most part of Money APIs allows you to supply either a String or a Currency.
53
+
54
+ Money.new(1000, "USD") == Money.new(900, Currency.new("USD"))
55
+ Money.new(1000, "EUR").currency == Currency.new("EUR")
56
+
57
+ A Money::Currency instance holds all the information about the currency,
58
+ including the currency symbol, name and much more.
59
+
60
+ currency = Money.new(1000, "USD")
61
+ currency.iso_code
62
+ # => "USD"
63
+ currency.name
64
+ # => "United States Dollar"
65
+
66
+ To define a new Money::Currency simply add a new item to the Money::Currency::TABLE hash,
67
+ where the key is the identifier for the Currency object and the value is a hash
68
+ containing all the currency attributes.
69
+
70
+ Money::Currency::TABLE[:usd] = {
71
+ :priority => 1,
72
+ :iso_code => "USD",
73
+ :name => "United States Dollar",
74
+ :symbol => "$",
75
+ :subunit => "Cent"
76
+ :subunit_to_unit => "100"
77
+ }
78
+
79
+ The pre-defined set of attributes includes:
80
+
81
+ * priority: a numerical value you can use to sort/group the currency list
82
+ * iso_code: the international 3-letter code as defined by the ISO 4217 standard
83
+ * name: the currency name
84
+ * symbol: the currency symbol (UTF-8 encoded)
85
+ * subunit: the name of the fractional monetary unit
86
+ * subunit_to_unit: the proportion between the unit and the subunit
87
+
88
+ All attributes are optional. Some attributes, such as :symbol, are used by the Money class
89
+ to print out a representation of the object. Other attributes, such as :name or :priority,
90
+ exist to provide a basic API you can take advantage of to build your application.
91
+
92
+ ==== priority
93
+
94
+ The priority attribute is an arbitrary numerical value you can assign to the Currency
95
+ and use in sorting/grouping operation.
96
+
97
+ For instance, let's assume your Rails application needs to a currency selector
98
+ like the one available at http://finance.yahoo.com/currency-converter/
99
+ You can create a couple of custom methods to return the list of major_currencies
100
+ and all_currencies as follows:
101
+
102
+ # Returns an array of currency id where
103
+ # priority < 10
104
+ def major_currencies(hash)
105
+ hash.inject([]) do |array, (id, attributes)|
106
+ priority = attributes[:priority]
107
+ if priority && priority < 10
108
+ array[priority] ||= []
109
+ array[priority] << id
110
+ end
111
+ array
112
+ end.compact.flatten
113
+ end
114
+
115
+ # Returns an array of all currency id
116
+ def all_currencies(hash)
117
+ hash.keys
118
+ end
119
+
120
+ major_currencies(Money::Currency::TABLE)
121
+ # => [ :usd, :eur, :bgp, :cad ]
122
+
123
+ major_currencies(Money::Currency::TABLE)
124
+ # => [ :aed, :afn, all, ... ]
125
+
126
+
127
+ === Default Currency
128
+
129
+ By default Money defaults to USD as its currency. This can be overwritten using
130
+
131
+ Money.default_currency = Money::Currency.new("CAD")
132
+
133
+ If you use Rails, then environment.rb is a very good place to put this.
134
+
45
135
  === Currency Exchange
46
136
 
47
137
  Exchanging money is performed through an exchange bank object. The default
@@ -87,11 +177,3 @@ field.
87
177
  validates_presence_of :sku, :currency
88
178
  validates_uniqueness_of :sku
89
179
  end
90
-
91
- === Default Currency
92
-
93
- By default Money defaults to USD as its currency. This can be overwritten using
94
-
95
- Money.default_currency = "CAD"
96
-
97
- If you use Rails, then environment.rb is a very good place to put this.
data/Rakefile CHANGED
@@ -1,5 +1,8 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
+ require 'rake/clean'
4
+
5
+ CLOBBER << 'pkg'
3
6
 
4
7
  begin
5
8
  require 'jeweler'
@@ -15,7 +18,9 @@ begin
15
18
  gem.add_development_dependency "hanna", ">= 0.1.12"
16
19
  end
17
20
  Jeweler::GemcutterTasks.new
18
- Jeweler::RubyforgeTasks.new
21
+ Jeweler::RubyforgeTasks.new do |gem|
22
+ gem.remote_doc_path = ""
23
+ end
19
24
  rescue LoadError
20
25
  puts "Jeweler not available. Install it with: gem install jeweler"
21
26
  end
@@ -38,7 +43,7 @@ Rake::RDocTask.new do |rdoc|
38
43
  rdoc.rdoc_dir = 'rdoc'
39
44
  rdoc.main = 'README.rdoc'
40
45
  rdoc.title = "money #{version}"
41
- rdoc.rdoc_files.include('README.rdoc', 'LICENSE')
46
+ rdoc.rdoc_files.include('README.rdoc', 'LICENSE', 'CHANGELOG.rdoc')
42
47
  rdoc.rdoc_files.include('lib/**/*.rb')
43
48
  rdoc.options << '-U'
44
49
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.2.0
1
+ 2.3.0
@@ -10,6 +10,7 @@ class Numeric
10
10
  end
11
11
 
12
12
  class String
13
+
13
14
  # Parses the current string and converts it to a Money object.
14
15
  # Excess characters will be discarded.
15
16
  #
@@ -26,7 +27,12 @@ class String
26
27
  cents = calculate_cents(self)
27
28
  Money.new(cents, currency)
28
29
  end
29
-
30
+
31
+ # Parses the current string and converts it to a Currency object.
32
+ def to_currency
33
+ Money::Currency.new(self)
34
+ end
35
+
30
36
  private
31
37
 
32
38
  def calculate_cents(number)
@@ -0,0 +1,289 @@
1
+ # encoding: utf-8
2
+
3
+ class Money
4
+
5
+ # Represents a specific currency unit.
6
+ class Currency
7
+ include Comparable
8
+
9
+ class UnknownCurrency < StandardError; end
10
+
11
+ # 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
+ ATTRIBUTES = [ :priority, :iso_code, :name, :symbol, :subunit, :subunit_to_unit ]
19
+
20
+ 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
+ :aed => { :priority => 100, :iso_code => "AED", :name => "United Arab Emirates Dirham", :symbol => "د.إ", :subunit => "Fils", :subunit_to_unit => "100" },
30
+ :afn => { :priority => 100, :iso_code => "AFN", :name => "Afghan Afghani", :symbol => "؋", :subunit => "Pul", :subunit_to_unit => "100" },
31
+ :all => { :priority => 100, :iso_code => "ALL", :name => "Albanian Lek", :symbol => "L", :subunit => "Qintar", :subunit_to_unit => "100" },
32
+ :amd => { :priority => 100, :iso_code => "AMD", :name => "Armenian Dram", :symbol => "դր.", :subunit => "Luma", :subunit_to_unit => "100" },
33
+ :ang => { :priority => 100, :iso_code => "ANG", :name => "Netherlands Antillean Gulden", :symbol => "ƒ", :subunit => "Cent", :subunit_to_unit => "100" },
34
+ :aoa => { :priority => 100, :iso_code => "AOA", :name => "Angolan Kwanza", :symbol => "Kz", :subunit => "Cêntimo", :subunit_to_unit => "100" },
35
+ :ars => { :priority => 100, :iso_code => "ARS", :name => "Argentine Peso", :symbol => "$", :subunit => "Centavo", :subunit_to_unit => "100" },
36
+ :aud => { :priority => 4, :iso_code => "AUD", :name => "Australian Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
37
+ :awg => { :priority => 100, :iso_code => "AWG", :name => "Aruban Florin", :symbol => "ƒ", :subunit => "Cent", :subunit_to_unit => "100" },
38
+ :azn => { :priority => 100, :iso_code => "AZN", :name => "Azerbaijani Manat", :symbol => nil, :subunit => "Qəpik", :subunit_to_unit => "100" },
39
+ :bam => { :priority => 100, :iso_code => "BAM", :name => "Bosnia and Herzegovina Convertible Mark", :symbol => "KM or КМ", :subunit => "Fening", :subunit_to_unit => "100" },
40
+ :bbd => { :priority => 100, :iso_code => "BBD", :name => "Barbadian Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
41
+ :bdt => { :priority => 100, :iso_code => "BDT", :name => "Bangladeshi Taka", :symbol => "৳", :subunit => "Paisa", :subunit_to_unit => "100" },
42
+ :bgn => { :priority => 100, :iso_code => "BGN", :name => "Bulgarian Lev", :symbol => "лв", :subunit => "Stotinka", :subunit_to_unit => "100" },
43
+ :bhd => { :priority => 100, :iso_code => "BHD", :name => "Bahraini Dinar", :symbol => "ب.د", :subunit => "Fils", :subunit_to_unit => "1,000" },
44
+ :bif => { :priority => 100, :iso_code => "BIF", :name => "Burundian Franc", :symbol => "Fr", :subunit => "Centime", :subunit_to_unit => "100" },
45
+ :bmd => { :priority => 100, :iso_code => "BMD", :name => "Bermudian Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
46
+ :bnd => { :priority => 100, :iso_code => "BND", :name => "Brunei Dollar", :symbol => "$", :subunit => "Sen", :subunit_to_unit => "100" },
47
+ :bob => { :priority => 100, :iso_code => "BOB", :name => "Bolivian Boliviano", :symbol => "Bs.", :subunit => "Centavo", :subunit_to_unit => "100" },
48
+ :brl => { :priority => 100, :iso_code => "BRL", :name => "Brazilian Real", :symbol => "R$ ", :subunit => "Centavo", :subunit_to_unit => "100" },
49
+ :bsd => { :priority => 100, :iso_code => "BSD", :name => "Bahamian Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
50
+ :btn => { :priority => 100, :iso_code => "BTN", :name => "Bhutanese Ngultrum", :symbol => nil, :subunit => "Chertrum", :subunit_to_unit => "100" },
51
+ :bwp => { :priority => 100, :iso_code => "BWP", :name => "Botswana Pula", :symbol => "P", :subunit => "Thebe", :subunit_to_unit => "100" },
52
+ :byr => { :priority => 100, :iso_code => "BYR", :name => "Belarusian Ruble", :symbol => "Br", :subunit => "Kapyeyka", :subunit_to_unit => "100" },
53
+ :bzd => { :priority => 100, :iso_code => "BZD", :name => "Belize Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
54
+ :cad => { :priority => 5, :iso_code => "CAD", :name => "Canadian Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
55
+ :cdf => { :priority => 100, :iso_code => "CDF", :name => "Congolese Franc", :symbol => "Fr", :subunit => "Centime", :subunit_to_unit => "100" },
56
+ :chf => { :priority => 100, :iso_code => "CHF", :name => "Swiss Franc", :symbol => "Fr", :subunit => "Rappen", :subunit_to_unit => "100" },
57
+ :chf => { :priority => 100, :iso_code => "CHF", :name => "Swiss Franc", :symbol => "Fr", :subunit => "Rappen", :subunit_to_unit => "100" },
58
+ :clp => { :priority => 100, :iso_code => "CLP", :name => "Chilean Peso", :symbol => "$", :subunit => "Centavo", :subunit_to_unit => "100" },
59
+ :cny => { :priority => 100, :iso_code => "CNY", :name => "Chinese Renminbi Yuan", :symbol => "¥", :subunit => "Jiao", :subunit_to_unit => "10" },
60
+ :cop => { :priority => 100, :iso_code => "COP", :name => "Colombian Peso", :symbol => "$", :subunit => "Centavo", :subunit_to_unit => "100" },
61
+ :crc => { :priority => 100, :iso_code => "CRC", :name => "Costa Rican Colón", :symbol => "₡", :subunit => "Céntimo", :subunit_to_unit => "100" },
62
+ :cuc => { :priority => 100, :iso_code => "CUC", :name => "Cuban Convertible Peso", :symbol => "$", :subunit => "Centavo", :subunit_to_unit => "100" },
63
+ :cup => { :priority => 100, :iso_code => "CUP", :name => "Cuban Peso", :symbol => "$", :subunit => "Centavo", :subunit_to_unit => "100" },
64
+ :cve => { :priority => 100, :iso_code => "CVE", :name => "Cape Verdean Escudo", :symbol => "$ or Esc", :subunit => "Centavo", :subunit_to_unit => "100" },
65
+ :czk => { :priority => 100, :iso_code => "CZK", :name => "Czech Koruna", :symbol => "Kč", :subunit => "Haléř", :subunit_to_unit => "100" },
66
+ :djf => { :priority => 100, :iso_code => "DJF", :name => "Djiboutian Franc", :symbol => "Fr", :subunit => "Centime", :subunit_to_unit => "100" },
67
+ :dkk => { :priority => 100, :iso_code => "DKK", :name => "Danish Krone", :symbol => "kr", :subunit => "Øre", :subunit_to_unit => "100" },
68
+ :dop => { :priority => 100, :iso_code => "DOP", :name => "Dominican Peso", :symbol => "$", :subunit => "Centavo", :subunit_to_unit => "100" },
69
+ :dzd => { :priority => 100, :iso_code => "DZD", :name => "Algerian Dinar", :symbol => "د.ج", :subunit => "Centime", :subunit_to_unit => "100" },
70
+ :eek => { :priority => 100, :iso_code => "EEK", :name => "Estonian Kroon", :symbol => "KR", :subunit => "Sent", :subunit_to_unit => "100" },
71
+ :egp => { :priority => 100, :iso_code => "EGP", :name => "Egyptian Pound", :symbol => "£ or ج.م", :subunit => "Piastre", :subunit_to_unit => "100" },
72
+ :ern => { :priority => 100, :iso_code => "ERN", :name => "Eritrean Nakfa", :symbol => "Nfk", :subunit => "Cent", :subunit_to_unit => "100" },
73
+ :etb => { :priority => 100, :iso_code => "ETB", :name => "Ethiopian Birr", :symbol => nil, :subunit => "Santim", :subunit_to_unit => "100" },
74
+ :eur => { :priority => 2, :iso_code => "EUR", :name => "Euro", :symbol => "€", :subunit => "Cent", :subunit_to_unit => "100" },
75
+ :fjd => { :priority => 100, :iso_code => "FJD", :name => "Fijian Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
76
+ :fkp => { :priority => 100, :iso_code => "FKP", :name => "Falkland Pound", :symbol => "£", :subunit => "Penny", :subunit_to_unit => "100" },
77
+ :gbp => { :priority => 3, :iso_code => "GBP", :name => "British Pound", :symbol => "£", :subunit => "Penny", :subunit_to_unit => "100" },
78
+ :gel => { :priority => 100, :iso_code => "GEL", :name => "Georgian Lari", :symbol => "ლ", :subunit => "Tetri", :subunit_to_unit => "100" },
79
+ :ghc => { :priority => 100, :iso_code => "GHC", :name => "Ghanaian Cedi", :symbol => "₵", :subunit => "Pesewa", :subunit_to_unit => "100" },
80
+ :gip => { :priority => 100, :iso_code => "GIP", :name => "Gibraltar Pound", :symbol => "£", :subunit => "Penny", :subunit_to_unit => "100" },
81
+ :gmd => { :priority => 100, :iso_code => "GMD", :name => "Gambian Dalasi", :symbol => "D", :subunit => "Butut", :subunit_to_unit => "100" },
82
+ :gnf => { :priority => 100, :iso_code => "GNF", :name => "Guinean Franc", :symbol => "Fr", :subunit => "Centime", :subunit_to_unit => "100" },
83
+ :gtq => { :priority => 100, :iso_code => "GTQ", :name => "Guatemalan Quetzal", :symbol => "Q", :subunit => "Centavo", :subunit_to_unit => "100" },
84
+ :gyd => { :priority => 100, :iso_code => "GYD", :name => "Guyanese Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
85
+ :hkd => { :priority => 100, :iso_code => "HKD", :name => "Hong Kong Dollar", :symbol => "$", :subunit => "Ho", :subunit_to_unit => "10" },
86
+ :hnl => { :priority => 100, :iso_code => "HNL", :name => "Honduran Lempira", :symbol => "L", :subunit => "Centavo", :subunit_to_unit => "100" },
87
+ :hrk => { :priority => 100, :iso_code => "HRK", :name => "Croatian Kuna", :symbol => "kn", :subunit => "Lipa", :subunit_to_unit => "100" },
88
+ :htg => { :priority => 100, :iso_code => "HTG", :name => "Haitian Gourde", :symbol => "G", :subunit => "Centime", :subunit_to_unit => "100" },
89
+ :huf => { :priority => 100, :iso_code => "HUF", :name => "Hungarian Forint", :symbol => "Ft", :subunit => "Fillér", :subunit_to_unit => "100" },
90
+ :idr => { :priority => 100, :iso_code => "IDR", :name => "Indonesian Rupiah", :symbol => "Rp", :subunit => "Sen", :subunit_to_unit => "100" },
91
+ :ils => { :priority => 100, :iso_code => "ILS", :name => "Israeli New Sheqel", :symbol => "₪", :subunit => "Agora", :subunit_to_unit => "100" },
92
+ :inr => { :priority => 100, :iso_code => "INR", :name => "Indian Rupee", :symbol => "₨", :subunit => "Paisa", :subunit_to_unit => "100" },
93
+ :iqd => { :priority => 100, :iso_code => "IQD", :name => "Iraqi Dinar", :symbol => "ع.د", :subunit => "Fils", :subunit_to_unit => "1,000" },
94
+ :irr => { :priority => 100, :iso_code => "IRR", :name => "Iranian Rial", :symbol => "﷼", :subunit => "Dinar", :subunit_to_unit => "100" },
95
+ :isk => { :priority => 100, :iso_code => "ISK", :name => "Icelandic Króna", :symbol => "kr", :subunit => "Eyrir", :subunit_to_unit => "100" },
96
+ :jmd => { :priority => 100, :iso_code => "JMD", :name => "Jamaican Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
97
+ :jod => { :priority => 100, :iso_code => "JOD", :name => "Jordanian Dinar", :symbol => "د.ا", :subunit => "Piastre", :subunit_to_unit => "100" },
98
+ :jpy => { :priority => 6, :iso_code => "JPY", :name => "Japanese Yen", :symbol => "¥", :subunit => "Sen", :subunit_to_unit => "100" },
99
+ :kes => { :priority => 100, :iso_code => "KES", :name => "Kenyan Shilling", :symbol => "Sh", :subunit => "Cent", :subunit_to_unit => "100" },
100
+ :kgs => { :priority => 100, :iso_code => "KGS", :name => "Kyrgyzstani Som", :symbol => nil, :subunit => "Tyiyn", :subunit_to_unit => "100" },
101
+ :khr => { :priority => 100, :iso_code => "KHR", :name => "Cambodian Riel", :symbol => "៛", :subunit => "Sen", :subunit_to_unit => "100" },
102
+ :kmf => { :priority => 100, :iso_code => "KMF", :name => "Comorian Franc", :symbol => "Fr", :subunit => "Centime", :subunit_to_unit => "100" },
103
+ :kpw => { :priority => 100, :iso_code => "KPW", :name => "North Korean Won", :symbol => "₩", :subunit => "Chŏn", :subunit_to_unit => "100" },
104
+ :krw => { :priority => 100, :iso_code => "KRW", :name => "South Korean Won", :symbol => "₩", :subunit => "Jeon", :subunit_to_unit => "100" },
105
+ :kwd => { :priority => 100, :iso_code => "KWD", :name => "Kuwaiti Dinar", :symbol => "د.ك", :subunit => "Fils", :subunit_to_unit => "1,000" },
106
+ :kyd => { :priority => 100, :iso_code => "KYD", :name => "Cayman Islands Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
107
+ :kzt => { :priority => 100, :iso_code => "KZT", :name => "Kazakhstani Tenge", :symbol => "〒", :subunit => "Tiyn", :subunit_to_unit => "100" },
108
+ :lak => { :priority => 100, :iso_code => "LAK", :name => "Lao Kip", :symbol => "₭", :subunit => "Att", :subunit_to_unit => "100" },
109
+ :lbp => { :priority => 100, :iso_code => "LBP", :name => "Lebanese Lira", :symbol => "ل.ل", :subunit => "Piastre", :subunit_to_unit => "100" },
110
+ :lkr => { :priority => 100, :iso_code => "LKR", :name => "Sri Lankan Rupee", :symbol => "₨", :subunit => "Cent", :subunit_to_unit => "100" },
111
+ :lrd => { :priority => 100, :iso_code => "LRD", :name => "Liberian Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
112
+ :lsl => { :priority => 100, :iso_code => "LSL", :name => "Lesotho Loti", :symbol => "L", :subunit => "Sente", :subunit_to_unit => "100" },
113
+ :ltl => { :priority => 100, :iso_code => "LTL", :name => "Lithuanian Litas", :symbol => "Lt", :subunit => "Centas", :subunit_to_unit => "100" },
114
+ :lvl => { :priority => 100, :iso_code => "LVL", :name => "Latvian Lats", :symbol => "Ls", :subunit => "Santīms", :subunit_to_unit => "100" },
115
+ :lyd => { :priority => 100, :iso_code => "LYD", :name => "Libyan Dinar", :symbol => "ل.د", :subunit => "Dirham", :subunit_to_unit => "1,000" },
116
+ :mad => { :priority => 100, :iso_code => "MAD", :name => "Moroccan Dirham", :symbol => "د.م.", :subunit => "Centime", :subunit_to_unit => "100" },
117
+ :mdl => { :priority => 100, :iso_code => "MDL", :name => "Moldovan Leu", :symbol => "L", :subunit => "Ban", :subunit_to_unit => "100" },
118
+ :mga => { :priority => 100, :iso_code => "MGA", :name => "Malagasy Ariary", :symbol => nil, :subunit => "Iraimbilanja", :subunit_to_unit => "5" },
119
+ :mkd => { :priority => 100, :iso_code => "MKD", :name => "Macedonian Denar", :symbol => "ден", :subunit => "Deni", :subunit_to_unit => "100" },
120
+ :mmk => { :priority => 100, :iso_code => "MMK", :name => "Myanmar Kyat", :symbol => "K", :subunit => "Pya", :subunit_to_unit => "100" },
121
+ :mnt => { :priority => 100, :iso_code => "MNT", :name => "Mongolian Tögrög", :symbol => "₮", :subunit => "Möngö", :subunit_to_unit => "100" },
122
+ :mop => { :priority => 100, :iso_code => "MOP", :name => "Macanese Pataca", :symbol => "P", :subunit => "Avo", :subunit_to_unit => "100" },
123
+ :mro => { :priority => 100, :iso_code => "MRO", :name => "Mauritanian Ouguiya", :symbol => "UM", :subunit => "Khoums", :subunit_to_unit => "5" },
124
+ :mur => { :priority => 100, :iso_code => "MUR", :name => "Mauritian Rupee", :symbol => "₨", :subunit => "Cent", :subunit_to_unit => "100" },
125
+ :mvr => { :priority => 100, :iso_code => "MVR", :name => "Maldivian Rufiyaa", :symbol => "ރ.", :subunit => "Laari", :subunit_to_unit => "100" },
126
+ :mwk => { :priority => 100, :iso_code => "MWK", :name => "Malawian Kwacha", :symbol => "MK", :subunit => "Tambala", :subunit_to_unit => "100" },
127
+ :mxn => { :priority => 100, :iso_code => "MXN", :name => "Mexican Peso", :symbol => "$", :subunit => "Centavo", :subunit_to_unit => "100" },
128
+ :myr => { :priority => 100, :iso_code => "MYR", :name => "Malaysian Ringgit", :symbol => "RM", :subunit => "Sen", :subunit_to_unit => "100" },
129
+ :mzn => { :priority => 100, :iso_code => "MZN", :name => "Mozambican Metical", :symbol => "MTn", :subunit => "Centavo", :subunit_to_unit => "100" },
130
+ :nad => { :priority => 100, :iso_code => "NAD", :name => "Namibian Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
131
+ :ngn => { :priority => 100, :iso_code => "NGN", :name => "Nigerian Naira", :symbol => "₦", :subunit => "Kobo", :subunit_to_unit => "100" },
132
+ :nio => { :priority => 100, :iso_code => "NIO", :name => "Nicaraguan Córdoba", :symbol => "C$", :subunit => "Centavo", :subunit_to_unit => "100" },
133
+ :nok => { :priority => 100, :iso_code => "NOK", :name => "Norwegian Krone", :symbol => "kr", :subunit => "Øre", :subunit_to_unit => "100" },
134
+ :npr => { :priority => 100, :iso_code => "NPR", :name => "Nepalese Rupee", :symbol => "₨", :subunit => "Paisa", :subunit_to_unit => "100" },
135
+ :nzd => { :priority => 100, :iso_code => "NZD", :name => "New Zealand Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
136
+ :omr => { :priority => 100, :iso_code => "OMR", :name => "Omani Rial", :symbol => "ر.ع.", :subunit => "Baisa", :subunit_to_unit => "1,000" },
137
+ :pab => { :priority => 100, :iso_code => "PAB", :name => "Panamanian Balboa", :symbol => "B/.", :subunit => "Centésimo", :subunit_to_unit => "100" },
138
+ :pen => { :priority => 100, :iso_code => "PEN", :name => "Peruvian Nuevo Sol", :symbol => "S/.", :subunit => "Céntimo", :subunit_to_unit => "100" },
139
+ :pgk => { :priority => 100, :iso_code => "PGK", :name => "Papua New Guinean Kina", :symbol => "K", :subunit => "Toea", :subunit_to_unit => "100" },
140
+ :php => { :priority => 100, :iso_code => "PHP", :name => "Philippine Peso", :symbol => "₱", :subunit => "Centavo", :subunit_to_unit => "100" },
141
+ :pkr => { :priority => 100, :iso_code => "PKR", :name => "Pakistani Rupee", :symbol => "₨", :subunit => "Paisa", :subunit_to_unit => "100" },
142
+ :pln => { :priority => 100, :iso_code => "PLN", :name => "Polish Złoty", :symbol => "zł", :subunit => "Grosz", :subunit_to_unit => "100" },
143
+ :pyg => { :priority => 100, :iso_code => "PYG", :name => "Paraguayan Guaraní", :symbol => "₲", :subunit => "Céntimo", :subunit_to_unit => "100" },
144
+ :qar => { :priority => 100, :iso_code => "QAR", :name => "Qatari Riyal", :symbol => "ر.ق", :subunit => "Dirham", :subunit_to_unit => "100" },
145
+ :ron => { :priority => 100, :iso_code => "RON", :name => "Romanian Leu", :symbol => "L", :subunit => "Ban", :subunit_to_unit => "100" },
146
+ :rsd => { :priority => 100, :iso_code => "RSD", :name => "Serbian Dinar", :symbol => "din. or дин.", :subunit => "Para", :subunit_to_unit => "100" },
147
+ :rub => { :priority => 100, :iso_code => "RUB", :name => "Russian Ruble", :symbol => "р.", :subunit => "Kopek", :subunit_to_unit => "100" },
148
+ :rub => { :priority => 100, :iso_code => "RUB", :name => "Russian Ruble", :symbol => "руб.", :subunit => "Kopek", :subunit_to_unit => "100" },
149
+ :rwf => { :priority => 100, :iso_code => "RWF", :name => "Rwandan Franc", :symbol => "Fr", :subunit => "Centime", :subunit_to_unit => "100" },
150
+ :sar => { :priority => 100, :iso_code => "SAR", :name => "Saudi Riyal", :symbol => "ر.س", :subunit => "Hallallah", :subunit_to_unit => "100" },
151
+ :sbd => { :priority => 100, :iso_code => "SBD", :name => "Solomon Islands Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
152
+ :scr => { :priority => 100, :iso_code => "SCR", :name => "Seychellois Rupee", :symbol => "₨", :subunit => "Cent", :subunit_to_unit => "100" },
153
+ :sdg => { :priority => 100, :iso_code => "SDG", :name => "Sudanese Pound", :symbol => "£", :subunit => "Piastre", :subunit_to_unit => "100" },
154
+ :sek => { :priority => 100, :iso_code => "SEK", :name => "Swedish Krona", :symbol => "kr", :subunit => "Öre", :subunit_to_unit => "100" },
155
+ :sgd => { :priority => 100, :iso_code => "SGD", :name => "Singapore Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
156
+ :shp => { :priority => 100, :iso_code => "SHP", :name => "Saint Helenian Pound", :symbol => "£", :subunit => "Penny", :subunit_to_unit => "100" },
157
+ :skk => { :priority => 100, :iso_code => "SKK", :name => "Slovak Koruna", :symbol => "Sk", :subunit => "Halier", :subunit_to_unit => "100" },
158
+ :sll => { :priority => 100, :iso_code => "SLL", :name => "Sierra Leonean Leone", :symbol => "Le", :subunit => "Cent", :subunit_to_unit => "100" },
159
+ :sos => { :priority => 100, :iso_code => "SOS", :name => "Somali Shilling", :symbol => "Sh", :subunit => "Cent", :subunit_to_unit => "100" },
160
+ :srd => { :priority => 100, :iso_code => "SRD", :name => "Surinamese Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
161
+ :std => { :priority => 100, :iso_code => "STD", :name => "São Tomé and Príncipe Dobra", :symbol => "Db", :subunit => "Cêntimo", :subunit_to_unit => "100" },
162
+ :svc => { :priority => 100, :iso_code => "SVC", :name => "Salvadoran Colón", :symbol => "₡", :subunit => "Centavo", :subunit_to_unit => "100" },
163
+ :syp => { :priority => 100, :iso_code => "SYP", :name => "Syrian Pound", :symbol => "£ or ل.س", :subunit => "Piastre", :subunit_to_unit => "100" },
164
+ :szl => { :priority => 100, :iso_code => "SZL", :name => "Swazi Lilangeni", :symbol => "L", :subunit => "Cent", :subunit_to_unit => "100" },
165
+ :thb => { :priority => 100, :iso_code => "THB", :name => "Thai Baht", :symbol => "฿", :subunit => "Satang", :subunit_to_unit => "100" },
166
+ :tjs => { :priority => 100, :iso_code => "TJS", :name => "Tajikistani Somoni", :symbol => "ЅМ", :subunit => "Diram", :subunit_to_unit => "100" },
167
+ :tmm => { :priority => 100, :iso_code => "TMM", :name => "Turkmenistani Manat", :symbol => "m", :subunit => "Tennesi", :subunit_to_unit => "100" },
168
+ :tnd => { :priority => 100, :iso_code => "TND", :name => "Tunisian Dinar", :symbol => "د.ت", :subunit => "Millime", :subunit_to_unit => "1,000" },
169
+ :top => { :priority => 100, :iso_code => "TOP", :name => "Tongan Paʻanga", :symbol => "T$", :subunit => "Seniti", :subunit_to_unit => "100" },
170
+ :try => { :priority => 100, :iso_code => "TRY", :name => "Turkish New Lira", :symbol => "YTL", :subunit => "New kuruş", :subunit_to_unit => "100" },
171
+ :try => { :priority => 100, :iso_code => "TRY", :name => "Turkish New Lira", :symbol => "₤", :subunit => "New kuruş", :subunit_to_unit => "100" },
172
+ :ttd => { :priority => 100, :iso_code => "TTD", :name => "Trinidad and Tobago Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
173
+ :twd => { :priority => 100, :iso_code => "TWD", :name => "New Taiwan Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
174
+ :tzs => { :priority => 100, :iso_code => "TZS", :name => "Tanzanian Shilling", :symbol => "Sh", :subunit => "Cent", :subunit_to_unit => "100" },
175
+ :uah => { :priority => 100, :iso_code => "UAH", :name => "Ukrainian Hryvnia", :symbol => "₴", :subunit => "Kopiyka", :subunit_to_unit => "100" },
176
+ :ugx => { :priority => 100, :iso_code => "UGX", :name => "Ugandan Shilling", :symbol => "Sh", :subunit => "Cent", :subunit_to_unit => "100" },
177
+ :usd => { :priority => 1, :iso_code => "USD", :name => "United States Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
178
+ :uyu => { :priority => 100, :iso_code => "UYU", :name => "Uruguayan Peso", :symbol => "$", :subunit => "Centésimo", :subunit_to_unit => "100" },
179
+ :uzs => { :priority => 100, :iso_code => "UZS", :name => "Uzbekistani Som", :symbol => nil, :subunit => "Tiyin", :subunit_to_unit => "100" },
180
+ :vef => { :priority => 100, :iso_code => "VEF", :name => "Venezuelan Bolívar", :symbol => "Bs F", :subunit => "Céntimo", :subunit_to_unit => "100" },
181
+ :vnd => { :priority => 100, :iso_code => "VND", :name => "Vietnamese Đồng", :symbol => "₫", :subunit => "Hào", :subunit_to_unit => "10" },
182
+ :vuv => { :priority => 100, :iso_code => "VUV", :name => "Vanuatu Vatu", :symbol => "Vt", :subunit => nil, :subunit_to_unit => nil },
183
+ :wst => { :priority => 100, :iso_code => "WST", :name => "Samoan Tala", :symbol => "T", :subunit => "Sene", :subunit_to_unit => "100" },
184
+ :xaf => { :priority => 100, :iso_code => "XAF", :name => "Central African Cfa Franc", :symbol => "Fr", :subunit => "Centime", :subunit_to_unit => "100" },
185
+ :xcd => { :priority => 100, :iso_code => "XCD", :name => "East Caribbean Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
186
+ :xof => { :priority => 100, :iso_code => "XOF", :name => "West African Cfa Franc", :symbol => "Fr", :subunit => "Centime", :subunit_to_unit => "100" },
187
+ :xpf => { :priority => 100, :iso_code => "XPF", :name => "Cfp Franc", :symbol => "Fr", :subunit => "Centime", :subunit_to_unit => "100" },
188
+ :yer => { :priority => 100, :iso_code => "YER", :name => "Yemeni Rial", :symbol => "﷼", :subunit => "Fils", :subunit_to_unit => "100" },
189
+ :zar => { :priority => 100, :iso_code => "ZAR", :name => "South African Rand", :symbol => "R", :subunit => "Cent", :subunit_to_unit => "100" },
190
+ :zmk => { :priority => 100, :iso_code => "ZMK", :name => "Zambian Kwacha", :symbol => "ZK", :subunit => "Ngwee", :subunit_to_unit => "100" },
191
+ :zwd => { :priority => 100, :iso_code => "ZWD", :name => "Zimbabwean Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => "100" },
192
+
193
+ # aliases for BC with documentation before Currency
194
+ :yen => { :priority => 100, :iso_code => "JPY", :name => "Japanese Yen", :symbol => "¥", :subunit => "Sen", :subunit_to_unit => "100" },
195
+ }
196
+
197
+ attr_reader :id, *ATTRIBUTES
198
+
199
+ def initialize(id)
200
+ @id = id.to_s.downcase.to_sym
201
+ data = TABLE[@id] || raise(UnknownCurrency, "Unknown currency `#{id}'")
202
+ ATTRIBUTES.each do |attribute|
203
+ instance_variable_set(:"@#{attribute}", data[attribute])
204
+ end
205
+ end
206
+
207
+ # Compares <tt>self</tt> with <tt>other_currency</tt>
208
+ # against the value of <tt>priority</tt> attribute.
209
+ def <=>(other_currency)
210
+ self.priority <=> other_currency.priority
211
+ end
212
+
213
+ # Returns <tt>true</tt> if <tt>self.iso_code</tt>
214
+ # is equal to <tt>other_currency.iso_code</tt>
215
+ def ==(other_currency)
216
+ self.equal?(other_currency) ||
217
+ self.id == other_currency.id
218
+ end
219
+
220
+ # Returns a string representation
221
+ # corresponding to the upcase <tt>id</tt> attribute.
222
+ #
223
+ # Currency.new(:usd).to_s
224
+ # # => "USD"
225
+ # Currency.new(:eur).to_s
226
+ # # => "EUR"
227
+ #
228
+ # -–
229
+ # DEV: id.to_s.upcase corresponds to iso_code
230
+ # but don't use ISO_CODE for consistency.
231
+ def to_s
232
+ id.to_s.upcase
233
+ end
234
+
235
+ # Returns a human readable representation.
236
+ #
237
+ # #<Currency id:usd>
238
+ #
239
+ def inspect
240
+ "#<#{self.class.name} id: #{id} #{ATTRIBUTES.map { |a| "#{a}: #{send(a)}" }.join(", ")}>"
241
+ end
242
+
243
+
244
+ def method_missing(method, *args, &block)
245
+ warn "DEPRECATION MESSAGE: `currency' is now a Currency instance. Call `currency.to_s.#{method}' instead."
246
+ iso_code.send(method, *args, &block)
247
+ end
248
+
249
+ class << self
250
+
251
+ # Lookup a Currency with given <tt>id</tt>
252
+ # an returns a <tt>Currency</tt> instance on success,
253
+ # <tt>nil</tt> otherwise.
254
+ #
255
+ # Currency.find(:eur)
256
+ # # => <#Currency id: eur ...>
257
+ # Currency.find(:foo)
258
+ # # => nil
259
+ #
260
+ def find(id)
261
+ id = id.to_s.downcase.to_sym
262
+ if data = self::TABLE[id]
263
+ new(id)
264
+ end
265
+ end
266
+
267
+ # Wraps the object in a Currency unless it's a Currency.
268
+ #
269
+ # Currency.wrap(nil)
270
+ # # => nil
271
+ # Currency.wrap(Currency.new(:usd))
272
+ # # => <#Currency id: usd ...>
273
+ # Currency.wrap("usd")
274
+ # # => <#Currency id: usd ...>
275
+ #
276
+ def wrap(object)
277
+ if object.nil?
278
+ nil
279
+ elsif object.is_a?(Currency)
280
+ object
281
+ else
282
+ Currency.new(object)
283
+ end
284
+ end
285
+
286
+ end
287
+
288
+ end
289
+ end