ShadowBelmolve-money 2.3.4 → 2.3.5

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/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ *.gem
2
+ doc
3
+ *.sqlite3
4
+ *.log
5
+ spec/*.log
6
+ tmp/**/*
7
+ db/**/*
8
+ coverage/*
9
+ *.swp
data/README.rdoc CHANGED
@@ -28,7 +28,7 @@ Install stable releases with the following command:
28
28
  The development version (hosted on Github) can be installed with:
29
29
 
30
30
  gem sources -a http://gems.github.com
31
- gem install ShadowBelmolve-money
31
+ gem install nofxx-money
32
32
 
33
33
 
34
34
  == Usage
@@ -96,7 +96,7 @@ fields on the database.
96
96
 
97
97
  config/enviroment.rb
98
98
 
99
- require.gem 'ShadowBelmolve-money', :lib => 'money'
99
+ require.gem 'nofxx-money', :lib => 'money'
100
100
 
101
101
  app/models/product.rb
102
102
 
data/Rakefile CHANGED
@@ -1,27 +1,48 @@
1
- %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
- require File.dirname(__FILE__) + '/lib/money'
3
-
4
- # Generate all the Rake tasks
5
- # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
- $hoe = Hoe.new('money', Money::VERSION) do |p|
7
- p.developer('Money Team', 'see@readme')
8
- p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
- p.rubyforge_name = p.name
10
- p.summary = "This library aids one in handling money and different currencies."
11
- p.description = "This library aids one in handling money and different currencies."
12
- p.url = "http://github.com/nofxx/money"
13
-
14
-
15
- p.extra_dev_deps = [
16
- ['newgem', ">= #{::Newgem::VERSION}"]
17
- ]
18
-
19
- p.clean_globs |= %w[**/.DS_Store *.log]
20
- path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
21
- p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
22
- p.rsync_args = '-av --delete --ignore-errors'
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "money"
8
+ gem.summary = "This library aids one in handling money and different currencies."
9
+ gem.description = "This library aids one in handling money and different currencies."
10
+ gem.email = "see@reame"
11
+ gem.homepage = "http://github.com/nofxx/money"
12
+ gem.authors = ["Money Team"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
23
18
  end
24
19
 
25
- require 'newgem/tasks' # load /tasks/*.rake
26
- Dir['tasks/**/*.rake'].each { |t| load t }
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+ task :default => :spec
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ if File.exist?('VERSION.yml')
37
+ config = YAML.load(File.read('VERSION.yml'))
38
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
39
+ else
40
+ version = ""
41
+ end
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "money #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
27
48
 
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 2.3.5
data/lib/money.rb CHANGED
@@ -23,7 +23,3 @@
23
23
  $LOAD_PATH << File.expand_path(File.dirname(__FILE__))
24
24
  require 'money/money'
25
25
  require 'money/core_extensions'
26
-
27
- class Money
28
- VERSION = "2.3.3"
29
- end
@@ -1,89 +1,99 @@
1
1
  require 'money/errors'
2
2
  require 'net/http'
3
3
  require 'rubygems'
4
- require 'hpricot'
5
-
4
+ begin
5
+ require "nokogiri"
6
+ Parser = Nokogiri
7
+ rescue
8
+ require "hpricot"
9
+ Parser = Hpricot
10
+ end
6
11
  # Class for aiding in exchanging money between different currencies.
7
12
  # By default, the Money class uses an object of this class (accessible through
8
13
  # Money#bank) for performing currency exchanges.
9
14
  #
10
- # By default, VariableExchangeBank has no knowledge about conversion rates.
15
+ # By default, ExchangeBank has no knowledge about conversion rates.
11
16
  # One must manually specify them with +add_rate+, after which one can perform
12
17
  # exchanges with +exchange+. For example:
13
18
  #
14
- # bank = Money::VariableExchangeBank.new
19
+ # bank = Money::ExchangeBank.new
15
20
  # bank.add_rate("CAD", 0.803115)
16
21
  # bank.add_rate("USD", 1.24515)
17
- #
22
+ #
18
23
  # # Exchange 100 CAD to USD:
19
24
  # bank.exchange(100_00, "CAD", "USD") # => 15504
20
25
  # # Exchange 100 USD to CAD:
21
26
  # bank.exchange(100_00, "USD", "CAD") # => 6450
22
27
  class Money
23
- class VariableExchangeBank
24
- # Returns the singleton instance of VariableExchangeBank.
28
+ class ExchangeBank
29
+ # Returns the singleton instance of ExchangeBank.
25
30
  #
26
31
  # By default, <tt>Money.default_bank</tt> returns the same object.
27
32
  def self.instance
28
33
  @@singleton
29
34
  end
30
-
35
+
31
36
  def initialize
32
37
  @rates = {}
33
- @rates["USD"] = 1.0
34
38
  @mutex = Mutex.new
35
39
  end
36
40
 
37
- def add_rate(currency, rate)
41
+ def add_rate(*params)
42
+ if rate = params.delete_at(2)
43
+ parse_rate(rate, *params)
44
+ else
45
+ parse_rate(params[1], Money.default_currency, params[0])
46
+ end
47
+ end
48
+
49
+ def parse_rate(rate,from,to)
50
+ return if from.upcase == to.upcase
38
51
  @mutex.synchronize do
39
- @rates[currency.upcase] = (currency.upcase != Money.default_currency) ? (rate * @rates[Money.default_currency]) : rate
52
+ @rates["#{from}<>#{to}".upcase] = rate
53
+ @rates["#{to}<>#{from}".upcase] = 1.0/rate
40
54
  end
41
55
  end
42
56
 
43
- def get_rate(currency = nil)
57
+ def get_rate(from, to = nil)
58
+ from, to = Money.default_currency, from unless to
44
59
  @mutex.synchronize do
45
- return nil unless @rates[currency]
46
- (currency != Money.default_currency) ? @rates[currency.upcase] / @rates[Money.default_currency] : @rates[currency.upcase]
60
+ @rates["#{from}<>#{to}".upcase]
47
61
  end
48
62
  end
49
-
63
+
50
64
  # Given two currency names, checks whether they're both the same currency.
51
65
  #
52
- # bank = VariableExchangeBank.new
66
+ # bank = ExchangeBank.new
53
67
  # bank.same_currency?("usd", "USD") # => true
54
68
  # bank.same_currency?("usd", "EUR") # => false
55
69
  def same_currency?(currency1, currency2)
56
70
  currency1.upcase == currency2.upcase
57
71
  end
58
-
72
+
59
73
  # Exchange the given amount of cents in +from_currency+ to +to_currency+.
60
74
  # Returns the amount of cents in +to_currency+ as an integer, rounded down.
61
75
  #
62
76
  # If the conversion rate is unknown, then Money::UnknownRate will be raised.
63
77
  def exchange(cents, from_currency, to_currency)
64
- from_currency.upcase!
65
- to_currency.upcase!
66
- if !@rates[from_currency] or !@rates[to_currency]
78
+ rate = get_rate(from_currency, to_currency)
79
+ if !rate
67
80
  raise Money::UnknownRate, "No conversion rate known for '#{from_currency}' -> '#{to_currency}'"
68
81
  end
69
- ((cents / @rates[from_currency]) * @rates[to_currency]).round
82
+ (cents * rate).floor
70
83
  end
71
84
 
72
85
  # Fetch rates
73
86
  def fetch_rates
74
- xml = Hpricot.XML(
75
- Net::HTTP.get(
76
- URI.parse('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml')
77
- )
78
- )
79
-
80
- @rates["EUR"] = 1.0
81
- (xml/:Cube).each do |ele|
82
- @rates[ele['currency'].upcase] = ele['rate'].to_f if ele['currency']
87
+ xml = Parser::XML(Net::HTTP.get(URI.parse('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml')))
88
+ curr = (xml/:Cube).select { |r| r["currency"] == Money.default_currency }.first
89
+ diff = Money.default_currency == "EUR" || !curr ? 1 : curr["rate"].to_f
90
+ (xml/:Cube).each do |x|
91
+ parse_rate x['rate'].to_f / diff, curr ? Money.default_currency : "EUR", x['currency'].upcase if x['currency']
83
92
  end
93
+ parse_rate diff, Money.default_currency, "EUR" if curr
94
+ self
84
95
  end
85
96
 
86
-
87
97
  # Auto fetch the currencies every X seconds
88
98
  # if no time is give, will fetch every hour
89
99
  def auto_fetch(time = 60*60)
@@ -100,7 +110,7 @@ class Money
100
110
  def stop_fetch
101
111
  @auto_fetch.kill if (@auto_fetch && @auto_fetch.alive?)
102
112
  end
103
-
104
- @@singleton = VariableExchangeBank.new
113
+
114
+ @@singleton = ExchangeBank.new
105
115
  end
106
116
  end
data/lib/money/money.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  # -*- coding: utf-8 -*-
2
- require 'money/variable_exchange_bank'
2
+ require 'money/exchange_bank'
3
3
 
4
4
  # Represents an amount of money in a certain currency.
5
5
  class Money
6
6
  include Comparable
7
-
8
7
  attr_reader :cents, :currency, :bank
8
+ alias :to_i :cents
9
9
 
10
10
  class << self
11
11
  # Each Money object is associated to a bank object, which is responsible
@@ -14,12 +14,22 @@ class Money
14
14
  #
15
15
  # bank1 = MyBank.new
16
16
  # bank2 = MyOtherBank.new
17
- #e if VariableExchangeBank.
17
+ #
18
+ # Money.default_bank = bank1
19
+ # money1 = Money.new(10)
20
+ # money1.bank # => bank1
21
+ #
22
+ # Money.default_bank = bank2
23
+ # money2 = Money.new(10)
24
+ # money2.bank # => bank2
25
+ # money1.bank # => bank1
26
+ #
27
+ # The default value for this property is an instance if VariableExchangeBank.
18
28
  # It allows one to specify custom exchange rates:
19
29
  #
20
30
  # Money.default_bank.add_rate("USD", "CAD", 1.24515)
21
31
  # Money.default_bank.add_rate("CAD", "USD", 0.803115)
22
- # Money.us_dollar(100).exchange_to("CAD") # => MONEY.ca_dollar(124)
32
+ # Money.us_dollar(100).exchange_to("CAD") # => Money.ca_dollar(124)
23
33
  # Money.ca_dollar(100).exchange_to("USD") # => Money.us_dollar(80)
24
34
  attr_accessor :default_bank
25
35
 
@@ -28,7 +38,7 @@ class Money
28
38
  attr_accessor :default_currency
29
39
  end
30
40
 
31
- self.default_bank = VariableExchangeBank.instance
41
+ self.default_bank = ExchangeBank.instance
32
42
  self.default_currency = "USD"
33
43
 
34
44
  CURRENCIES = {
@@ -67,8 +77,8 @@ class Money
67
77
  Money.new(cents, "BRL")
68
78
  end
69
79
 
70
- def self.add_rate(currency, rate)
71
- Money.default_bank.add_rate(currency, rate)
80
+ def self.add_rate(*params)
81
+ Money.default_bank.add_rate(*params)
72
82
  end
73
83
 
74
84
  # Creates a new money object.
@@ -78,7 +88,7 @@ class Money
78
88
  # Money.ca_dollar and Money.us_dollar
79
89
  def initialize(cents, currency = nil, bank = nil)
80
90
  @cents = cents.to_i
81
- @currency = currency || Money.default_currency
91
+ @currency = (currency || Money.default_currency).upcase
82
92
  @bank = bank || Money.default_bank
83
93
  end
84
94
 
@@ -114,11 +124,6 @@ class Money
114
124
  end
115
125
  end
116
126
 
117
- # get the cents value of the object
118
- def cents
119
- @cents
120
- end
121
-
122
127
  # multiply money by fixnum
123
128
  def *(fixnum)
124
129
  Money.new(cents * fixnum, currency)
@@ -150,21 +155,22 @@ class Money
150
155
  Money.new(rate / 100 / period * cents * count)
151
156
  end
152
157
 
153
- #Round to nearest coin value
154
- # basically, we don't have coins for cents in CZK,
155
- # our smallest fraction is 0.50CZK
158
+ # Round to nearest coin value
159
+ # basically, we don't have coins for cents in CZK,
160
+ # our smallest fraction is 0.50CZK
161
+ #
162
+ # Money.new(14_58).round_to_coin(50) => 14.50
156
163
  #
157
- #Money.new(14_58).round_to_coin(50) => 14.50
158
164
  def round_to_coin(coin)
159
165
  coef = 1.0/coin
160
- val = (cents * coef).round / coef
166
+ val = (cents * coef).floor / coef
161
167
  Money.new(val, currency)
162
168
  end
163
169
 
164
- #Returns array a where
165
- # a[0] is price _after_ applying tax (tax base)
166
- # a[1] is tax
167
- def tax_brakedown(tax)
170
+ # Returns array a where
171
+ # a[0] is price _after_ applying tax (tax base)
172
+ # a[1] is tax
173
+ def tax_breakdown(tax)
168
174
  _tax = (cents * (tax / 100.0)).round
169
175
  [Money.new(cents + _tax, currency), Money.new(_tax, currency)]
170
176
  end
@@ -172,16 +178,16 @@ class Money
172
178
  #Returns array a where
173
179
  # a[0] is price _before_ applying tax (tax base)
174
180
  # a[1] is tax
175
- def tax_reverse_brakedown(tax)
181
+ def tax_reverse_breakdown(tax)
176
182
  coef = tax/100.0
177
- [Money.new((cents / (1+coef)).round, currency),
183
+ [Money.new((cents / (1+coef)).round, currency),
178
184
  Money.new((cents*coef/(1+coef)).round, currency) ]
179
185
  end
180
-
186
+
181
187
  # Just a helper if you got tax inputs in percentage.
182
188
  # Ie. add_tax(20) => cents * 1.20
183
189
  def add_tax(tax)
184
- tax_brakedown(tax)[0]
190
+ tax_breakdown(tax)[0]
185
191
  end
186
192
 
187
193
  # Split money in number of installments
@@ -250,17 +256,20 @@ class Money
250
256
  symbol = ""
251
257
  end
252
258
  else
253
- symbol = CURRENCIES[currency][:symbol]
259
+ symbol = (CURRENCIES[currency] ? CURRENCIES[currency][:symbol] : "$")
254
260
  end
255
261
  self.currency
256
262
 
263
+ delimiter = (CURRENCIES[currency] ? CURRENCIES[currency][:delimiter] : "," )
264
+ separator = (CURRENCIES[currency] ? CURRENCIES[currency][:separator] : "." )
265
+
257
266
  if rules[:no_cents]
258
267
  formatted = sprintf("#{symbol}%d", cents.to_f / 100)
259
- formatted.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{CURRENCIES[currency][:delimiter]}")
268
+ formatted.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
260
269
  else
261
270
  formatted = sprintf("#{symbol}%.2f", cents.to_f / 100).split('.')
262
- formatted[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{CURRENCIES[currency][:delimiter]}")
263
- formatted = formatted.join(CURRENCIES[currency][:separator])
271
+ formatted[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
272
+ formatted = formatted.join(separator)
264
273
  end
265
274
 
266
275
  # Commify ("10000" => "10,000")
@@ -272,7 +281,7 @@ class Money
272
281
  formatted << currency
273
282
  formatted << '</span>' if rules[:html]
274
283
  end
275
- formatted.gsub!(CURRENCIES[currency][:symbol],CURRENCIES[currency][:html]) if rules[:html]
284
+ formatted.gsub!(symbol,CURRENCIES[currency][:html]) if rules[:html]
276
285
  formatted
277
286
  end
278
287
 
@@ -289,17 +298,11 @@ class Money
289
298
  rules
290
299
  end
291
300
 
292
-
293
301
  # Money.ca_dollar(100).to_s => "1.00"
294
302
  def to_s
295
303
  sprintf("%.2f", cents / 100.0)
296
304
  end
297
305
 
298
- # Money.new(123).to_i => "100"
299
- def to_i
300
- cents
301
- end
302
-
303
306
  # Money.ca_dollar(100).to_f => "1.0"
304
307
  def to_f
305
308
  cents / 100.0
@@ -326,7 +329,7 @@ end
326
329
 
327
330
  #
328
331
  # Represent a financial array.
329
- # Investment/Time/Installments...
332
+ # Investment/Time/Installments...TODO...
330
333
  #
331
334
  class Wallet < Array
332
335
 
data/money.gemspec CHANGED
@@ -2,36 +2,63 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{money}
5
- s.version = "2.3.4"
5
+ s.version = "2.3.5"
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-24}
9
+ s.date = %q{2009-05-29}
10
10
  s.description = %q{This library aids one in handling money and different currencies.}
11
- s.email = ["see@readme"]
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" ]
14
- s.has_rdoc = true
15
- s.homepage = %q{http://github.com/ShadowBelmolve/money}
16
- s.rdoc_options = ["--main", "README.rdoc"]
11
+ s.email = %q{see@reame}
12
+ s.extra_rdoc_files = [
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".gitignore",
17
+ "History.txt",
18
+ "MIT-LICENSE",
19
+ "Manifest.txt",
20
+ "README.rdoc",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "lib/money.rb",
24
+ "lib/money/acts_as_money.rb",
25
+ "lib/money/core_extensions.rb",
26
+ "lib/money/errors.rb",
27
+ "lib/money/exchange_bank.rb",
28
+ "lib/money/money.rb",
29
+ "money.gemspec",
30
+ "rails/init.rb",
31
+ "spec/db/database.yml",
32
+ "spec/db/schema.rb",
33
+ "spec/money/acts_as_money_spec.rb",
34
+ "spec/money/core_extensions_spec.rb",
35
+ "spec/money/exchange_bank_spec.rb",
36
+ "spec/money/money_spec.rb",
37
+ "spec/spec.opts",
38
+ "spec/spec_helper.rb",
39
+ "tmp/.gitignore"
40
+ ]
41
+ s.homepage = %q{http://github.com/nofxx/money}
42
+ s.rdoc_options = ["--charset=UTF-8"]
17
43
  s.require_paths = ["lib"]
18
- s.rubyforge_project = %q{money}
19
- s.rubygems_version = %q{1.3.1}
44
+ s.rubygems_version = %q{1.3.3}
20
45
  s.summary = %q{This library aids one in handling money and different currencies.}
46
+ s.test_files = [
47
+ "spec/money/exchange_bank_spec.rb",
48
+ "spec/money/acts_as_money_spec.rb",
49
+ "spec/money/money_spec.rb",
50
+ "spec/money/core_extensions_spec.rb",
51
+ "spec/db/schema.rb",
52
+ "spec/spec_helper.rb"
53
+ ]
21
54
 
22
55
  if s.respond_to? :specification_version then
23
56
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
- s.specification_version = 2
57
+ s.specification_version = 3
25
58
 
26
59
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
- s.add_development_dependency(%q<newgem>, [">= 1.3.0"])
28
- s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
29
60
  else
30
- s.add_dependency(%q<newgem>, [">= 1.3.0"])
31
- s.add_dependency(%q<hoe>, [">= 1.8.0"])
32
61
  end
33
62
  else
34
- s.add_dependency(%q<newgem>, [">= 1.3.0"])
35
- s.add_dependency(%q<hoe>, [">= 1.8.0"])
36
63
  end
37
64
  end
@@ -1,46 +1,142 @@
1
1
  require File.dirname(__FILE__) + '/../spec_helper.rb'
2
2
 
3
- describe Money::VariableExchangeBank do
3
+ describe Money::ExchangeBank do
4
4
  before :each do
5
- @bank = Money::VariableExchangeBank.new
5
+ @bank = Money::ExchangeBank.new
6
+ end
7
+
8
+ it "returns the previously specified conversion rate" do
9
+ @bank.add_rate("USD", "EUR", 0.788332676)
10
+ @bank.add_rate("EUR", "YEN", 122.631477)
11
+ @bank.get_rate("USD", "EUR").should == 0.788332676
12
+ @bank.get_rate("EUR", "YEN").should == 122.631477
13
+ end
14
+
15
+ it "returns the previously specified conversion rate" do
16
+ @bank.add_rate("USD", "EUR", 0.788332676)
17
+ @bank.add_rate("EUR", "YEN", 122.631477)
18
+ @bank.get_rate("USD", "EUR").should == 0.788332676
19
+ @bank.get_rate("EUR", "YEN").should == 122.631477
20
+ end
21
+
22
+ it "treats currency names case-insensitively" do
23
+ @bank.add_rate("usd", "eur", 1)
24
+ @bank.get_rate("USD", "EUR").should == 1
25
+ @bank.same_currency?("USD", "usd").should be_true
26
+ @bank.same_currency?("EUR", "usd").should be_false
27
+ end
28
+
29
+ it "returns nil if the conversion rate is unknown" do
30
+ @bank.get_rate("American Pesos", "EUR").should be_nil
31
+ end
32
+
33
+ it "exchanges money from one currency to another according to the specified conversion rates" do
34
+ @bank.add_rate("USD", "EUR", 0.5)
35
+ @bank.add_rate("EUR", "YEN", 10)
36
+ @bank.exchange(10_00, "USD", "EUR").should == 5_00
37
+ @bank.exchange(500_00, "EUR", "YEN").should == 5000_00
38
+ end
39
+
40
+ it "rounds the exchanged result down" do
41
+ @bank.add_rate("USD", "EUR", 0.788332676)
42
+ @bank.add_rate("EUR", "YEN", 122.631477)
43
+ @bank.exchange(10_00, "USD", "EUR").should == 7_88
44
+ @bank.exchange(500_00, "EUR", "YEN").should == 6131573
6
45
  end
7
46
 
8
47
  it "returns the previously specified conversion rate" do
9
- @bank.add_rate("USD", 0.788332676)
10
48
  @bank.add_rate("EUR", 122.631477)
11
- @bank.get_rate("USD").should == 0.788332676
12
49
  @bank.get_rate("EUR").should == 122.631477
13
50
  end
14
51
 
15
52
  it "treats currency names case-insensitively" do
16
- @bank.add_rate("usd", 1)
17
- @bank.get_rate("USD").should == 1
53
+ @bank.add_rate("yen", 1)
54
+ @bank.get_rate("YEN").should == 1
18
55
  @bank.same_currency?("USD", "usd").should be_true
19
56
  @bank.same_currency?("EUR", "usd").should be_false
20
57
  end
21
58
 
59
+ it "should work with a different default currency" do
60
+ Money.default_currency = "BRL"
61
+ @bank.add_rate("yen", 10)
62
+ @bank.exchange(5_00, "brl", "yen").should == 50_00
63
+ Money.default_currency = "USD"
64
+ end
65
+
22
66
  it "returns nil if the conversion rate is unknown" do
23
67
  @bank.get_rate("American Pesos").should be_nil
24
68
  end
25
69
 
26
70
  it "exchanges money from one currency to another according to the specified conversion rates" do
27
- @bank.add_rate("USD", 1.0)
28
71
  @bank.add_rate("EUR", 0.5)
29
72
  @bank.add_rate("YEN", 5)
30
73
  @bank.exchange(10_00, "USD", "EUR").should == 5_00
31
- @bank.exchange(500_00, "EUR", "YEN").should == 5000_00
74
+ @bank.exchange(500_00, "USD", "YEN").should == 2500_00
75
+ @bank.exchange(2500_00, "YEN", "USD").should == 500_00
32
76
  end
33
77
 
34
- it "rounds the exchanged result down" do
35
- @bank.add_rate("USD", 1.0)
78
+ it "should deal fine with FOO_TO_FOO" do
79
+ @bank.add_rate("USD", 2.0)
80
+ @bank.get_rate("USD").should be_nil
81
+ end
82
+
83
+ it "should test some more" do
36
84
  @bank.add_rate("EUR", 0.788332676)
37
85
  @bank.add_rate("YEN", 122.631477)
38
86
  @bank.exchange(10_00, "USD", "EUR").should == 788
39
- @bank.exchange(500_00, "EUR", "YEN").should == 7777901
87
+ @bank.exchange(500_00, "USD", "YEN").should == 6131573
40
88
  end
41
89
 
42
90
  it "raises Money::UnknownRate upon conversion if the conversion rate is unknown" do
43
91
  block = lambda { @bank.exchange(10, "USD", "ABC") }
44
92
  block.should raise_error(Money::UnknownRate)
45
93
  end
94
+
95
+ describe "Fetching Data" do
96
+
97
+ before(:each) do
98
+ URI.should_receive(:parse).with("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml").and_return(:uri)
99
+ Net::HTTP.should_receive(:get).with(:uri).and_return("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gesmes:Envelope xmlns:gesmes=\"http://www.gesmes.org/xml/2002-08-01\" xmlns=\"http://www.ecb.int/vocabulary/2002-08-01/eurofxref\">\n\t<gesmes:subject>Reference rates</gesmes:subject>\n\t<gesmes:Sender>\n\t\t<gesmes:name>European Central Bank</gesmes:name>\n\t</gesmes:Sender>\n\t<Cube>\n\t\t<Cube time='2009-05-29'>\n\t\t\t<Cube currency='USD' rate='1.4098'/>\n\t\t\t<Cube currency='JPY' rate='135.22'/>\n\t\t\t<Cube currency='BGN' rate='1.9558'/>\n\t\t\t<Cube currency='CZK' rate='26.825'/>\n\t\t\t<Cube currency='DKK' rate='7.4453'/>\n\t\t\t<Cube currency='EEK' rate='15.6466'/>\n\t\t\t<Cube currency='GBP' rate='0.87290'/>\n\t\t\t<Cube currency='HUF' rate='282.48'/>\n\t\t\t<Cube currency='LTL' rate='3.4528'/>\n\t\t\t<Cube currency='LVL' rate='0.7093'/>\n\t\t\t<Cube currency='PLN' rate='4.4762'/>\n\t\t\t<Cube currency='RON' rate='4.1825'/>\n\t\t\t<Cube currency='SEK' rate='10.6678'/>\n\t\t\t<Cube currency='CHF' rate='1.5128'/>\n\t\t\t<Cube currency='NOK' rate='8.8785'/>\n\t\t\t<Cube currency='HRK' rate='7.3500'/>\n\t\t\t<Cube currency='RUB' rate='43.4455'/>\n\t\t\t<Cube currency='TRY' rate='2.1737'/>\n\t\t\t<Cube currency='AUD' rate='1.7671'/>\n\t\t\t<Cube currency='BRL' rate='2.8320'/>\n\t\t\t<Cube currency='CAD' rate='1.5501'/>\n\t\t\t<Cube currency='CNY' rate='9.6263'/>\n\t\t\t<Cube currency='HKD' rate='10.9273'/>\n\t\t\t<Cube currency='IDR' rate='14539.26'/>\n\t\t\t<Cube currency='INR' rate='66.4260'/>\n\t\t\t<Cube currency='KRW' rate='1764.04'/>\n\t\t\t<Cube currency='MXN' rate='18.4340'/>\n\t\t\t<Cube currency='MYR' rate='4.9167'/>\n\t\t\t<Cube currency='NZD' rate='2.2135'/>\n\t\t\t<Cube currency='PHP' rate='66.516'/>\n\t\t\t<Cube currency='SGD' rate='2.0350'/>\n\t\t\t<Cube currency='THB' rate='48.377'/>\n\t\t\t<Cube currency='ZAR' rate='11.2413'/>\n\t\t</Cube>\n\t</Cube>\n</gesmes:Envelope>")
100
+ end
101
+
102
+ it "should fetch data" do
103
+ Money.default_currency = "EUR"
104
+ @bank.fetch_rates
105
+ @bank.get_rate("DKK").should be_close(7.4453, 0.0001)
106
+ @bank.get_rate("BRL").should be_close(2.832, 0.001)
107
+ @bank.exchange(10_00, "EUR", "DKK").should == 74_45
108
+ end
109
+
110
+ it "should fetch diff than eur" do
111
+ Money.default_currency = "BRL"
112
+ @bank.fetch_rates
113
+ @bank.get_rate("DKK").should be_close(2.6289, 0.0001)
114
+ @bank.get_rate("EEK").should be_close(5.5249, 0.0001)
115
+ @bank.get_rate("EUR").should be_close(2.832, 0.001)
116
+ end
117
+
118
+ it "should fetch for an unknown one" do
119
+ Money.default_currency = "XXX"
120
+ @bank.fetch_rates
121
+ @bank.get_rate("DKK").should be_nil
122
+ @bank.get_rate("EUR", "USD").should be_close(1.4098, 0.001)
123
+ Money.default_currency = "USD"
124
+
125
+ end
126
+ end
127
+
128
+ describe "Live Fetching" do
129
+
130
+ if ENV["LIVE"]
131
+
132
+ it "should fetch data live" do
133
+ @bank.fetch_rates
134
+ @bank.get_rate("DKK").should_not be_nil
135
+ @bank.get_rate("EUR").should_not be_nil
136
+ end
137
+
138
+ end
139
+
140
+ end
141
+
46
142
  end
@@ -3,11 +3,12 @@ require File.dirname(__FILE__) + '/../spec_helper.rb'
3
3
 
4
4
  describe Money do
5
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") }
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
+ it { Money.new(199).to_i.should eql(199) }
8
9
 
9
- it "is associated to the singleton instance of VariableExchangeBank by default" do
10
- Money.new(0).bank.object_id.should == Money::VariableExchangeBank.instance.object_id
10
+ it "is associated to the singleton instance of ExchangeBank by default" do
11
+ Money.new(0).bank.object_id.should == Money::ExchangeBank.instance.object_id
11
12
  end
12
13
 
13
14
  it "should return the amount of cents passed to the constructor" do
@@ -111,19 +112,30 @@ describe Money do
111
112
 
112
113
  it "shuld sum array" do
113
114
  Money.new(10_00).split_in_installments(3).sum.cents.should eql(1000)
115
+ Money.new(19_99).split_in_installments(3).sum.cents.should eql(1999)
114
116
  end
115
117
 
116
118
  it "should calculate tax" do
119
+ Money.new(100).add_tax(20).cents.should eql(120)
120
+ Money.new(100).add_tax(-20).cents.should eql(80)
117
121
  Money.new(1218).add_tax(19).cents.should eql(1449)
118
122
  Money.new(1218).add_tax(-19).cents.should eql(987)
123
+ Money.new(1000).add_tax(150).cents.should eql(2500)
124
+ Money.new(1000).add_tax(-150).cents.should eql(-500)
119
125
  end
120
126
 
121
- it "should provide tax brakedown" do
122
- Money.new(1225).tax_brakedown(19).map{|c| c.to_s}.should eql(['14.58','2.33'])
127
+ it "should round to .50 coin" do
128
+ Money.new(14_58).round_to_coin(50).cents.should eql(1450)
129
+ Money.new(14_58).round_to_coin(10).cents.should eql(1450)
130
+ Money.new(14_58).round_to_coin(5).cents.should eql(1455)
123
131
  end
124
132
 
125
- it "should provide reverse tax brakedown" do
126
- Money.new(-8).tax_reverse_brakedown(19).map{|c| c.to_s}.should eql(['-0.07','-0.01'])
133
+ it "should provide tax breakdown" do
134
+ Money.new(1225).tax_breakdown(19).map{|c| c.to_s}.should eql(['14.58','2.33'])
135
+ end
136
+
137
+ it "should provide reverse tax breakdown" do
138
+ Money.new(-8).tax_reverse_breakdown(19).map{|c| c.to_s}.should eql(['-0.07','-0.01'])
127
139
  end
128
140
 
129
141
  it "shuld to_s wallet" do
@@ -140,6 +152,11 @@ describe Money do
140
152
 
141
153
  describe "Taxes and Interest" do
142
154
 
155
+ it "Money rate works" do
156
+ Money.add_rate("EUR", "USD", 10)
157
+ Money.new(10_00, "EUR").exchange_to("USD").should == Money.new(100_00, "USD")
158
+ end
159
+
143
160
  it "Money.add_rate works" do
144
161
  Money.add_rate("USD", 1.0)
145
162
  Money.add_rate("EUR", 10)
@@ -147,8 +164,7 @@ describe Money do
147
164
  end
148
165
 
149
166
  it "Money method missing exchange" do
150
- Money.add_rate("EUR", 1.0)
151
- Money.add_rate("BRL", 10)
167
+ Money.add_rate("EUR", "BRL", 10)
152
168
  Money.new(10_00, "EUR").as_brl.should == Money.new(100_00, "BRL")
153
169
  end
154
170
 
@@ -207,51 +223,55 @@ describe Money do
207
223
  it { Money.new(880000000000, "GBP").format(:no_cents => true).should eql("£8,800,000,000") }
208
224
  it { Money.new(8800000000088, "EUR").format.should eql("€88,000,000,000.88") }
209
225
 
210
- end
211
-
212
-
213
- describe "Actions involving two Money objects" do
214
- describe "if the other Money object has the same currency" do
215
- it "#<=> compares the two objects' amounts" do
216
- (Money.new(1_00, "USD") <=> Money.new(1_00, "USD")).should == 0
217
- (Money.new(1_00, "USD") <=> Money.new(99, "USD")).should > 0
218
- (Money.new(1_00, "USD") <=> Money.new(2_00, "USD")).should < 0
219
- end
220
-
221
- it "#+ adds the other object's amount to the current object's amount while retaining the currency" do
222
- (Money.new(10_00, "USD") + Money.new(90, "USD")).should == Money.new(10_90, "USD")
223
- end
224
-
225
- it "#- substracts the other object's amount from the current object's amount while retaining the currency" do
226
- (Money.new(10_00, "USD") - Money.new(90, "USD")).should == Money.new(9_10, "USD")
227
- end
226
+ it "should fail nicely if symbol can`t be found" do
227
+ Money.default_currency = "XXX"
228
+ Money.new(800).format.should eql("$8.00")
229
+ Money.default_currency = "USD"
228
230
  end
229
231
 
230
- describe "if the other Money object has a different currency" do
231
- it "#<=> compares the two objects' amount after converting the other object's amount to its own currency" do
232
- target = Money.new(200_00, "EUR")
233
- target.should_receive(:exchange_to).with("USD").and_return(Money.new(300_00, "USD"))
234
- (Money.new(100_00, "USD") <=> target).should < 0
235
-
236
- target = Money.new(200_00, "EUR")
237
- target.should_receive(:exchange_to).with("USD").and_return(Money.new(100_00, "USD"))
238
- (Money.new(100_00, "USD") <=> target).should == 0
239
-
240
- target = Money.new(200_00, "EUR")
241
- target.should_receive(:exchange_to).with("USD").and_return(Money.new(99_00, "USD"))
242
- (Money.new(100_00, "USD") <=> target).should > 0
243
- end
244
-
245
- it "#+ adds the other object's amount, converted to this object's currency, to this object's amount while retaining its currency" do
246
- other = Money.new(90, "EUR")
247
- other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD"))
248
- (Money.new(10_00, "USD") + other).should == Money.new(19_00, "USD")
232
+ describe "Actions involving two Money objects" do
233
+ describe "if the other Money object has the same currency" do
234
+ it "#<=> compares the two objects' amounts" do
235
+ (Money.new(1_00, "USD") <=> Money.new(1_00, "USD")).should == 0
236
+ (Money.new(1_00, "USD") <=> Money.new(99, "USD")).should > 0
237
+ (Money.new(1_00, "USD") <=> Money.new(2_00, "USD")).should < 0
238
+ end
239
+
240
+ it "#+ adds the other object's amount to the current object's amount while retaining the currency" do
241
+ (Money.new(10_00, "USD") + Money.new(90, "USD")).should == Money.new(10_90, "USD")
242
+ end
243
+
244
+ it "#- substracts the other object's amount from the current object's amount while retaining the currency" do
245
+ (Money.new(10_00, "USD") - Money.new(90, "USD")).should == Money.new(9_10, "USD")
246
+ end
249
247
  end
250
248
 
251
- it "#- substracts the other object's amount, converted to this object's currency, from this object's amount while retaining its currency" do
252
- other = Money.new(90, "EUR")
253
- other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD"))
254
- (Money.new(10_00, "USD") - other).should == Money.new(1_00, "USD")
249
+ describe "if the other Money object has a different currency" do
250
+ it "#<=> compares the two objects' amount after converting the other object's amount to its own currency" do
251
+ target = Money.new(200_00, "EUR")
252
+ target.should_receive(:exchange_to).with("USD").and_return(Money.new(300_00, "USD"))
253
+ (Money.new(100_00, "USD") <=> target).should < 0
254
+
255
+ target = Money.new(200_00, "EUR")
256
+ target.should_receive(:exchange_to).with("USD").and_return(Money.new(100_00, "USD"))
257
+ (Money.new(100_00, "USD") <=> target).should == 0
258
+
259
+ target = Money.new(200_00, "EUR")
260
+ target.should_receive(:exchange_to).with("USD").and_return(Money.new(99_00, "USD"))
261
+ (Money.new(100_00, "USD") <=> target).should > 0
262
+ end
263
+
264
+ it "#+ adds the other object's amount, converted to this object's currency, to this object's amount while retaining its currency" do
265
+ other = Money.new(90, "EUR")
266
+ other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD"))
267
+ (Money.new(10_00, "USD") + other).should == Money.new(19_00, "USD")
268
+ end
269
+
270
+ it "#- substracts the other object's amount, converted to this object's currency, from this object's amount while retaining its currency" do
271
+ other = Money.new(90, "EUR")
272
+ other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD"))
273
+ (Money.new(10_00, "USD") - other).should == Money.new(1_00, "USD")
274
+ end
255
275
  end
256
276
  end
257
277
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  begin
2
+ require 'rubygems'
2
3
  require 'spec'
3
4
  require 'active_record'
4
5
  rescue LoadError
@@ -6,8 +7,8 @@ rescue LoadError
6
7
  gem 'rspec'
7
8
  require 'spec'
8
9
  end
9
- require 'active_record'
10
- $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
11
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
11
12
  require 'money'
12
13
 
13
14
  config = YAML.load_file(File.dirname(__FILE__) + '/db/database.yml')
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
4
+ version: 2.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Money Team
@@ -9,57 +9,34 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-24 00:00:00 -07:00
12
+ date: 2009-05-29 00:00:00 -07:00
13
13
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: newgem
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 1.3.0
24
- version:
25
- - !ruby/object:Gem::Dependency
26
- name: hoe
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: 1.8.0
34
- version:
14
+ dependencies: []
15
+
35
16
  description: This library aids one in handling money and different currencies.
36
- email:
37
- - see@readme
17
+ email: see@reame
38
18
  executables: []
39
19
 
40
20
  extensions: []
41
21
 
42
22
  extra_rdoc_files:
43
- - History.txt
44
- - Manifest.txt
45
23
  - README.rdoc
46
24
  files:
25
+ - .gitignore
47
26
  - History.txt
48
27
  - MIT-LICENSE
49
28
  - Manifest.txt
50
29
  - README.rdoc
51
30
  - Rakefile
31
+ - VERSION
52
32
  - lib/money.rb
53
33
  - lib/money/acts_as_money.rb
54
34
  - lib/money/core_extensions.rb
55
35
  - lib/money/errors.rb
36
+ - lib/money/exchange_bank.rb
56
37
  - lib/money/money.rb
57
- - lib/money/variable_exchange_bank.rb
58
38
  - money.gemspec
59
39
  - rails/init.rb
60
- - script/console
61
- - script/destroy
62
- - script/generate
63
40
  - spec/db/database.yml
64
41
  - spec/db/schema.rb
65
42
  - spec/money/acts_as_money_spec.rb
@@ -68,12 +45,12 @@ files:
68
45
  - spec/money/money_spec.rb
69
46
  - spec/spec.opts
70
47
  - spec/spec_helper.rb
71
- has_rdoc: true
72
- homepage: http://github.com/ShadowBelmolve/money
48
+ - tmp/.gitignore
49
+ has_rdoc: false
50
+ homepage: http://github.com/nofxx/money
73
51
  post_install_message:
74
52
  rdoc_options:
75
- - --main
76
- - README.rdoc
53
+ - --charset=UTF-8
77
54
  require_paths:
78
55
  - lib
79
56
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -90,10 +67,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
67
  version:
91
68
  requirements: []
92
69
 
93
- rubyforge_project: money
70
+ rubyforge_project:
94
71
  rubygems_version: 1.2.0
95
72
  signing_key:
96
- specification_version: 2
73
+ specification_version: 3
97
74
  summary: This library aids one in handling money and different currencies.
98
- test_files: []
99
-
75
+ test_files:
76
+ - spec/money/exchange_bank_spec.rb
77
+ - spec/money/acts_as_money_spec.rb
78
+ - spec/money/money_spec.rb
79
+ - spec/money/core_extensions_spec.rb
80
+ - spec/db/schema.rb
81
+ - spec/spec_helper.rb
data/script/console DELETED
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # File: script/console
3
- irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
-
5
- libs = " -r irb/completion"
6
- # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
- # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
- libs << " -r #{File.dirname(__FILE__) + '/../lib/postgis_adapter.rb'}"
9
- puts "Loading postgis_adapter gem"
10
- exec "#{irb} #{libs} --simple-prompt"
data/script/destroy DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
- APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
-
4
- begin
5
- require 'rubigen'
6
- rescue LoadError
7
- require 'rubygems'
8
- require 'rubigen'
9
- end
10
- require 'rubigen/scripts/destroy'
11
-
12
- ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
- RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
- RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
- APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
-
4
- begin
5
- require 'rubigen'
6
- rescue LoadError
7
- require 'rubygems'
8
- require 'rubigen'
9
- end
10
- require 'rubigen/scripts/generate'
11
-
12
- ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
- RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
- RubiGen::Scripts::Generate.new.run(ARGV)