money 2.0.0 → 2.1.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.
data/Rakefile CHANGED
@@ -14,5 +14,5 @@ end
14
14
 
15
15
  desc "Run unit tests"
16
16
  task :test do
17
- sh "spec -f s -c test/*_spec.rb"
17
+ ruby "-S spec -f s -c test/*_spec.rb"
18
18
  end
@@ -24,10 +24,10 @@ class String
24
24
  currency = matches[0] ? matches[0][0] : Money.default_currency
25
25
 
26
26
  # Get the cents amount
27
- matches = scan /(\-?[\d ]+([\.,](\d+))?)/
27
+ sans_spaces = gsub(/\s+/, '')
28
+ matches = sans_spaces.scan /(\-?\d+(?:[\.,]\d+)?)/
28
29
  cents = if matches[0]
29
30
  value = matches[0][0].gsub(/,/, '.')
30
- value.gsub!(/ +/, '')
31
31
  value.to_f * 100
32
32
  else
33
33
  0
data/lib/money/money.rb CHANGED
@@ -127,46 +127,64 @@ class Money
127
127
  end
128
128
 
129
129
 
130
- # Format the price according to several rules
131
- # Currently supported are :with_currency, :no_cents and :html
130
+ # Format the price according to several rules. The following options are
131
+ # supported: :display_free, :with_currency, :no_cents, :symbol and :html
132
+ #
133
+ # display_free:
134
+ #
135
+ # Money.us_dollar(0).format(:display_free => true) => "free"
136
+ # Money.us_dollar(0).format(:display_free => "gratis") => "gratis"
137
+ # Money.us_dollar(0).format => "$0.00"
132
138
  #
133
139
  # with_currency:
134
140
  #
135
- # Money.ca_dollar(0).format => "free"
136
141
  # Money.ca_dollar(100).format => "$1.00"
137
- # Money.ca_dollar(100).format(:with_currency) => "$1.00 CAD"
138
- # Money.us_dollar(85).format(:with_currency) => "$0.85 USD"
142
+ # Money.ca_dollar(100).format(:with_currency => true) => "$1.00 CAD"
143
+ # Money.us_dollar(85).format(:with_currency => true) => "$0.85 USD"
139
144
  #
140
145
  # no_cents:
141
146
  #
142
- # Money.ca_dollar(100).format(:no_cents) => "$1"
143
- # Money.ca_dollar(599).format(:no_cents) => "$5"
147
+ # Money.ca_dollar(100).format(:no_cents => true) => "$1"
148
+ # Money.ca_dollar(599).format(:no_cents => true) => "$5"
144
149
  #
145
- # Money.ca_dollar(570).format(:no_cents, :with_currency) => "$5 CAD"
146
- # Money.ca_dollar(39000).format(:no_cents) => "$390"
150
+ # Money.ca_dollar(570).format(:no_cents => true, :with_currency => true) => "$5 CAD"
151
+ # Money.ca_dollar(39000).format(:no_cents => true) => "$390"
152
+ #
153
+ # symbol:
154
+ #
155
+ # Money.new(100, :currency => "GBP").format(:symbol => "£") => "£1.00"
147
156
  #
148
157
  # html:
149
158
  #
150
- # Money.ca_dollar(570).format(:html, :with_currency) => "$5.70 <span class=\"currency\">CAD</span>"
151
- def format(*rules)
152
- return "free" if cents == 0
153
-
154
- rules = rules.flatten
159
+ # Money.ca_dollar(570).format(:html => true, :with_currency => true) => "$5.70 <span class=\"currency\">CAD</span>"
160
+ def format(rules = {})
161
+ if cents == 0
162
+ if rules[:display_free].respond_to?(:to_str)
163
+ return rules[:display_free]
164
+ elsif rules[:display_free]
165
+ return "free"
166
+ end
167
+ end
155
168
 
156
- if rules.include?(:no_cents)
157
- formatted = sprintf("$%d", cents.to_f / 100 )
169
+ symbol = rules[:symbol] ? rules[:symbol].empty? ? "$" : rules[:symbol] : "$"
170
+
171
+ if rules[:no_cents]
172
+ formatted = sprintf("#{symbol}%d", cents.to_f / 100)
158
173
  else
