money 3.7.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. data/CHANGELOG.md +384 -351
  2. data/LICENSE +21 -21
  3. data/README.md +243 -214
  4. data/Rakefile +49 -49
  5. data/lib/money.rb +28 -27
  6. data/lib/money/bank/base.rb +131 -131
  7. data/lib/money/bank/variable_exchange.rb +252 -252
  8. data/lib/money/core_extensions.rb +82 -82
  9. data/lib/money/currency.rb +263 -422
  10. data/lib/money/currency_loader.rb +19 -0
  11. data/lib/money/money.rb +405 -405
  12. data/lib/money/money/arithmetic.rb +246 -246
  13. data/lib/money/money/formatting.rb +260 -244
  14. data/lib/money/money/parsing.rb +350 -350
  15. data/money.gemspec +29 -35
  16. data/spec/bank/base_spec.rb +72 -72
  17. data/spec/bank/variable_exchange_spec.rb +238 -238
  18. data/spec/core_extensions_spec.rb +158 -158
  19. data/spec/currency_spec.rb +120 -133
  20. data/spec/money/arithmetic_spec.rb +479 -479
  21. data/spec/money/formatting_spec.rb +383 -375
  22. data/spec/money/parsing_spec.rb +197 -197
  23. data/spec/money_spec.rb +292 -292
  24. data/spec/spec_helper.rb +28 -28
  25. metadata +54 -126
  26. data/lib/money.rbc +0 -184
  27. data/lib/money/bank/base.rbc +0 -818
  28. data/lib/money/bank/variable_exchange.rbc +0 -2550
  29. data/lib/money/core_extensions.rbc +0 -664
  30. data/lib/money/currency.rbc +0 -22708
  31. data/lib/money/money.rbc +0 -3861
  32. data/lib/money/money/arithmetic.rbc +0 -2778
  33. data/lib/money/money/formatting.rbc +0 -2265
  34. data/lib/money/money/parsing.rbc +0 -2737
  35. data/spec/bank/base_spec.rbc +0 -2461
  36. data/spec/bank/variable_exchange_spec.rbc +0 -7541
  37. data/spec/core_extensions_spec.rbc +0 -5921
  38. data/spec/currency_spec.rbc +0 -4535
  39. data/spec/money/arithmetic_spec.rbc +0 -25140
  40. data/spec/money/formatting_spec.rbc +0 -12545
  41. data/spec/money/parsing_spec.rbc +0 -6511
  42. data/spec/money_spec.rbc +0 -9824
  43. data/spec/spec_helper.rbc +0 -575
