ShadowBelmolve-money 2.3.1 → 2.3.3
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/README.rdoc +22 -13
- data/lib/money.rb +1 -1
- data/lib/money/acts_as_money.rb +1 -1
- data/lib/money/money.rb +27 -17
- data/lib/money/variable_exchange_bank.rb +51 -22
- data/money.gemspec +6 -6
- data/spec/money/acts_as_money_spec.rb +0 -3
- data/spec/money/exchange_bank_spec.rb +15 -13
- data/spec/money/money_spec.rb +99 -65
- data/spec/spec_helper.rb +2 -3
- metadata +3 -4
data/README.rdoc
CHANGED
|
@@ -12,7 +12,7 @@ This library aids one in handling money and different currencies. Features:
|
|
|
12
12
|
|
|
13
13
|
Resources:
|
|
14
14
|
|
|
15
|
-
- This fork: http://github.com/
|
|
15
|
+
- This fork: http://github.com/ShadowBelmolve/money
|
|
16
16
|
- Website: http://money.rubyforge.org
|
|
17
17
|
- RDoc API: http://money.rubyforge.org
|
|
18
18
|
- Git repository: http://github.com/FooBarWidget/money/tree/master
|
|
@@ -42,8 +42,11 @@ The development version (hosted on Github) can be installed with:
|
|
|
42
42
|
money.currency # => "USD"
|
|
43
43
|
money.format # => "$10.00"
|
|
44
44
|
|
|
45
|
+
Money.new(880088, "EUR").format # => €8,800.88
|
|
46
|
+
Money.new(-8000).format(:no_cents => true) # => $-80
|
|
47
|
+
|
|
45
48
|
Money.new(1000, "USD") == Money.new(1000, "USD") # => true
|
|
46
|
-
Money.new(1000, "USD") == Money.new(100, "USD")
|
|
49
|
+
Money.new(1000, "USD") == Money.new( 100, "USD") # => false
|
|
47
50
|
Money.new(1000, "USD") == Money.new(1000, "EUR") # => false
|
|
48
51
|
|
|
49
52
|
|
|
@@ -53,25 +56,31 @@ Exchanging money is performed through an exchange bank object. The default
|
|
|
53
56
|
exchange bank object requires one to manually specify the exchange rate. Here's
|
|
54
57
|
an example of how it works:
|
|
55
58
|
|
|
56
|
-
Money.add_rate("
|
|
57
|
-
Money.add_rate("
|
|
59
|
+
Money.add_rate("CAD", 0.803115)
|
|
60
|
+
Money.add_rate("USD", 1.24515)
|
|
58
61
|
|
|
59
|
-
Money.us_dollar(
|
|
60
|
-
Money.ca_dollar(
|
|
62
|
+
Money.us_dollar(100_00).exchange_to("CAD") # => Money.new(15504, "CAD")
|
|
63
|
+
Money.ca_dollar(100_00).exchange_to("USD") # => Money.new(6450, "USD")
|
|
61
64
|
|
|
62
65
|
or
|
|
63
66
|
|
|
64
|
-
Money.us_dollar(100).
|
|
65
|
-
Money.ca_dollar(100).
|
|
67
|
+
Money.us_dollar(100).as_cad # => Money.new(155, "CAD")
|
|
68
|
+
Money.ca_dollar(100).as_usd # => Money.new(64, "USD")
|
|
66
69
|
|
|
67
70
|
Comparison and arithmetic operations work as expected:
|
|
68
71
|
|
|
69
72
|
Money.new(1000, "USD") <=> Money.new(900, "USD") # => 1; 9.00 USD is smaller
|
|
70
73
|
Money.new(1000, "EUR") + Money.new(10, "EUR") == Money.new(1010, "EUR")
|
|
71
74
|
|
|
72
|
-
Money.add_rate("
|
|
75
|
+
Money.add_rate("EUR", 0.5)
|
|
73
76
|
Money.new(1000, "EUR") + Money.new(1000, "USD") == Money.new(1500, "EUR")
|
|
74
77
|
|
|
78
|
+
Fetch the exchange rates published by the European Bank
|
|
79
|
+
|
|
80
|
+
Money.default_bank.fetch_rates # Fetch the rates
|
|
81
|
+
Money.default_bank.auto_fetch 3600 # Fetch the rates every hour
|
|
82
|
+
Money.default_bank.stop_fetch # Stop auto-fetch
|
|
83
|
+
|
|
75
84
|
There is nothing stopping you from creating bank objects which scrapes
|
|
76
85
|
www.xe.com for the current rates or just returns <tt>rand(2)</tt>:
|
|
77
86
|
|
|
@@ -80,9 +89,9 @@ www.xe.com for the current rates or just returns <tt>rand(2)</tt>:
|
|
|
80
89
|
|
|
81
90
|
=== Ruby on Rails
|
|
82
91
|
|
|
83
|
-
Use the +
|
|
84
|
-
The following example requires a +
|
|
85
|
-
fields.
|
|
92
|
+
Use the +has_money+ method to embed the money object in your models.
|
|
93
|
+
The following example requires a +price_cents+ and a +price_currency+
|
|
94
|
+
fields on the database.
|
|
86
95
|
|
|
87
96
|
config/enviroment.rb
|
|
88
97
|
|
|
@@ -107,7 +116,7 @@ migration:
|
|
|
107
116
|
|
|
108
117
|
=== Default Currency
|
|
109
118
|
|
|
110
|
-
By default Money defaults to USD as its currency. This can be overwritten using
|
|
119
|
+
By default Money defaults to USD as its currency. This can be overwritten using:
|
|
111
120
|
|
|
112
121
|
Money.default_currency = "CAD"
|
|
113
122
|
|
data/lib/money.rb
CHANGED
data/lib/money/acts_as_money.rb
CHANGED
|
@@ -24,7 +24,7 @@ module ActsAsMoney #:nodoc:
|
|
|
24
24
|
#
|
|
25
25
|
def has_money(*attributes)
|
|
26
26
|
config = {:with_currency => true, :with_cents => false,
|
|
27
|
-
:allow_nil =>
|
|
27
|
+
:allow_nil => false }.update(attributes.extract_options!)
|
|
28
28
|
|
|
29
29
|
for attribute in attributes do
|
|
30
30
|
mapping = [[config[:cents] || "#{attribute}_cents", 'cents']]
|
data/lib/money/money.rb
CHANGED
|
@@ -28,6 +28,9 @@ class Money
|
|
|
28
28
|
attr_accessor :default_currency
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
+
self.default_bank = VariableExchangeBank.instance
|
|
32
|
+
self.default_currency = "USD"
|
|
33
|
+
|
|
31
34
|
CURRENCIES = {
|
|
32
35
|
"USD" => { :delimiter => ",", :separator => ".", :symbol => "$" },
|
|
33
36
|
"CAD" => { :delimiter => ",", :separator => ".", :symbol => "$" },
|
|
@@ -38,9 +41,6 @@ class Money
|
|
|
38
41
|
"GBP" => { :delimiter => ",", :separator => ".", :symbol => '£', :html => '£' },
|
|
39
42
|
"JPY" => { :delimiter => ".", :separator => ".", :symbol => '¥', :html => '¥' },
|
|
40
43
|
}
|
|
41
|
-
self.default_bank = VariableExchangeBank.instance
|
|
42
|
-
self.default_currency = "USD"
|
|
43
|
-
|
|
44
44
|
|
|
45
45
|
# Create a new money object with value 0.
|
|
46
46
|
def self.empty(currency = default_currency)
|
|
@@ -67,8 +67,8 @@ class Money
|
|
|
67
67
|
Money.new(cents, "BRL")
|
|
68
68
|
end
|
|
69
69
|
|
|
70
|
-
def self.add_rate(
|
|
71
|
-
Money.default_bank.add_rate(
|
|
70
|
+
def self.add_rate(currency, rate)
|
|
71
|
+
Money.default_bank.add_rate(currency, rate)
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
# Creates a new money object.
|
|
@@ -76,10 +76,10 @@ class Money
|
|
|
76
76
|
#
|
|
77
77
|
# Alternativly you can use the convinience methods like
|
|
78
78
|
# Money.ca_dollar and Money.us_dollar
|
|
79
|
-
def initialize(cents, currency =
|
|
80
|
-
@cents = cents.
|
|
81
|
-
@currency = currency
|
|
82
|
-
@bank = bank
|
|
79
|
+
def initialize(cents, currency = nil, bank = nil)
|
|
80
|
+
@cents = cents.to_i
|
|
81
|
+
@currency = currency || Money.default_currency
|
|
82
|
+
@bank = bank || Money.default_bank
|
|
83
83
|
end
|
|
84
84
|
|
|
85
85
|
# Do two money objects equal? Only works if both objects are of the same currency
|
|
@@ -96,6 +96,7 @@ class Money
|
|
|
96
96
|
end
|
|
97
97
|
|
|
98
98
|
def +(other_money)
|
|
99
|
+
other_money = Money.new(other_money) unless other_money.is_a? Money
|
|
99
100
|
if currency == other_money.currency
|
|
100
101
|
Money.new(cents + other_money.cents, other_money.currency)
|
|
101
102
|
else
|
|
@@ -104,6 +105,7 @@ class Money
|
|
|
104
105
|
end
|
|
105
106
|
|
|
106
107
|
def -(other_money)
|
|
108
|
+
other_money = Money.new(other_money) unless other_money.is_a? Money
|
|
107
109
|
if currency == other_money.currency
|
|
108
110
|
Money.new(cents - other_money.cents, other_money.currency)
|
|
109
111
|
else
|
|
@@ -138,13 +140,13 @@ class Money
|
|
|
138
140
|
|
|
139
141
|
# Calculates compound interest
|
|
140
142
|
# Returns a money object with the sum of self + it
|
|
141
|
-
def compound_interest(rate,count=1)
|
|
142
|
-
Money.new(cents * ((1 + rate / 100.0 /
|
|
143
|
+
def compound_interest(rate, count = 1, period = 12)
|
|
144
|
+
Money.new(cents * ((1 + rate / 100.0 / period) ** count - 1))
|
|
143
145
|
end
|
|
144
146
|
|
|
145
147
|
# Calculate self + simple interest
|
|
146
|
-
def simple_interest(rate,count=1)
|
|
147
|
-
Money.new(rate/100/
|
|
148
|
+
def simple_interest(rate, count = 1, period = 12)
|
|
149
|
+
Money.new(rate / 100 / period * cents * count)
|
|
148
150
|
end
|
|
149
151
|
|
|
150
152
|
# Split money in number of installments
|
|
@@ -152,10 +154,11 @@ class Money
|
|
|
152
154
|
# Money.new(10_00).split_in_installments(3)
|
|
153
155
|
# => [ 3.34, 3.33, 3.33 ] (All Money instances)
|
|
154
156
|
#
|
|
155
|
-
def split_in_installments(fixnum,
|
|
157
|
+
def split_in_installments(fixnum, order=false)
|
|
156
158
|
wallet = Wallet.new(fixnum, Money.new(cents/fixnum,currency))
|
|
157
159
|
to_add = cents % fixnum
|
|
158
160
|
to_add.times { |m| wallet[m] += Money.new(1) }
|
|
161
|
+
wallet.reverse! if order
|
|
159
162
|
wallet
|
|
160
163
|
end
|
|
161
164
|
|
|
@@ -164,8 +167,8 @@ class Money
|
|
|
164
167
|
# Money.new(1000_00).split_in_installments(Money.new(300_00))
|
|
165
168
|
# => [ 334_00, 333_00, 333_00 ] (All Money instances)
|
|
166
169
|
#
|
|
167
|
-
def in_installments_of(other_money,
|
|
168
|
-
split_in_installments(cents/other_money.cents,
|
|
170
|
+
def in_installments_of(other_money, order=false)
|
|
171
|
+
split_in_installments(cents/other_money.cents, order)
|
|
169
172
|
end
|
|
170
173
|
|
|
171
174
|
# Just a helper if you got tax inputs in percentage.
|
|
@@ -220,6 +223,7 @@ class Money
|
|
|
220
223
|
else
|
|
221
224
|
symbol = CURRENCIES[currency][:symbol]
|
|
222
225
|
end
|
|
226
|
+
self.currency
|
|
223
227
|
|
|
224
228
|
if rules[:no_cents]
|
|
225
229
|
formatted = sprintf("#{symbol}%d", cents.to_f / 100)
|
|
@@ -239,8 +243,9 @@ class Money
|
|
|
239
243
|
formatted << currency
|
|
240
244
|
formatted << '</span>' if rules[:html]
|
|
241
245
|
end
|
|
246
|
+
formatted.gsub!(CURRENCIES[currency][:symbol],CURRENCIES[currency][:html]) if rules[:html]
|
|
242
247
|
formatted
|
|
243
|
-
|
|
248
|
+
end
|
|
244
249
|
|
|
245
250
|
def normalize_formatting_rules(rules)
|
|
246
251
|
if rules.size == 1
|
|
@@ -261,6 +266,11 @@ class Money
|
|
|
261
266
|
sprintf("%.2f", cents / 100.0)
|
|
262
267
|
end
|
|
263
268
|
|
|
269
|
+
# Money.new(123).to_i => "100"
|
|
270
|
+
def to_i
|
|
271
|
+
cents
|
|
272
|
+
end
|
|
273
|
+
|
|
264
274
|
# Money.ca_dollar(100).to_f => "1.0"
|
|
265
275
|
def to_f
|
|
266
276
|
cents / 100.0
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
require 'thread'
|
|
2
1
|
require 'money/errors'
|
|
2
|
+
require 'net/http'
|
|
3
|
+
require 'rubygems'
|
|
4
|
+
require 'hpricot'
|
|
3
5
|
|
|
4
6
|
# Class for aiding in exchanging money between different currencies.
|
|
5
7
|
# By default, the Money class uses an object of this class (accessible through
|
|
@@ -10,13 +12,13 @@ require 'money/errors'
|
|
|
10
12
|
# exchanges with +exchange+. For example:
|
|
11
13
|
#
|
|
12
14
|
# bank = Money::VariableExchangeBank.new
|
|
13
|
-
# bank.add_rate("
|
|
14
|
-
# bank.add_rate("
|
|
15
|
+
# bank.add_rate("CAD", 0.803115)
|
|
16
|
+
# bank.add_rate("USD", 1.24515)
|
|
15
17
|
#
|
|
16
18
|
# # Exchange 100 CAD to USD:
|
|
17
|
-
# bank.exchange(100_00, "CAD", "USD") # =>
|
|
19
|
+
# bank.exchange(100_00, "CAD", "USD") # => 15504
|
|
18
20
|
# # Exchange 100 USD to CAD:
|
|
19
|
-
# bank.exchange(100_00, "USD", "CAD") # =>
|
|
21
|
+
# bank.exchange(100_00, "USD", "CAD") # => 6450
|
|
20
22
|
class Money
|
|
21
23
|
class VariableExchangeBank
|
|
22
24
|
# Returns the singleton instance of VariableExchangeBank.
|
|
@@ -28,22 +30,16 @@ class Money
|
|
|
28
30
|
|
|
29
31
|
def initialize
|
|
30
32
|
@rates = {}
|
|
31
|
-
@
|
|
33
|
+
@rates["USD"] = 1.0
|
|
32
34
|
end
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
@mutex.synchronize do
|
|
37
|
-
@rates["#{from}_TO_#{to}".upcase] = rate
|
|
38
|
-
end
|
|
35
|
+
|
|
36
|
+
def add_rate(currency, rate)
|
|
37
|
+
@rates[currency.upcase] = (currency.upcase != Money.default_currency) ? (rate * @rates[Money.default_currency]) : rate
|
|
39
38
|
end
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
@mutex.synchronize do
|
|
45
|
-
@rates["#{from}_TO_#{to}".upcase]
|
|
46
|
-
end
|
|
39
|
+
|
|
40
|
+
def get_rate(currency = nil)
|
|
41
|
+
return nil unless @rates[currency]
|
|
42
|
+
(currency != Money.default_currency) ? @rates[currency.upcase] / @rates[Money.default_currency] : @rates[currency.upcase]
|
|
47
43
|
end
|
|
48
44
|
|
|
49
45
|
# Given two currency names, checks whether they're both the same currency.
|
|
@@ -60,11 +56,44 @@ class Money
|
|
|
60
56
|
#
|
|
61
57
|
# If the conversion rate is unknown, then Money::UnknownRate will be raised.
|
|
62
58
|
def exchange(cents, from_currency, to_currency)
|
|
63
|
-
|
|
64
|
-
|
|
59
|
+
from_currency.upcase!
|
|
60
|
+
to_currency.upcase!
|
|
61
|
+
if !@rates[from_currency] or !@rates[to_currency]
|
|
65
62
|
raise Money::UnknownRate, "No conversion rate known for '#{from_currency}' -> '#{to_currency}'"
|
|
66
63
|
end
|
|
67
|
-
(cents *
|
|
64
|
+
((cents / @rates[from_currency]) * @rates[to_currency]).round
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Fetch rates
|
|
68
|
+
def fetch_rates
|
|
69
|
+
xml = Hpricot.XML(
|
|
70
|
+
Net::HTTP.get(
|
|
71
|
+
URI.parse('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml')
|
|
72
|
+
)
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
@rates["EUR"] = 1.0
|
|
76
|
+
(xml/:Cube).each do |ele|
|
|
77
|
+
@rates[ele['currency'].upcase] = ele['rate'].to_f if ele['currency']
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
# Auto fetch the currencies every X seconds
|
|
83
|
+
# if no time is give, will fetch every hour
|
|
84
|
+
def auto_fetch(time = 60*60)
|
|
85
|
+
@auto_fetch.kill if (@auto_fetch && @auto_fetch.alive?)
|
|
86
|
+
@auto_fetch = Thread.new {
|
|
87
|
+
loop do
|
|
88
|
+
self.fetch_rates
|
|
89
|
+
sleep time
|
|
90
|
+
end
|
|
91
|
+
}
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# stop auto fetch
|
|
95
|
+
def stop_fetch
|
|
96
|
+
@auto_fetch.kill if (@auto_fetch && @auto_fetch.alive?)
|
|
68
97
|
end
|
|
69
98
|
|
|
70
99
|
@@singleton = VariableExchangeBank.new
|
data/money.gemspec
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
Gem::Specification.new do |s|
|
|
4
4
|
s.name = %q{money}
|
|
5
|
-
s.version = "2.3.
|
|
5
|
+
s.version = "2.3.3"
|
|
6
6
|
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
8
8
|
s.authors = ["Money Team"]
|
|
9
|
-
s.date = %q{2009-03-
|
|
9
|
+
s.date = %q{2009-03-24}
|
|
10
10
|
s.description = %q{This library aids one in handling money and different currencies.}
|
|
11
11
|
s.email = ["see@readme"]
|
|
12
12
|
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"]
|
|
13
|
-
s.files = ["History.txt", "MIT-LICENSE", "Manifest.txt", "README.rdoc", "Rakefile", "lib/money.rb", "lib/money/acts_as_money.rb", "lib/money/core_extensions.rb", "lib/money/errors.rb", "lib/money/money.rb", "lib/money/variable_exchange_bank.rb", "money.gemspec", "rails/init.rb", "script/console", "script/destroy", "script/generate", "spec/db/database.yml", "spec/db/schema.rb", "spec/money/acts_as_money_spec.rb", "spec/money/core_extensions_spec.rb", "spec/money/exchange_bank_spec.rb", "spec/money/money_spec.rb", "spec/spec.opts", "spec/spec_helper.rb"
|
|
13
|
+
s.files = ["History.txt", "MIT-LICENSE", "Manifest.txt", "README.rdoc", "Rakefile", "lib/money.rb", "lib/money/acts_as_money.rb", "lib/money/core_extensions.rb", "lib/money/errors.rb", "lib/money/money.rb", "lib/money/variable_exchange_bank.rb", "money.gemspec", "rails/init.rb", "script/console", "script/destroy", "script/generate", "spec/db/database.yml", "spec/db/schema.rb", "spec/money/acts_as_money_spec.rb", "spec/money/core_extensions_spec.rb", "spec/money/exchange_bank_spec.rb", "spec/money/money_spec.rb", "spec/spec.opts", "spec/spec_helper.rb" ]
|
|
14
14
|
s.has_rdoc = true
|
|
15
15
|
s.homepage = %q{http://github.com/ShadowBelmolve/money}
|
|
16
16
|
s.rdoc_options = ["--main", "README.rdoc"]
|
|
@@ -24,14 +24,14 @@ Gem::Specification.new do |s|
|
|
|
24
24
|
s.specification_version = 2
|
|
25
25
|
|
|
26
26
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
|
27
|
-
s.add_development_dependency(%q<newgem>, [">= 1.
|
|
27
|
+
s.add_development_dependency(%q<newgem>, [">= 1.3.0"])
|
|
28
28
|
s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
|
|
29
29
|
else
|
|
30
|
-
s.add_dependency(%q<newgem>, [">= 1.
|
|
30
|
+
s.add_dependency(%q<newgem>, [">= 1.3.0"])
|
|
31
31
|
s.add_dependency(%q<hoe>, [">= 1.8.0"])
|
|
32
32
|
end
|
|
33
33
|
else
|
|
34
|
-
s.add_dependency(%q<newgem>, [">= 1.
|
|
34
|
+
s.add_dependency(%q<newgem>, [">= 1.3.0"])
|
|
35
35
|
s.add_dependency(%q<hoe>, [">= 1.8.0"])
|
|
36
36
|
end
|
|
37
37
|
end
|
|
@@ -6,39 +6,41 @@ describe Money::VariableExchangeBank do
|
|
|
6
6
|
end
|
|
7
7
|
|
|
8
8
|
it "returns the previously specified conversion rate" do
|
|
9
|
-
@bank.add_rate("USD",
|
|
10
|
-
@bank.add_rate("EUR",
|
|
11
|
-
@bank.get_rate("USD"
|
|
12
|
-
@bank.get_rate("EUR"
|
|
9
|
+
@bank.add_rate("USD", 0.788332676)
|
|
10
|
+
@bank.add_rate("EUR", 122.631477)
|
|
11
|
+
@bank.get_rate("USD").should == 0.788332676
|
|
12
|
+
@bank.get_rate("EUR").should == 122.631477
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
it "treats currency names case-insensitively" do
|
|
16
|
-
@bank.add_rate("usd",
|
|
17
|
-
@bank.get_rate("USD"
|
|
16
|
+
@bank.add_rate("usd", 1)
|
|
17
|
+
@bank.get_rate("USD").should == 1
|
|
18
18
|
@bank.same_currency?("USD", "usd").should be_true
|
|
19
19
|
@bank.same_currency?("EUR", "usd").should be_false
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
it "returns nil if the conversion rate is unknown" do
|
|
23
|
-
@bank.get_rate("American Pesos"
|
|
23
|
+
@bank.get_rate("American Pesos").should be_nil
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
it "exchanges money from one currency to another according to the specified conversion rates" do
|
|
27
|
-
@bank.add_rate("USD",
|
|
28
|
-
@bank.add_rate("EUR",
|
|
27
|
+
@bank.add_rate("USD", 1.0)
|
|
28
|
+
@bank.add_rate("EUR", 0.5)
|
|
29
|
+
@bank.add_rate("YEN", 5)
|
|
29
30
|
@bank.exchange(10_00, "USD", "EUR").should == 5_00
|
|
30
31
|
@bank.exchange(500_00, "EUR", "YEN").should == 5000_00
|
|
31
32
|
end
|
|
32
33
|
|
|
33
34
|
it "rounds the exchanged result down" do
|
|
34
|
-
@bank.add_rate("USD",
|
|
35
|
-
@bank.add_rate("EUR",
|
|
35
|
+
@bank.add_rate("USD", 1.0)
|
|
36
|
+
@bank.add_rate("EUR", 0.788332676)
|
|
37
|
+
@bank.add_rate("YEN", 122.631477)
|
|
36
38
|
@bank.exchange(10_00, "USD", "EUR").should == 788
|
|
37
|
-
@bank.exchange(500_00, "EUR", "YEN").should ==
|
|
39
|
+
@bank.exchange(500_00, "EUR", "YEN").should == 7777901
|
|
38
40
|
end
|
|
39
41
|
|
|
40
42
|
it "raises Money::UnknownRate upon conversion if the conversion rate is unknown" do
|
|
41
|
-
block = lambda { @bank.exchange(10, "USD", "
|
|
43
|
+
block = lambda { @bank.exchange(10, "USD", "ABC") }
|
|
42
44
|
block.should raise_error(Money::UnknownRate)
|
|
43
45
|
end
|
|
44
46
|
end
|
data/spec/money/money_spec.rb
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
|
3
3
|
|
|
4
4
|
describe Money do
|
|
5
|
+
|
|
6
|
+
it { Money.new(10_00).to_f.should eql(10.0) }
|
|
7
|
+
it { Money.new(10_00).to_s.should eql("10.00") }
|
|
8
|
+
|
|
5
9
|
it "is associated to the singleton instance of VariableExchangeBank by default" do
|
|
6
10
|
Money.new(0).bank.object_id.should == Money::VariableExchangeBank.instance.object_id
|
|
7
11
|
end
|
|
@@ -11,9 +15,13 @@ describe Money do
|
|
|
11
15
|
end
|
|
12
16
|
|
|
13
17
|
it "should rounds the given cents to an integer" do
|
|
14
|
-
Money.new(1.
|
|
18
|
+
Money.new(1.0, "USD").cents.should == 1
|
|
15
19
|
Money.new(1.01, "USD").cents.should == 1
|
|
16
|
-
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "should use the floor method" do
|
|
23
|
+
Money.new(1.50, "USD").cents.should == 1
|
|
24
|
+
Money.new(1.99, "USD").cents.should == 1
|
|
17
25
|
end
|
|
18
26
|
|
|
19
27
|
it "#currency returns the currency passed to the constructor" do
|
|
@@ -34,76 +42,32 @@ describe Money do
|
|
|
34
42
|
money.exchange_to("EUR")
|
|
35
43
|
end
|
|
36
44
|
|
|
37
|
-
it "
|
|
45
|
+
it "should exchange_to exchanges the amount properly" do
|
|
38
46
|
money = Money.new(100_00, "USD")
|
|
39
47
|
money.bank.should_receive(:exchange).with(100_00, "USD", "EUR").and_return(200_00)
|
|
40
48
|
money.exchange_to("EUR").should == Money.new(200_00, "EUR")
|
|
41
49
|
end
|
|
42
50
|
|
|
43
|
-
it "
|
|
51
|
+
it "should returns true if and only if their amount and currency are equal" do
|
|
44
52
|
Money.new(1_00, "USD").should == Money.new(1_00, "USD")
|
|
45
53
|
end
|
|
46
54
|
|
|
47
|
-
it "
|
|
48
|
-
(Money.new(1_00, "USD")
|
|
55
|
+
it "should add retaining the currency" do
|
|
56
|
+
(Money.new(1_00, "USD") + 10).should == Money.new(1_10, "USD")
|
|
49
57
|
end
|
|
50
58
|
|
|
51
|
-
it "
|
|
52
|
-
(Money.new(
|
|
59
|
+
it "should substract retaining the currency" do
|
|
60
|
+
(Money.new(1_00, "USD") - 10).should == Money.new(90, "USD")
|
|
53
61
|
end
|
|
54
62
|
|
|
55
|
-
it "
|
|
56
|
-
|
|
57
|
-
@money[0].cents.should eql(334)
|
|
58
|
-
@money[1].cents.should eql(333)
|
|
59
|
-
@money[2].cents.should eql(333)
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
it "# divides the money ammout in installments add first" do
|
|
63
|
-
@money = Money.new(10_00).split_in_installments(3,true)
|
|
64
|
-
@money.to_s.should eql(["3.34", "3.33", "3.33"])
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
it "# divides the money ammout in installments base on payment" do
|
|
68
|
-
money = Money.new(3_00)
|
|
69
|
-
Money.new(10_00).in_installments_of(money)[0].cents.should eql(334)
|
|
70
|
-
Money.new(10_00).in_installments_of(money)[1].cents.should eql(333)
|
|
71
|
-
Money.new(10_00).in_installments_of(money)[2].cents.should eql(333)
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
it "shuld sum array" do
|
|
75
|
-
Money.new(10_00).split_in_installments(3).sum.cents.should eql(1000)
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
it "should calculate tax" do
|
|
79
|
-
Money.new(100).add_tax(20).cents.should eql(120)
|
|
80
|
-
Money.new(100).add_tax(-20).cents.should eql(80)
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
it "should calculate compound tax" do
|
|
84
|
-
@ma = Money.new(1000_00)
|
|
85
|
-
@ma.compound_interest(12.99,12).to_s.should eql("137.92")
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
it "should calculate compound tax" do
|
|
89
|
-
@ma = Money.new(1000_00)
|
|
90
|
-
@ma.simple_interest(12.99,12).to_s.should eql("129.90")
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
it "should calculate compound tax" do
|
|
94
|
-
@ma = Money.new(2500_00)
|
|
95
|
-
@ma.compound_interest(12.99,3).to_s.should eql("82.07")
|
|
63
|
+
it "should multiply while retaining the currency" do
|
|
64
|
+
(Money.new(1_00, "USD") * 10).should == Money.new(10_00, "USD")
|
|
96
65
|
end
|
|
97
66
|
|
|
98
|
-
it "
|
|
99
|
-
|
|
100
|
-
@money.split_in_installments(3).sum.cents.should eql(1100)
|
|
67
|
+
it "should divides while retaining the currency" do
|
|
68
|
+
(Money.new(10_00, "USD") / 10).should == Money.new(1_00, "USD")
|
|
101
69
|
end
|
|
102
70
|
|
|
103
|
-
it { Money.new(10_00).to_f.should eql(10.0) }
|
|
104
|
-
it { Money.new(10_00).to_s.should eql("10.00") }
|
|
105
|
-
|
|
106
|
-
|
|
107
71
|
it "should create a new Money object of 0 cents if empty" do
|
|
108
72
|
Money.empty.should == Money.new(0)
|
|
109
73
|
end
|
|
@@ -124,27 +88,97 @@ describe Money do
|
|
|
124
88
|
Money.real(50).should == Money.new(50, "BRL")
|
|
125
89
|
end
|
|
126
90
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
91
|
+
describe "Installments" do
|
|
92
|
+
|
|
93
|
+
it "# divides the money ammout in installments add first" do
|
|
94
|
+
@money = Money.new(10_00).split_in_installments(3)
|
|
95
|
+
@money[0].cents.should eql(334)
|
|
96
|
+
@money[1].cents.should eql(333)
|
|
97
|
+
@money[2].cents.should eql(333)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
it "# divides the money ammout in installments add last" do
|
|
101
|
+
@money = Money.new(10_00).split_in_installments(3,true)
|
|
102
|
+
@money.to_s.should eql(["3.33", "3.33", "3.34"])
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
it "# divides the money ammout in installments base on payment" do
|
|
106
|
+
money = Money.new(3_00)
|
|
107
|
+
Money.new(10_00).in_installments_of(money)[0].cents.should eql(334)
|
|
108
|
+
Money.new(10_00).in_installments_of(money)[1].cents.should eql(333)
|
|
109
|
+
Money.new(10_00).in_installments_of(money)[2].cents.should eql(333)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
it "shuld sum array" do
|
|
113
|
+
Money.new(10_00).split_in_installments(3).sum.cents.should eql(1000)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
it "should calculate tax" do
|
|
117
|
+
Money.new(100).add_tax(20).cents.should eql(120)
|
|
118
|
+
Money.new(100).add_tax(-20).cents.should eql(80)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
it "shuld to_s wallet" do
|
|
122
|
+
@money = Money.new(10_00)
|
|
123
|
+
@money.split_in_installments(3).to_s.should eql(["3.34", "3.33", "3.33"])
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
it "shuld sum array" do
|
|
127
|
+
@money = Money.new(10_00).add_tax(10)
|
|
128
|
+
@money.split_in_installments(3).sum.cents.should eql(1100)
|
|
129
|
+
end
|
|
130
|
+
|
|
130
131
|
end
|
|
131
132
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
133
|
+
describe "Taxes and Interest" do
|
|
134
|
+
|
|
135
|
+
it "Money.add_rate works" do
|
|
136
|
+
Money.add_rate("USD", 1.0)
|
|
137
|
+
Money.add_rate("EUR", 10)
|
|
138
|
+
Money.new(10_00, "EUR").exchange_to("USD").should == Money.new(1_00, "USD")
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
it "Money method missing exchange" do
|
|
142
|
+
Money.add_rate("EUR", 1.0)
|
|
143
|
+
Money.add_rate("BRL", 10)
|
|
144
|
+
Money.new(10_00, "EUR").as_brl.should == Money.new(100_00, "BRL")
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
it "should calculate compound tax" do
|
|
148
|
+
m = Money.new(1000_00)
|
|
149
|
+
m.compound_interest(12.99,12).to_s.should eql("137.91")
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
it "should simple interest" do
|
|
153
|
+
m = Money.new(1000_00)
|
|
154
|
+
m.simple_interest(12.99,12).to_s.should eql("129.90")
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
it "should calculate compound interest" do
|
|
158
|
+
m = Money.new(2500_00)
|
|
159
|
+
m.compound_interest(12.99,3).to_s.should eql("82.06")
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
it "should calculate compound interest" do
|
|
163
|
+
m = Money.new(2500_00)
|
|
164
|
+
m.compound_interest(12.99,3,6).to_s.should eql("165.91")
|
|
165
|
+
end
|
|
166
|
+
|
|
135
167
|
end
|
|
136
168
|
|
|
137
169
|
describe "Format out " do
|
|
138
170
|
|
|
139
171
|
describe "Options" do
|
|
140
172
|
before(:each) do
|
|
141
|
-
@cash =
|
|
173
|
+
@cash = Money.new(2_00, "JPY")
|
|
142
174
|
end
|
|
143
175
|
|
|
144
|
-
it { @cash.format.should eql("
|
|
176
|
+
it { @cash.format.should eql("¥2.00") }
|
|
145
177
|
it { @cash.format(:symbol => "R$ ").should eql("R$ 2.00") }
|
|
146
|
-
it { @cash.format(:no_cents => true).should eql("
|
|
178
|
+
it { @cash.format(:no_cents => true).should eql("¥2") }
|
|
147
179
|
it { @cash.format(:no_cents => true, :symbol => "R$ ").should eql("R$ 2") }
|
|
180
|
+
it { @cash.format(:html => true).should eql("¥2.00") }
|
|
181
|
+
|
|
148
182
|
end
|
|
149
183
|
|
|
150
184
|
it { Money.new(0).format.should eql("$0.00") }
|
data/spec/spec_helper.rb
CHANGED
|
@@ -14,7 +14,6 @@ config = YAML.load_file(File.dirname(__FILE__) + '/db/database.yml')
|
|
|
14
14
|
ActiveRecord::Base.logger = Logger.new("/tmp/money-debug.log")
|
|
15
15
|
ActiveRecord::Base.establish_connection(config)
|
|
16
16
|
|
|
17
|
+
db_file = File.exists?(File.dirname(__FILE__) + '/../tmp/acts_as_money.sqlite3')
|
|
18
|
+
load(File.dirname(__FILE__) + "/db/schema.rb") unless db_file
|
|
17
19
|
|
|
18
|
-
def load_schema
|
|
19
|
-
load(File.dirname(__FILE__) + "/db/schema.rb")
|
|
20
|
-
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ShadowBelmolve-money
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.3.
|
|
4
|
+
version: 2.3.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Money Team
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
|
|
12
|
-
date: 2009-03-
|
|
12
|
+
date: 2009-03-24 00:00:00 -07:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
@@ -20,7 +20,7 @@ dependencies:
|
|
|
20
20
|
requirements:
|
|
21
21
|
- - ">="
|
|
22
22
|
- !ruby/object:Gem::Version
|
|
23
|
-
version: 1.
|
|
23
|
+
version: 1.3.0
|
|
24
24
|
version:
|
|
25
25
|
- !ruby/object:Gem::Dependency
|
|
26
26
|
name: hoe
|
|
@@ -68,7 +68,6 @@ files:
|
|
|
68
68
|
- spec/money/money_spec.rb
|
|
69
69
|
- spec/spec.opts
|
|
70
70
|
- spec/spec_helper.rb
|
|
71
|
-
- tmp/acts_as_money.sqlite3
|
|
72
71
|
has_rdoc: true
|
|
73
72
|
homepage: http://github.com/ShadowBelmolve/money
|
|
74
73
|
post_install_message:
|