159
- formatted = sprintf("$%.2f", cents.to_f / 100 )
174
+ formatted = sprintf("#{symbol}%.2f", cents.to_f / 100)
160
175
  end
176
+
177
+ # Commify ("10000" => "10,000")
178
+ formatted.gsub!(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2')
161
179
 
162
- if rules.include?(:with_currency)
180
+ if rules[:with_currency]
163
181
  formatted << " "
164
- formatted << '<span class="currency">' if rules.include?(:html)
182
+ formatted << '<span class="currency">' if rules[:html]
165
183
  formatted << currency
166
- formatted << '</span>' if rules.include?(:html)
184
+ formatted << '</span>' if rules[:html]
167
185
  end
168
186
  formatted
169
- end
187
+ end
170
188
 
171
189
  # Money.ca_dollar(100).to_s => "1.00"
172
190
  def to_s
data/money.gemspec CHANGED
@@ -1,13 +1,13 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "money"
3
- s.version = "2.0.0"
3
+ s.version = "2.1.0"
4
4
  s.summary = "Money and currency exchange support library"
5
5
  s.email = "hongli@phusion.nl"
6
6
  s.homepage = "http://money.rubyforge.org/"
7
7
  s.description = "Money and currency exchange support library."
8
8
  s.has_rdoc = true
9
9
  s.rubyforge_project = "money"
10
- s.authors = ["Tobias Luetke", "Hongli Lai"]
10
+ s.authors = ["Tobias Luetke", "Hongli Lai", "Jeremy McNevin"]
11
11
 
12
12
  s.files = [
13
13
  "README.rdoc", "MIT-LICENSE", "money.gemspec", "Rakefile",
@@ -2,32 +2,35 @@ $LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/../lib")
2
2
  require 'money/core_extensions'
3
3
 
4
4
  describe "Money core extensions" do
5
- specify "Numberic#to_money works" do
6
- money = 1234.to_money
7
- money.cents.should == 1234_00
8
- money.currency.should == Money.default_currency
9
-
10
- money = 100.37.to_money
11
- money.cents.should == 100_37
12
- money.currency.should == Money.default_currency
13
- end
14
-
15
- specify "String#to_money works" do
16
- "100".to_money.should == Money.new(100_00)
17
- "100.37".to_money.should == Money.new(100_37)
18
- "100,37".to_money.should == Money.new(100_37)
19
- "100 000".to_money.should == Money.new(100_000_00)
20
-
21
- "100 USD".to_money.should == Money.new(100_00, "USD")
22
- "100 EUR".to_money.should == Money.new(100_00, "EUR")
23
- "100.37 EUR".to_money.should == Money.new(100_37, "EUR")
24
- "100,37 EUR".to_money.should == Money.new(100_37, "EUR")
25
-
26
- "USD 100".to_money.should == Money.new(100_00, "USD")
27
- "EUR 100".to_money.should == Money.new(100_00, "EUR")
28
- "EUR 100.37".to_money.should == Money.new(100_37, "EUR")
29
- "EUR 100,37".to_money.should == Money.new(100_37, "EUR")
30
-
31
- "$100 USD".to_money.should == Money.new(100_00, "USD")
32
- end
5
+ specify "Numberic#to_money works" do
6
+ money = 1234.to_money
7
+ money.cents.should == 1234_00
8
+ money.currency.should == Money.default_currency
9
+
10
+ money = 100.37.to_money
11
+ money.cents.should == 100_37
12
+ money.currency.should == Money.default_currency
13
+ end
14
+
15
+ specify "String#to_money works" do
16
+ "100".to_money.should == Money.new(100_00)
17
+ "100.37".to_money.should == Money.new(100_37)
18
+ "100,37".to_money.should == Money.new(100_37)
19
+ "100 000".to_money.should == Money.new(100_000_00)
20
+
21
+ "100 USD".to_money.should == Money.new(100_00, "USD")
22
+ "-100 USD".to_money.should == Money.new(-100_00, "USD")
23
+ "100 EUR".to_money.should == Money.new(100_00, "EUR")
24
+ "100.37 EUR".to_money.should == Money.new(100_37, "EUR")
25
+ "100,37 EUR".to_money.should == Money.new(100_37, "EUR")
26
+
27
+ "USD 100".to_money.should == Money.new(100_00, "USD")
28
+ "EUR 100".to_money.should == Money.new(100_00, "EUR")
29
+ "EUR 100.37".to_money.should == Money.new(100_37, "EUR")
30
+ "CAD -100.37".to_money.should == Money.new(-100_37, "CAD")
31
+ "EUR 100,37".to_money.should == Money.new(100_37, "EUR")
32
+ "EUR -100,37".to_money.should == Money.new(-100_37, "EUR")
33
+
34
+ "$100 USD".to_money.should == Money.new(100_00, "USD")
35
+ end
33
36
  end
@@ -2,44 +2,44 @@ $LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/../lib")
2
2
  require 'money/variable_exchange_bank'
3
3
 
4
4
  describe Money::VariableExchangeBank do
5
- before :each do
6
- @bank = Money::VariableExchangeBank.new
7
- end
8
-
9
- it "returns the previously specified conversion rate" do
10
- @bank.add_rate("USD", "EUR", 0.788332676)
11
- @bank.add_rate("EUR", "YEN", 122.631477)
12
- @bank.get_rate("USD", "EUR").should == 0.788332676
13
- @bank.get_rate("EUR", "YEN").should == 122.631477
14
- end
15
-
16
- it "treats currency names case-insensitively" do
17
- @bank.add_rate("usd", "eur", 1)
18
- @bank.get_rate("USD", "EUR").should == 1
19
- @bank.same_currency?("USD", "usd").should be_true
20
- @bank.same_currency?("EUR", "usd").should be_false
21
- end
22
-
23
- it "returns nil if the conversion rate is unknown" do
24
- @bank.get_rate("American Pesos", "EUR").should be_nil
25
- end
26
-
27
- it "exchanges money from one currency to another according to the specified conversion rates" do
28
- @bank.add_rate("USD", "EUR", 0.5)
29
- @bank.add_rate("EUR", "YEN", 10)
30
- @bank.exchange(10_00, "USD", "EUR").should == 5_00
31
- @bank.exchange(500_00, "EUR", "YEN").should == 5000_00
32
- end
33
-
34
- it "rounds the exchanged result down" do
35
- @bank.add_rate("USD", "EUR", 0.788332676)
36
- @bank.add_rate("EUR", "YEN", 122.631477)
37
- @bank.exchange(10_00, "USD", "EUR").should == 788
38
- @bank.exchange(500_00, "EUR", "YEN").should == 6131573
39
- end
40
-
41
- it "raises Money::UnknownRate upon conversion if the conversion rate is unknown" do
42
- block = lambda { @bank.exchange(10, "USD", "EUR") }
43
- block.should raise_error(Money::UnknownRate)
44
- end
5
+ before :each do
6
+ @bank = Money::VariableExchangeBank.new
7
+ end
8
+
9
+ it "returns the previously specified conversion rate" do
10
+ @bank.add_rate("USD", "EUR", 0.788332676)
11
+ @bank.add_rate("EUR", "YEN", 122.631477)
12
+ @bank.get_rate("USD", "EUR").should == 0.788332676
13
+ @bank.get_rate("EUR", "YEN").should == 122.631477
14
+ end
15
+
16
+ it "treats currency names case-insensitively" do
17
+ @bank.add_rate("usd", "eur", 1)
18
+ @bank.get_rate("USD", "EUR").should == 1
19
+ @bank.same_currency?("USD", "usd").should be_true
20
+ @bank.same_currency?("EUR", "usd").should be_false
21
+ end
22
+
23
+ it "returns nil if the conversion rate is unknown" do
24
+ @bank.get_rate("American Pesos", "EUR").should be_nil
25
+ end
26
+
27
+ it "exchanges money from one currency to another according to the specified conversion rates" do
28
+ @bank.add_rate("USD", "EUR", 0.5)
29
+ @bank.add_rate("EUR", "YEN", 10)
30
+ @bank.exchange(10_00, "USD", "EUR").should == 5_00
31
+ @bank.exchange(500_00, "EUR", "YEN").should == 5000_00
32
+ end
33
+
34
+ it "rounds the exchanged result down" do
35
+ @bank.add_rate("USD", "EUR", 0.788332676)
36
+ @bank.add_rate("EUR", "YEN", 122.631477)
37
+ @bank.exchange(10_00, "USD", "EUR").should == 788
38
+ @bank.exchange(500_00, "EUR", "YEN").should == 6131573
39
+ end
40
+
41
+ it "raises Money::UnknownRate upon conversion if the conversion rate is unknown" do
42
+ block = lambda { @bank.exchange(10, "USD", "EUR") }
43
+ block.should raise_error(Money::UnknownRate)
44
+ end
45
45
  end
data/test/money_spec.rb CHANGED
@@ -1,124 +1,177 @@
1
+ # encoding: utf-8
1
2
  $LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/../lib")
2
3
  require 'money/money'
3
4
 
4
5
  describe Money do
5
- it "is associated to the singleton instance of VariableExchangeBank by default" do
6
- Money.new(0).bank.object_id.should == Money::VariableExchangeBank.instance.object_id
7
- end
8
-
9
- specify "#cents returns the amount of cents passed to the constructor" do
10
- Money.new(200_00, "USD").cents.should == 200_00
11
- end
12
-
13
- it "rounds the given cents to an integer" do
14
- Money.new(1.00, "USD").cents.should == 1
15
- Money.new(1.01, "USD").cents.should == 1
16
- Money.new(1.50, "USD").cents.should == 2
17
- end
18
-
19
- specify "#currency returns the currency passed to the constructor" do
20
- Money.new(200_00, "USD").currency.should == "USD"
21
- end
22
-
23
- specify "#zero? returns whether the amount is 0" do
24
- Money.new(0, "USD").should be_zero
25
- Money.new(0, "EUR").should be_zero
26
- Money.new(1, "USD").should_not be_zero
27
- Money.new(10, "YEN").should_not be_zero
28
- Money.new(-1, "EUR").should_not be_zero
29
- end
30
-
31
- specify "#exchange_to exchanges the amount via its exchange bank" do
32
- money = Money.new(100_00, "USD")
33
- money.bank.should_receive(:exchange).with(100_00, "USD", "EUR").and_return(200_00)
34
- money.exchange_to("EUR")
35
- end
36
-
37
- specify "#exchange_to exchanges the amount properly" do
38
- money = Money.new(100_00, "USD")
39
- money.bank.should_receive(:exchange).with(100_00, "USD", "EUR").and_return(200_00)
40
- money.exchange_to("EUR").should == Money.new(200_00, "EUR")
41
- end
42
-
43
- specify "#== returns true if and only if their amount and currency are equal" do
44
- Money.new(1_00, "USD").should == Money.new(1_00, "USD")
45
- Money.new(1_00, "USD").should_not == Money.new(1_00, "EUR")
46
- Money.new(1_00, "USD").should_not == Money.new(2_00, "USD")
47
- Money.new(1_00, "USD").should_not == Money.new(99_00, "EUR")
48
- end
49
-
50
- specify "#* multiplies the money's amount by the multiplier while retaining the currency" do
51
- (Money.new(1_00, "USD") * 10).should == Money.new(10_00, "USD")
52
- end
53
-
54
- specify "#* divides the money's amount by the divisor while retaining the currency" do
55
- (Money.new(10_00, "USD") / 10).should == Money.new(1_00, "USD")
56
- end
57
-
58
- specify "Money.empty creates a new Money object of 0 cents" do
59
- Money.empty.should == Money.new(0)
60
- end
61
-
62
- specify "Money.ca_dollar creates a new Money object of the given value in CAD" do
63
- Money.ca_dollar(50).should == Money.new(50, "CAD")
64
- end
65
-
66
- specify "Money.ca_dollar creates a new Money object of the given value in USD" do
67
- Money.us_dollar(50).should == Money.new(50, "USD")
68
- end
69
-
70
- specify "Money.ca_dollar creates a new Money object of the given value in EUR" do
71
- Money.euro(50).should == Money.new(50, "EUR")
72
- end
73
-
74
- specify "Money.add_rate works" do
75
- Money.add_rate("EUR", "USD", 10)
76
- Money.new(10_00, "EUR").exchange_to("USD").should == Money.new(100_00, "USD")
77
- end
6
+ it "is associated to the singleton instance of VariableExchangeBank by default" do
7
+ Money.new(0).bank.object_id.should == Money::VariableExchangeBank.instance.object_id
8
+ end
9
+
10
+ specify "#cents returns the amount of cents passed to the constructor" do
11
+ Money.new(200_00, "USD").cents.should == 200_00
12
+ end
13
+
14
+ it "rounds the given cents to an integer" do
15
+ Money.new(1.00, "USD").cents.should == 1
16
+ Money.new(1.01, "USD").cents.should == 1
17
+ Money.new(1.50, "USD").cents.should == 2
18
+ end
19
+
20
+ specify "#currency returns the currency passed to the constructor" do
21
+ Money.new(200_00, "USD").currency.should == "USD"
22
+ end
23
+
24
+ specify "#zero? returns whether the amount is 0" do
25
+ Money.new(0, "USD").should be_zero
26
+ Money.new(0, "EUR").should be_zero
27
+ Money.new(1, "USD").should_not be_zero
28
+ Money.new(10, "YEN").should_not be_zero
29
+ Money.new(-1, "EUR").should_not be_zero
30
+ end
31
+
32
+ specify "#exchange_to exchanges the amount via its exchange bank" do
33
+ money = Money.new(100_00, "USD")
34
+ money.bank.should_receive(:exchange).with(100_00, "USD", "EUR").and_return(200_00)
35
+ money.exchange_to("EUR")
36
+ end
37
+
38
+ specify "#exchange_to exchanges the amount properly" do
39
+ money = Money.new(100_00, "USD")
40
+ money.bank.should_receive(:exchange).with(100_00, "USD", "EUR").and_return(200_00)
41
+ money.exchange_to("EUR").should == Money.new(200_00, "EUR")
42
+ end
43
+
44
+ specify "#== returns true if and only if their amount and currency are equal" do
45
+ Money.new(1_00, "USD").should == Money.new(1_00, "USD")
46
+ Money.new(1_00, "USD").should_not == Money.new(1_00, "EUR")
47
+ Money.new(1_00, "USD").should_not == Money.new(2_00, "USD")
48
+ Money.new(1_00, "USD").should_not == Money.new(99_00, "EUR")
49
+ end
50
+
51
+ specify "#* multiplies the money's amount by the multiplier while retaining the currency" do
52
+ (Money.new(1_00, "USD") * 10).should == Money.new(10_00, "USD")
53
+ end
54
+
55
+ specify "#* divides the money's amount by the divisor while retaining the currency" do
56
+ (Money.new(10_00, "USD") / 10).should == Money.new(1_00, "USD")
57
+ end
58
+
59
+ specify "Money.empty creates a new Money object of 0 cents" do
60
+ Money.empty.should == Money.new(0)
61
+ end
62
+
63
+ specify "Money.ca_dollar creates a new Money object of the given value in CAD" do
64
+ Money.ca_dollar(50).should == Money.new(50, "CAD")
65
+ end
66
+
67
+ specify "Money.ca_dollar creates a new Money object of the given value in USD" do
68
+ Money.us_dollar(50).should == Money.new(50, "USD")
69
+ end
70
+
71
+ specify "Money.ca_dollar creates a new Money object of the given value in EUR" do
72
+ Money.euro(50).should == Money.new(50, "EUR")
73
+ end
74
+
75
+ specify "Money.add_rate works" do
76
+ Money.add_rate("EUR", "USD", 10)
77
+ Money.new(10_00, "EUR").exchange_to("USD").should == Money.new(100_00, "USD")
78
+ end
79
+
80
+ describe "#format" do
81
+ it "returns the monetary value as a string" do
82
+ Money.ca_dollar(100).format.should == "$1.00"
83
+ end
84
+
85
+ describe "if the monetary value is 0" do
86
+ before :each do
87
+ @money = Money.us_dollar(0)
88
+ end
89
+
90
+ it "returns 'free' when :display_free is true" do
91
+ @money.format(:display_free => true).should == 'free'
92
+ end
93
+
94
+ it "returns '$0.00' when :display_free is false or not given" do
95
+ @money.format.should == '$0.00'
96
+ @money.format(:display_free => false).should == '$0.00'
97
+ @money.format(:display_free => nil).should == '$0.00'
98
+ end
99
+
100
+ it "returns the value specified by :display_free if it's a string-like object" do
101
+ @money.format(:display_free => 'gratis').should == 'gratis'
102
+ end
103
+ end
104
+
105
+ specify "#format(:with_currency => true) works as documented" do
106
+ Money.ca_dollar(100).format(:with_currency => true).should == "$1.00 CAD"
107
+ Money.us_dollar(85).format(:with_currency => true).should == "$0.85 USD"
108
+ end
109
+
110
+ specify "#format(:no_cents => true) works as documented" do
111
+ Money.ca_dollar(100).format(:no_cents => true).should == "$1"
112
+ Money.ca_dollar(599).format(:no_cents => true).should == "$5"
113
+ Money.ca_dollar(570).format(:no_cents => true, :with_currency => true).should == "$5 CAD"
114
+ Money.ca_dollar(39000).format(:no_cents => true).should == "$390"
115
+ end
116
+
117
+ specify "#format(:symbol => ...) works as documented" do
118
+ Money.new(100, :currency => "GBP").format(:symbol => "£").should == "£1.00"
119
+ end
120
+
121
+ specify "#format(:html => true) works as documented" do
122
+ string = Money.ca_dollar(570).format(:html => true, :with_currency => true)
123
+ string.should == "$5.70 <span class=\"currency\">CAD</span>"
124
+ end
125
+
126
+ it "should insert commas into the result if the amount is sufficiently large" do
127
+ Money.us_dollar(1_000_000_000_12).format.should == "$1,000,000,000.12"
128
+ Money.us_dollar(1_000_000_000_12).format(:no_cents => true).should == "$1,000,000,000"
129
+ end
130
+ end
78
131
  end
79
132
 
80
133
  describe "Actions involving two Money objects" do
81
- describe "if the other Money object has the same currency" do
82
- specify "#<=> compares the two objects' amounts" do
83
- (Money.new(1_00, "USD") <=> Money.new(1_00, "USD")).should == 0
84
- (Money.new(1_00, "USD") <=> Money.new(99, "USD")).should > 0
85
- (Money.new(1_00, "USD") <=> Money.new(2_00, "USD")).should < 0
86
- end
87
-
88
- specify "#+ adds the other object's amount to the current object's amount while retaining the currency" do
89
- (Money.new(10_00, "USD") + Money.new(90, "USD")).should == Money.new(10_90, "USD")
90
- end
91
-
92
- specify "#- substracts the other object's amount from the current object's amount while retaining the currency" do
93
- (Money.new(10_00, "USD") - Money.new(90, "USD")).should == Money.new(9_10, "USD")
94
- end
95
- end
96
-
97
- describe "if the other Money object has a different currency" do
98
- specify "#<=> compares the two objects' amount after converting the other object's amount to its own currency" do
99
- target = Money.new(200_00, "EUR")
100
- target.should_receive(:exchange_to).with("USD").and_return(Money.new(300_00, "USD"))
101
- (Money.new(100_00, "USD") <=> target).should < 0
102
-
103
- target = Money.new(200_00, "EUR")
104
- target.should_receive(:exchange_to).with("USD").and_return(Money.new(100_00, "USD"))
105
- (Money.new(100_00, "USD") <=> target).should == 0
106
-
107
- target = Money.new(200_00, "EUR")
108
- target.should_receive(:exchange_to).with("USD").and_return(Money.new(99_00, "USD"))
109
- (Money.new(100_00, "USD") <=> target).should > 0
110
- end
111
-
112
- specify "#+ adds the other object's amount, converted to this object's currency, to this object's amount while retaining its currency" do
113
- other = Money.new(90, "EUR")
114
- other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD"))
115
- (Money.new(10_00, "USD") + other).should == Money.new(19_00, "USD")
116
- end
117
-
118
- specify "#- substracts the other object's amount, converted to this object's currency, from this object's amount while retaining its currency" do
119
- other = Money.new(90, "EUR")
120
- other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD"))
121
- (Money.new(10_00, "USD") - other).should == Money.new(1_00, "USD")
122
- end
123
- end
134
+ describe "if the other Money object has the same currency" do
135
+ specify "#<=> compares the two objects' amounts" do
136
+ (Money.new(1_00, "USD") <=> Money.new(1_00, "USD")).should == 0
137
+ (Money.new(1_00, "USD") <=> Money.new(99, "USD")).should > 0
138
+ (Money.new(1_00, "USD") <=> Money.new(2_00, "USD")).should < 0
139
+ end
140
+
141
+ specify "#+ adds the other object's amount to the current object's amount while retaining the currency" do
142
+ (Money.new(10_00, "USD") + Money.new(90, "USD")).should == Money.new(10_90, "USD")
143
+ end
144
+
145
+ specify "#- substracts the other object's amount from the current object's amount while retaining the currency" do
146
+ (Money.new(10_00, "USD") - Money.new(90, "USD")).should == Money.new(9_10, "USD")
147
+ end
148
+ end
149
+
150
+ describe "if the other Money object has a different currency" do
151
+ specify "#<=> compares the two objects' amount after converting the other object's amount to its own currency" do
152
+ target = Money.new(200_00, "EUR")
153
+ target.should_receive(:exchange_to).with("USD").and_return(Money.new(300_00, "USD"))
154
+ (Money.new(100_00, "USD") <=> target).should < 0
155
+
156
+ target = Money.new(200_00, "EUR")
157
+ target.should_receive(:exchange_to).with("USD").and_return(Money.new(100_00, "USD"))
158
+ (Money.new(100_00, "USD") <=> target).should == 0
159
+
160
+ target = Money.new(200_00, "EUR")
161
+ target.should_receive(:exchange_to).with("USD").and_return(Money.new(99_00, "USD"))
162
+ (Money.new(100_00, "USD") <=> target).should > 0
163
+ end
164
+
165
+ specify "#+ adds the other object's amount, converted to this object's currency, to this object's amount while retaining its currency" do
166
+ other = Money.new(90, "EUR")
167
+ other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD"))
168
+ (Money.new(10_00, "USD") + other).should == Money.new(19_00, "USD")
169
+ end
170
+
171
+ specify "#- substracts the other object's amount, converted to this object's currency, from this object's amount while retaining its currency" do
172
+ other = Money.new(90, "EUR")
173
+ other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD"))
174
+ (Money.new(10_00, "USD") - other).should == Money.new(1_00, "USD")
175
+ end
176
+ end
124
177
  end
metadata CHANGED
@@ -1,16 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Luetke
8
8
  - Hongli Lai
9
+ - Jeremy McNevin
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
13
 
13
- date: 2008-10-31 00:00:00 +01:00
14
+ date: 2009-02-15 00:00:00 +01:00
14
15
  default_executable:
15
16
  dependencies: []
16
17
 
@@ -57,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
58
  requirements: []
58
59
 
59
60
  rubyforge_project: money
60
- rubygems_version: 1.2.0
61
+ rubygems_version: 1.3.1
61
62
  signing_key:
62
63
  specification_version: 2
63
64
  summary: Money and currency exchange support library