@@ -1,158 +1,158 @@
1
- require "spec_helper"
2
-
3
- describe "Money core extensions" do
4
-
5
- describe Numeric do
6
- describe "#to_money" do
7
- it "should work" do
8
- money = 1234.to_money
9
- money.cents.should == 1234_00
10
- money.currency.should == Money.default_currency
11
-
12
- money = 100.37.to_money
13
- money.cents.should == 100_37
14
- money.currency.should == Money.default_currency
15
-
16
- money = BigDecimal.new('1234').to_money
17
- money.cents.should == 1234_00
18
- money.currency.should == Money.default_currency
19
- end
20
-
21
- it "should accept optional currency" do
22
- 1234.to_money('USD').should == Money.new(123400, 'USD')
23
- 1234.to_money('EUR').should == Money.new(123400, 'EUR')
24
- end
25
-
26
- it "should respect :subunit_to_unit currency property" do
27
- 10.to_money('USD').should == Money.new(10_00, 'USD')
28
- 10.to_money('TND').should == Money.new(10_000, 'TND')
29
- 10.to_money('CLP').should == Money.new(10, 'CLP')
30
- end
31
-
32
- specify "#issue/15" do
33
- amount = 555.55.to_money
34
- amount.should == Money.new(55555)
35
- end
36
- end
37
- end
38
-
39
- describe String do
40
- describe "#to_money" do
41
-
42
- StringToMoney = {
43
- "20.15" => Money.new(20_15) ,
44
- "100" => Money.new(100_00) ,
45
- "100.37" => Money.new(100_37) ,
46
- "100,37" => Money.new(100_37) ,
47
- "100 000" => Money.new(100_000_00) ,
48
- "100,000.00" => Money.new(100_000_00) ,
49
- "1,000" => Money.new(1_000_00) ,
50
- "-1,000" => Money.new(-1_000_00) ,
51
- "1,000.5" => Money.new(1_000_50) ,
52
- "1,000.51" => Money.new(1_000_51) ,
53
- "1,000.505" => Money.new(1_000_51) ,
54
- "1,000.504" => Money.new(1_000_50) ,
55
- "1,000.0000" => Money.new(1_000_00) ,
56
- "1,000.5000" => Money.new(1_000_50) ,
57
- "1,000.5099" => Money.new(1_000_51) ,
58
- "1.550" => Money.new(1_55) ,
59
- "25." => Money.new(25_00) ,
60
- ".75" => Money.new(75) ,
61
-
62
- "100 USD" => Money.new(100_00, "USD") ,
63
- "-100 USD" => Money.new(-100_00, "USD") ,
64
- "100 EUR" => Money.new(100_00, "EUR") ,
65
- "100.37 EUR" => Money.new(100_37, "EUR") ,
66
- "100,37 EUR" => Money.new(100_37, "EUR") ,
67
- "100,000.00 USD" => Money.new(100_000_00, "USD") ,
68
- "100.000,00 EUR" => Money.new(100_000_00, "EUR") ,
69
- "1,000 USD" => Money.new(1_000_00, "USD") ,
70
- "-1,000 USD" => Money.new(-1_000_00, "USD") ,
71
- "1,000.5500 USD" => Money.new(1_000_55, "USD") ,
72
- "-1,000.6500 USD" => Money.new(-1_000_65, "USD") ,
73
- "1.550 USD" => Money.new(1_55, "USD") ,
74
-
75
- "USD 100" => Money.new(100_00, "USD") ,
76
- "EUR 100" => Money.new(100_00, "EUR") ,
77
- "EUR 100.37" => Money.new(100_37, "EUR") ,
78
- "CAD -100.37" => Money.new(-100_37, "CAD") ,
79
- "EUR 100,37" => Money.new(100_37, "EUR") ,
80
- "EUR -100,37" => Money.new(-100_37, "EUR") ,
81
- "USD 100,000.00" => Money.new(100_000_00, "USD") ,
82
- "EUR 100.000,00" => Money.new(100_000_00, "EUR") ,
83
- "USD 1,000" => Money.new(1_000_00, "USD") ,
84
- "USD -1,000" => Money.new(-1_000_00, "USD") ,
85
- "USD 1,000.9000" => Money.new(1_000_90, "USD") ,
86
- "USD -1,000.090" => Money.new(-1_000_09, "USD") ,
87
- "USD 1.5500" => Money.new(1_55, "USD") ,
88
-
89
- "$100 USD" => Money.new(100_00, "USD") ,
90
- "$1,194.59 USD" => Money.new(1_194_59, "USD") ,
91
- "$-1,955 USD" => Money.new(-1_955_00, "USD") ,
92
- "$1,194.5900 USD" => Money.new(1_194_59, "USD") ,
93
- "$-1,955.000 USD" => Money.new(-1_955_00, "USD") ,
94
- "$1.99000 USD" => Money.new(1_99, "USD") ,
95
- }
96
-
97
- specify "it should work" do
98
- StringToMoney.each do |string, money|
99
- string.to_money.should == money
100
- end
101
- end
102
-
103
- it "should coerce input to string" do
104
- Money.parse(20, "USD").should == Money.new(20_00, "USD")
105
- end
106
-
107
- it "should accept optional currency" do
108
- "10.10".to_money('USD').should == Money.new(1010, 'USD')
109
- "10.10".to_money('EUR').should == Money.new(1010, 'EUR')
110
- "10.10 USD".to_money('USD').should == Money.new(1010, 'USD')
111
- end
112
-
113
- it "should raise error if optional currency doesn't match string currency" do
114
- lambda{ "10.10 USD".to_money('EUR') }.should raise_error(/Mismatching Currencies/)
115
- end
116
-
117
- it "should ignore unrecognized data" do
118
- "hello 2000 world".to_money.should == Money.new(2000_00)
119
- end
120
-
121
- it "should respect :subunit_to_unit currency property" do
122
- "1".to_money("USD").should == Money.new(1_00, "USD")
123
- "1".to_money("TND").should == Money.new(1_000, "TND")
124
- "1".to_money("CLP").should == Money.new(1, "CLP")
125
- "1.5".to_money("KWD").cents.should == 1500
126
- end
127
- end
128
-
129
- describe "#to_currency" do
130
- it "should convert string to Currency" do
131
- "USD".to_currency.should == Money::Currency.new(:usd)
132
- "EUR".to_currency.should == Money::Currency.new(:eur)
133
- end
134
-
135
- it "should raise Money::Currency::UnknownCurrency with unknown Currency" do
136
- lambda { "XXX".to_currency }.should raise_error(Money::Currency::UnknownCurrency)
137
- lambda { " ".to_currency }.should raise_error(Money::Currency::UnknownCurrency)
138
- end
139
- end
140
- end
141
-
142
- describe Symbol do
143
- describe "#to_currency" do
144
- it "should convert symbol to Currency" do
145
- :usd.to_currency.should == Money::Currency.new(:usd)
146
- :ars.to_currency.should == Money::Currency.new(:ars)
147
- end
148
- it "should work case-insensitive" do
149
- :EUR.to_currency.should == Money::Currency.new(:eur)
150
- end
151
- it "should raise Money::Currency::UnknownCurrency with unknown Currency" do
152
- lambda { :XXX.to_currency }.should raise_error(Money::Currency::UnknownCurrency)
153
- lambda { :" ".to_currency }.should raise_error(Money::Currency::UnknownCurrency)
154
- end
155
- end
156
- end
157
-
158
- end
1
+ require "spec_helper"
2
+
3
+ describe "Money core extensions" do
4
+
5
+ describe Numeric do
6
+ describe "#to_money" do
7
+ it "should work" do
8
+ money = 1234.to_money
9
+ money.cents.should == 1234_00
10
+ money.currency.should == Money.default_currency
11
+
12
+ money = 100.37.to_money
13
+ money.cents.should == 100_37
14
+ money.currency.should == Money.default_currency
15
+
16
+ money = BigDecimal.new('1234').to_money
17
+ money.cents.should == 1234_00
18
+ money.currency.should == Money.default_currency
19
+ end
20
+
21
+ it "should accept optional currency" do
22
+ 1234.to_money('USD').should == Money.new(123400, 'USD')
23
+ 1234.to_money('EUR').should == Money.new(123400, 'EUR')
24
+ end
25
+
26
+ it "should respect :subunit_to_unit currency property" do
27
+ 10.to_money('USD').should == Money.new(10_00, 'USD')
28
+ 10.to_money('TND').should == Money.new(10_000, 'TND')
29
+ 10.to_money('CLP').should == Money.new(10, 'CLP')
30
+ end
31
+
32
+ specify "#issue/15" do
33
+ amount = 555.55.to_money
34
+ amount.should == Money.new(55555)
35
+ end
36
+ end
37
+ end
38
+
39
+ describe String do
40
+ describe "#to_money" do
41
+
42
+ StringToMoney = {
43
+ "20.15" => Money.new(20_15) ,
44
+ "100" => Money.new(100_00) ,
45
+ "100.37" => Money.new(100_37) ,
46
+ "100,37" => Money.new(100_37) ,
47
+ "100 000" => Money.new(100_000_00) ,
48
+ "100,000.00" => Money.new(100_000_00) ,
49
+ "1,000" => Money.new(1_000_00) ,
50
+ "-1,000" => Money.new(-1_000_00) ,
51
+ "1,000.5" => Money.new(1_000_50) ,
52
+ "1,000.51" => Money.new(1_000_51) ,
53
+ "1,000.505" => Money.new(1_000_51) ,
54
+ "1,000.504" => Money.new(1_000_50) ,
55
+ "1,000.0000" => Money.new(1_000_00) ,
56
+ "1,000.5000" => Money.new(1_000_50) ,
57
+ "1,000.5099" => Money.new(1_000_51) ,
58
+ "1.550" => Money.new(1_55) ,
59
+ "25." => Money.new(25_00) ,
60
+ ".75" => Money.new(75) ,
61
+
62
+ "100 USD" => Money.new(100_00, "USD") ,
63
+ "-100 USD" => Money.new(-100_00, "USD") ,
64
+ "100 EUR" => Money.new(100_00, "EUR") ,
65
+ "100.37 EUR" => Money.new(100_37, "EUR") ,
66
+ "100,37 EUR" => Money.new(100_37, "EUR") ,
67
+ "100,000.00 USD" => Money.new(100_000_00, "USD") ,
68
+ "100.000,00 EUR" => Money.new(100_000_00, "EUR") ,
69
+ "1,000 USD" => Money.new(1_000_00, "USD") ,
70
+ "-1,000 USD" => Money.new(-1_000_00, "USD") ,
71
+ "1,000.5500 USD" => Money.new(1_000_55, "USD") ,
72
+ "-1,000.6500 USD" => Money.new(-1_000_65, "USD") ,
73
+ "1.550 USD" => Money.new(1_55, "USD") ,
74
+
75
+ "USD 100" => Money.new(100_00, "USD") ,
76
+ "EUR 100" => Money.new(100_00, "EUR") ,
77
+ "EUR 100.37" => Money.new(100_37, "EUR") ,
78
+ "CAD -100.37" => Money.new(-100_37, "CAD") ,
79
+ "EUR 100,37" => Money.new(100_37, "EUR") ,
80
+ "EUR -100,37" => Money.new(-100_37, "EUR") ,
81
+ "USD 100,000.00" => Money.new(100_000_00, "USD") ,
82
+ "EUR 100.000,00" => Money.new(100_000_00, "EUR") ,
83
+ "USD 1,000" => Money.new(1_000_00, "USD") ,
84
+ "USD -1,000" => Money.new(-1_000_00, "USD") ,
85
+ "USD 1,000.9000" => Money.new(1_000_90, "USD") ,
86
+ "USD -1,000.090" => Money.new(-1_000_09, "USD") ,
87
+ "USD 1.5500" => Money.new(1_55, "USD") ,
88
+
89
+ "$100 USD" => Money.new(100_00, "USD") ,
90
+ "$1,194.59 USD" => Money.new(1_194_59, "USD") ,
91
+ "$-1,955 USD" => Money.new(-1_955_00, "USD") ,
92
+ "$1,194.5900 USD" => Money.new(1_194_59, "USD") ,
93
+ "$-1,955.000 USD" => Money.new(-1_955_00, "USD") ,
94
+ "$1.99000 USD" => Money.new(1_99, "USD") ,
95
+ }
96
+
97
+ specify "it should work" do
98
+ StringToMoney.each do |string, money|
99
+ string.to_money.should == money
100
+ end
101
+ end
102
+
103
+ it "should coerce input to string" do
104
+ Money.parse(20, "USD").should == Money.new(20_00, "USD")
105
+ end
106
+
107
+ it "should accept optional currency" do
108
+ "10.10".to_money('USD').should == Money.new(1010, 'USD')
109
+ "10.10".to_money('EUR').should == Money.new(1010, 'EUR')
110
+ "10.10 USD".to_money('USD').should == Money.new(1010, 'USD')
111
+ end
112
+
113
+ it "should raise error if optional currency doesn't match string currency" do
114
+ lambda{ "10.10 USD".to_money('EUR') }.should raise_error(/Mismatching Currencies/)
115
+ end
116
+
117
+ it "should ignore unrecognized data" do
118
+ "hello 2000 world".to_money.should == Money.new(2000_00)
119
+ end
120
+
121
+ it "should respect :subunit_to_unit currency property" do
122
+ "1".to_money("USD").should == Money.new(1_00, "USD")
123
+ "1".to_money("TND").should == Money.new(1_000, "TND")
124
+ "1".to_money("CLP").should == Money.new(1, "CLP")
125
+ "1.5".to_money("KWD").cents.should == 1500
126
+ end
127
+ end
128
+
129
+ describe "#to_currency" do
130
+ it "should convert string to Currency" do
131
+ "USD".to_currency.should == Money::Currency.new(:usd)
132
+ "EUR".to_currency.should == Money::Currency.new(:eur)
133
+ end
134
+
135
+ it "should raise Money::Currency::UnknownCurrency with unknown Currency" do
136
+ lambda { "XXX".to_currency }.should raise_error(Money::Currency::UnknownCurrency)
137
+ lambda { " ".to_currency }.should raise_error(Money::Currency::UnknownCurrency)
138
+ end
139
+ end
140
+ end
141
+
142
+ describe Symbol do
143
+ describe "#to_currency" do
144
+ it "should convert symbol to Currency" do
145
+ :usd.to_currency.should == Money::Currency.new(:usd)
146
+ :ars.to_currency.should == Money::Currency.new(:ars)
147
+ end
148
+ it "should work case-insensitive" do
149
+ :EUR.to_currency.should == Money::Currency.new(:eur)
150
+ end
151
+ it "should raise Money::Currency::UnknownCurrency with unknown Currency" do
152
+ lambda { :XXX.to_currency }.should raise_error(Money::Currency::UnknownCurrency)
153
+ lambda { :" ".to_currency }.should raise_error(Money::Currency::UnknownCurrency)
154
+ end
155
+ end
156
+ end
157
+
158
+ end
@@ -1,133 +1,120 @@
1
- # encoding: utf-8
2
-
3
- require "spec_helper"
4
-
5
- describe Money::Currency do
6
-
7
- specify "#initialize should lookup data from TABLE" do
8
- with_custom_definitions do
9
- Money::Currency::TABLE[:usd] = {:priority => 1, :iso_code => "USD", :name => "United States Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => 100, :decimal_mark => ".", :thousands_separator => ","}
10
- Money::Currency::TABLE[:eur] = {:priority => 2, :iso_code => "EUR", :name => "Euro", :symbol => "€", :subunit => "Cent", :subunit_to_unit => 100, :decimal_mark => ".", :thousands_separator => ","}
11
-
12
- currency = Money::Currency.new("USD")
13
- currency.id.should == :usd
14
- currency.priority.should == 1
15
- currency.iso_code.should == "USD"
16
- currency.name.should == "United States Dollar"
17
- currency.decimal_mark.should == "."
18
- currency.separator.should == "."
19
- currency.thousands_separator.should == ","
20
- currency.delimiter.should == ","
21
- end
22
- end
23
-
24
- specify "#initialize should raise UnknownMoney::Currency with unknown currency" do
25
- lambda { Money::Currency.new("xxx") }.should raise_error(Money::Currency::UnknownCurrency, /xxx/)
26
- end
27
-
28
- specify "#== should return true if self === other" do
29
- currency = Money::Currency.new(:eur)
30
- currency.should == currency
31
- end
32
-
33
- specify "#== should return true if the id is equal" do
34
- Money::Currency.new(:eur).should == Money::Currency.new(:eur)
35
- Money::Currency.new(:eur).should_not == Money::Currency.new(:usd)
36
- end
37
-
38
- specify "#eql? should return true if #== returns true" do
39
- Money::Currency.new(:eur).eql?(Money::Currency.new(:eur)).should be true
40
- Money::Currency.new(:eur).eql?(Money::Currency.new(:usd)).should be false
41
- end
42
-
43
- specify "#hash should return the same value for equal objects" do
44
- Money::Currency.new(:eur).hash.should == Money::Currency.new(:eur).hash
45
- Money::Currency.new(:eur).hash.should_not == Money::Currency.new(:usd).hash
46
- end
47
-
48
- specify "#hash can be used to return the intersection of Currency object arrays" do
49
- intersection = [Money::Currency.new(:eur), Money::Currency.new(:usd)] & [Money::Currency.new(:eur)]
50
- intersection.should == [Money::Currency.new(:eur)]
51
- end
52
-
53
- specify "#<=> should compare objects by priority" do
54
- Money::Currency.new(:cad).should > Money::Currency.new(:usd)
55
- Money::Currency.new(:usd).should < Money::Currency.new(:eur)
56
- end
57
-
58
- specify "#to_s" do
59
- Money::Currency.new(:usd).to_s.should == "USD"
60
- Money::Currency.new(:eur).to_s.should == "EUR"
61
- end
62
-
63
- specify "#to_currency" do
64
- usd = Money::Currency.new(:usd)
65
- usd.to_currency.should == usd
66
- end
67
-
68
- specify "#inspect" do
69
- Money::Currency.new(:usd).inspect.should ==
70
- %Q{#<Money::Currency id: usd, priority: 1, symbol_first: true, thousands_separator: ,, html_entity: $, decimal_mark: ., name: United States Dollar, symbol: $, subunit_to_unit: 100, iso_code: USD, subunit: Cent>}
71
- end
72
-
73
-
74
- specify "#self.find should return currency matching given id" do
75
- with_custom_definitions do
76
- Money::Currency::TABLE[:usd] = { :priority => 1, :iso_code => "USD", :name => "United States Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => 100, :decimal_mark => ".", :thousands_separator => "," }
77
- Money::Currency::TABLE[:eur] = { :priority => 2, :iso_code => "EUR", :name => "Euro", :symbol => "€", :subunit => "Cent", :subunit_to_unit => 100, :decimal_mark => ".", :thousands_separator => "," }
78
-
79
- expected = Money::Currency.new(:eur)
80
- Money::Currency.find(:eur).should == expected
81
- Money::Currency.find(:EUR).should == expected
82
- Money::Currency.find("eur").should == expected
83
- Money::Currency.find("EUR").should == expected
84
- end
85
- end
86
-
87
- specify "#self.find should return nil unless currency matching given id" do
88
- with_custom_definitions do
89
- Money::Currency::TABLE[:usd] = { :position => 1, :iso_code => "USD", :name => "United States Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => 100, :decimal_mark => ".", :thousands_separator => "," }
90
- Money::Currency::TABLE[:eur] = { :position => 2, :iso_code => "EUR", :name => "Euro", :symbol => "€", :subunit => "Cent", :subunit_to_unit => 100, :decimal_mark => ".", :thousands_separator => "," }
91
-
92
- expected = Money::Currency.new(:eur)
93
- Money::Currency.find(:eur).should == expected
94
- Money::Currency.find(:EUR).should == expected
95
- Money::Currency.find("eur").should == expected
96
- Money::Currency.find("EUR").should == expected
97
- end
98
- end
99
-
100
- specify "#self.wrap should return nil if object is nil" do
101
- Money::Currency.wrap(nil).should == nil
102
- Money::Currency.wrap(Money::Currency.new(:usd)).should == Money::Currency.new(:usd)
103
- Money::Currency.wrap(:usd).should == Money::Currency.new(:usd)
104
- end
105
-
106
-
107
- def with_custom_definitions(&block)
108
- begin
109
- old = Money::Currency::TABLE.dup
110
- Money::Currency::TABLE.clear
111
- yield
112
- ensure
113
- silence_warnings do
114
- Money::Currency.const_set("TABLE", old)
115
- end
116
- end
117
- end
118
-
119
- # Sets $VERBOSE to nil for the duration of the block and back to its original value afterwards.
120
- #
121
- # silence_warnings do
122
- # value = noisy_call # no warning voiced
123
- # end
124
- #
125
- # noisy_call # warning voiced
126
- def silence_warnings
127
- old_verbose, $VERBOSE = $VERBOSE, nil
128
- yield
129
- ensure
130
- $VERBOSE = old_verbose
131
- end
132
-
133
- end
1
+ # encoding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe Money::Currency do
6
+
7
+ specify "#initialize should lookup data from loaded config" do
8
+ currency = Money::Currency.new("USD")
9
+ currency.id.should == :usd
10
+ currency.priority.should == 1
11
+ currency.iso_code.should == "USD"
12
+ currency.iso_numeric.should == "840"
13
+ currency.name.should == "United States Dollar"
14
+ currency.decimal_mark.should == "."
15
+ currency.separator.should == "."
16
+ currency.thousands_separator.should == ","
17
+ currency.delimiter.should == ","
18
+ end
19
+
20
+ specify "#initialize should raise UnknownMoney::Currency with unknown currency" do
21
+ lambda { Money::Currency.new("xxx") }.should raise_error(Money::Currency::UnknownCurrency, /xxx/)
22
+ end
23
+
24
+ specify "#== should return true if self === other" do
25
+ currency = Money::Currency.new(:eur)
26
+ currency.should == currency
27
+ end
28
+
29
+ specify "#== should return true if the id is equal" do
30
+ Money::Currency.new(:eur).should == Money::Currency.new(:eur)
31
+ Money::Currency.new(:eur).should_not == Money::Currency.new(:usd)
32
+ end
33
+
34
+ specify "#eql? should return true if #== returns true" do
35
+ Money::Currency.new(:eur).eql?(Money::Currency.new(:eur)).should be true
36
+ Money::Currency.new(:eur).eql?(Money::Currency.new(:usd)).should be false
37
+ end
38
+
39
+ specify "#hash should return the same value for equal objects" do
40
+ Money::Currency.new(:eur).hash.should == Money::Currency.new(:eur).hash
41
+ Money::Currency.new(:eur).hash.should_not == Money::Currency.new(:usd).hash
42
+ end
43
+
44
+ specify "#hash can be used to return the intersection of Currency object arrays" do
45
+ intersection = [Money::Currency.new(:eur), Money::Currency.new(:usd)] & [Money::Currency.new(:eur)]
46
+ intersection.should == [Money::Currency.new(:eur)]
47
+ end
48
+
49
+ specify "#<=> should compare objects by priority" do
50
+ Money::Currency.new(:cad).should > Money::Currency.new(:usd)
51
+ Money::Currency.new(:usd).should < Money::Currency.new(:eur)
52
+ end
53
+
54
+ specify "#to_s" do
55
+ Money::Currency.new(:usd).to_s.should == "USD"
56
+ Money::Currency.new(:eur).to_s.should == "EUR"
57
+ end
58
+
59
+ specify "#to_currency" do
60
+ usd = Money::Currency.new(:usd)
61
+ usd.to_currency.should == usd
62
+ end
63
+
64
+ specify "#inspect" do
65
+ Money::Currency.new(:usd).inspect.should ==
66
+ %Q{#<Money::Currency id: usd, priority: 1, symbol_first: true, thousands_separator: ,, html_entity: $, decimal_mark: ., name: United States Dollar, symbol: $, subunit_to_unit: 100, iso_code: USD, iso_numeric: 840, subunit: Cent>}
67
+ end
68
+
69
+
70
+ specify "#self.find should return currency matching given id" do
71
+ with_custom_definitions do
72
+ Money::Currency::TABLE[:usd] = JSON.parse(%Q({ "priority": 1, "iso_code": "USD", "iso_numeric": "840", "name": "United States Dollar", "symbol": "$", "subunit": "Cent", "subunit_to_unit": 100, "symbol_first": true, "html_entity": "$", "decimal_mark": ".", "thousands_separator": "," }))
73
+ Money::Currency::TABLE[:eur] = JSON.parse(%Q({ "priority": 2, "iso_code": "EUR", "iso_numeric": "978", "name": "Euro", "symbol": "€", "subunit": "Cent", "subunit_to_unit": 100, "symbol_first": false, "html_entity": "&#x20AC;", "decimal_mark": ",", "thousands_separator": "." }))
74
+
75
+ expected = Money::Currency.new(:eur)
76
+ Money::Currency.find(:eur).should == expected
77
+ Money::Currency.find(:EUR).should == expected
78
+ Money::Currency.find("eur").should == expected
79
+ Money::Currency.find("EUR").should == expected
80
+ end
81
+ end
82
+
83
+ specify "#self.find should return nil unless currency matching given id" do
84
+ Money::Currency.find("ZZZ").should be_nil
85
+ end
86
+
87
+ specify "#self.wrap should return nil if object is nil" do
88
+ Money::Currency.wrap(nil).should == nil
89
+ Money::Currency.wrap(Money::Currency.new(:usd)).should == Money::Currency.new(:usd)
90
+ Money::Currency.wrap(:usd).should == Money::Currency.new(:usd)
91
+ end
92
+
93
+
94
+ def with_custom_definitions(&block)
95
+ begin
96
+ old = Money::Currency::TABLE.dup
97
+ Money::Currency::TABLE.clear
98
+ yield
99
+ ensure
100
+ silence_warnings do
101
+ Money::Currency.const_set("TABLE", old)
102
+ end
103
+ end
104
+ end
105
+
106
+ # Sets $VERBOSE to nil for the duration of the block and back to its original value afterwards.
107
+ #
108
+ # silence_warnings do
109
+ # value = noisy_call # no warning voiced
110
+ # end
111
+ #
112
+ # noisy_call # warning voiced
113
+ def silence_warnings
114
+ old_verbose, $VERBOSE = $VERBOSE, nil
115
+ yield
116
+ ensure
117
+ $VERBOSE = old_verbose
118
+ end
119
+
120
+ end