money 2.1.5 → 2.2.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.
@@ -1,72 +1,72 @@
1
- require 'thread'
2
- require 'money/errors'
3
-
4
- # Class for aiding in exchanging money between different currencies.
5
- # By default, the Money class uses an object of this class (accessible through
6
- # Money#bank) for performing currency exchanges.
7
- #
8
- # By default, VariableExchangeBank has no knowledge about conversion rates.
9
- # One must manually specify them with +add_rate+, after which one can perform
10
- # exchanges with +exchange+. For example:
11
- #
12
- # bank = Money::VariableExchangeBank.new
13
- # bank.add_rate("USD", "CAD", 1.24515)
14
- # bank.add_rate("CAD", "USD", 0.803115)
15
- #
16
- # # Exchange 100 CAD to USD:
17
- # bank.exchange(100_00, "CAD", "USD") # => 124
18
- # # Exchange 100 USD to CAD:
19
- # bank.exchange(100_00, "USD", "CAD") # => 80
20
- class Money
21
- class VariableExchangeBank
22
- # Returns the singleton instance of VariableExchangeBank.
23
- #
24
- # By default, <tt>Money.default_bank</tt> returns the same object.
25
- def self.instance
26
- @@singleton
27
- end
28
-
29
- def initialize
30
- @rates = {}
31
- @mutex = Mutex.new
32
- end
33
-
34
- # Registers a conversion rate. +from+ and +to+ are both currency names.
35
- def add_rate(from, to, rate)
36
- @mutex.synchronize do
37
- @rates["#{from}_TO_#{to}".upcase] = rate
38
- end
39
- end
40
-
41
- # Gets the rate for exchanging the currency named +from+ to the currency
42
- # named +to+. Returns nil if the rate is unknown.
43
- def get_rate(from, to)
44
- @mutex.synchronize do
45
- @rates["#{from}_TO_#{to}".upcase]
46
- end
47
- end
48
-
49
- # Given two currency names, checks whether they're both the same currency.
50
- #
51
- # bank = VariableExchangeBank.new
52
- # bank.same_currency?("usd", "USD") # => true
53
- # bank.same_currency?("usd", "EUR") # => false
54
- def same_currency?(currency1, currency2)
55
- currency1.upcase == currency2.upcase
56
- end
57
-
58
- # Exchange the given amount of cents in +from_currency+ to +to_currency+.
59
- # Returns the amount of cents in +to_currency+ as an integer, rounded down.
60
- #
61
- # If the conversion rate is unknown, then Money::UnknownRate will be raised.
62
- def exchange(cents, from_currency, to_currency)
63
- rate = get_rate(from_currency, to_currency)
64
- if !rate
65
- raise Money::UnknownRate, "No conversion rate known for '#{from_currency}' -> '#{to_currency}'"
66
- end
67
- (cents * rate).floor
68
- end
69
-
70
- @@singleton = VariableExchangeBank.new
71
- end
72
- end
1
+ require 'thread'
2
+ require 'money/errors'
3
+
4
+ # Class for aiding in exchanging money between different currencies.
5
+ # By default, the Money class uses an object of this class (accessible through
6
+ # Money#bank) for performing currency exchanges.
7
+ #
8
+ # By default, VariableExchangeBank has no knowledge about conversion rates.
9
+ # One must manually specify them with +add_rate+, after which one can perform
10
+ # exchanges with +exchange+. For example:
11
+ #
12
+ # bank = Money::VariableExchangeBank.new
13
+ # bank.add_rate("USD", "CAD", 1.24515)
14
+ # bank.add_rate("CAD", "USD", 0.803115)
15
+ #
16
+ # # Exchange 100 CAD to USD:
17
+ # bank.exchange(100_00, "CAD", "USD") # => 124
18
+ # # Exchange 100 USD to CAD:
19
+ # bank.exchange(100_00, "USD", "CAD") # => 80
20
+ class Money
21
+ class VariableExchangeBank
22
+ # Returns the singleton instance of VariableExchangeBank.
23
+ #
24
+ # By default, <tt>Money.default_bank</tt> returns the same object.
25
+ def self.instance
26
+ @@singleton
27
+ end
28
+
29
+ def initialize
30
+ @rates = {}
31
+ @mutex = Mutex.new
32
+ end
33
+
34
+ # Registers a conversion rate. +from+ and +to+ are both currency names.
35
+ def add_rate(from, to, rate)
36
+ @mutex.synchronize do
37
+ @rates["#{from}_TO_#{to}".upcase] = rate
38
+ end
39
+ end
40
+
41
+ # Gets the rate for exchanging the currency named +from+ to the currency
42
+ # named +to+. Returns nil if the rate is unknown.
43
+ def get_rate(from, to)
44
+ @mutex.synchronize do
45
+ @rates["#{from}_TO_#{to}".upcase]
46
+ end
47
+ end
48
+
49
+ # Given two currency names, checks whether they're both the same currency.
50
+ #
51
+ # bank = VariableExchangeBank.new
52
+ # bank.same_currency?("usd", "USD") # => true
53
+ # bank.same_currency?("usd", "EUR") # => false
54
+ def same_currency?(currency1, currency2)
55
+ currency1.upcase == currency2.upcase
56
+ end
57
+
58
+ # Exchange the given amount of cents in +from_currency+ to +to_currency+.
59
+ # Returns the amount of cents in +to_currency+ as an integer, rounded down.
60
+ #
61
+ # If the conversion rate is unknown, then Money::UnknownRate will be raised.
62
+ def exchange(cents, from_currency, to_currency)
63
+ rate = get_rate(from_currency, to_currency)
64
+ if !rate
65
+ raise Money::UnknownRate, "No conversion rate known for '#{from_currency}' -> '#{to_currency}'"
66
+ end
67
+ (cents * rate).floor
68
+ end
69
+
70
+ @@singleton = VariableExchangeBank.new
71
+ end
72
+ end
@@ -1,24 +1,65 @@
1
- Gem::Specification.new do |s|
2
- s.name = "money"
3
- s.version = "2.1.5"
4
- s.summary = "Money and currency exchange support library"
5
- s.email = "hongli@phusion.nl"
6
- s.homepage = "http://money.rubyforge.org/"
7
- s.description = "Money and currency exchange support library."
8
- s.has_rdoc = true
9
- s.rubyforge_project = "money"
10
- s.authors = ["Tobias Luetke", "Hongli Lai", "Jeremy McNevin"]
11
-
12
- s.files = [
13
- "README.rdoc", "MIT-LICENSE", "money.gemspec", "Rakefile",
14
- "lib/money.rb",
15
- "lib/money/core_extensions.rb",
16
- "lib/money/errors.rb",
17
- "lib/money/money.rb",
18
- "lib/money/symbols.rb",
19
- "lib/money/variable_exchange_bank.rb",
20
- "test/core_extensions_spec.rb",
21
- "test/exchange_bank_spec.rb",
22
- "test/money_spec.rb"
23
- ]
24
- end
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{money}
8
+ s.version = "2.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tobias Luetke", "Hongli Lai", "Jeremy McNevin", "Shane Emmons"]
12
+ s.date = %q{2010-02-17}
13
+ s.description = %q{Money and currency exchange support library.}
14
+ s.email = %q{hongli@phusion.nl}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/money.rb",
27
+ "lib/money/core_extensions.rb",
28
+ "lib/money/defaults.rb",
29
+ "lib/money/errors.rb",
30
+ "lib/money/money.rb",
31
+ "lib/money/variable_exchange_bank.rb",
32
+ "money.gemspec",
33
+ "test/core_extensions_spec.rb",
34
+ "test/exchange_bank_spec.rb",
35
+ "test/money_spec.rb"
36
+ ]
37
+ s.homepage = %q{http://money.rubyforge.org/}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubyforge_project = %q{money}
41
+ s.rubygems_version = %q{1.3.5}
42
+ s.summary = %q{Money and currency exchange support library}
43
+ s.test_files = [
44
+ "test/core_extensions_spec.rb",
45
+ "test/exchange_bank_spec.rb",
46
+ "test/money_spec.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
54
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
55
+ s.add_development_dependency(%q<hanna>, [">= 0.1.12"])
56
+ else
57
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
58
+ s.add_dependency(%q<hanna>, [">= 0.1.12"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
62
+ s.add_dependency(%q<hanna>, [">= 0.1.12"])
63
+ end
64
+ end
65
+
@@ -1,73 +1,73 @@
1
- $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
2
- require 'money/core_extensions'
3
-
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
- "20.15".to_money.should == Money.new(20_15)
17
- "100".to_money.should == Money.new(100_00)
18
- "100.37".to_money.should == Money.new(100_37)
19
- "100,37".to_money.should == Money.new(100_37)
20
- "100 000".to_money.should == Money.new(100_000_00)
21
- "100,000.00".to_money.should == Money.new(100_000_00)
22
- "1,000".to_money.should == Money.new(1_000_00)
23
- "-1,000".to_money.should == Money.new(-1_000_00)
24
- "1,000.5".to_money.should == Money.new(1_000_50)
25
- "1,000.51".to_money.should == Money.new(1_000_51)
26
- "1,000.505".to_money.should == Money.new(1_000_51)
27
- "1,000.504".to_money.should == Money.new(1_000_50)
28
- "1,000.0000".to_money.should == Money.new(1_000_00)
29
- "1,000.5000".to_money.should == Money.new(1_000_50)
30
- "1,000.5099".to_money.should == Money.new(1_000_51)
31
- "1.550".to_money.should == Money.new(1_55)
32
- "25.".to_money.should == Money.new(25_00)
33
- ".75".to_money.should == Money.new(75)
34
-
35
- "100 USD".to_money.should == Money.new(100_00, "USD")
36
- "-100 USD".to_money.should == Money.new(-100_00, "USD")
37
- "100 EUR".to_money.should == Money.new(100_00, "EUR")
38
- "100.37 EUR".to_money.should == Money.new(100_37, "EUR")
39
- "100,37 EUR".to_money.should == Money.new(100_37, "EUR")
40
- "100,000.00 USD".to_money.should == Money.new(100_000_00, "USD")
41
- "100.000,00 EUR".to_money.should == Money.new(100_000_00, "EUR")
42
- "1,000 USD".to_money.should == Money.new(1_000_00, "USD")
43
- "-1,000 USD".to_money.should == Money.new(-1_000_00, "USD")
44
- "1,000.5500 USD".to_money.should == Money.new(1_000_55, "USD")
45
- "-1,000.6500 USD".to_money.should == Money.new(-1_000_65, "USD")
46
- "1.550 USD".to_money.should == Money.new(1_55, "USD")
47
-
48
- "USD 100".to_money.should == Money.new(100_00, "USD")
49
- "EUR 100".to_money.should == Money.new(100_00, "EUR")
50
- "EUR 100.37".to_money.should == Money.new(100_37, "EUR")
51
- "CAD -100.37".to_money.should == Money.new(-100_37, "CAD")
52
- "EUR 100,37".to_money.should == Money.new(100_37, "EUR")
53
- "EUR -100,37".to_money.should == Money.new(-100_37, "EUR")
54
- "USD 100,000.00".to_money.should == Money.new(100_000_00, "USD")
55
- "EUR 100.000,00".to_money.should == Money.new(100_000_00, "EUR")
56
- "USD 1,000".to_money.should == Money.new(1_000_00, "USD")
57
- "USD -1,000".to_money.should == Money.new(-1_000_00, "USD")
58
- "USD 1,000.9000".to_money.should == Money.new(1_000_90, "USD")
59
- "USD -1,000.090".to_money.should == Money.new(-1_000_09, "USD")
60
- "USD 1.5500".to_money.should == Money.new(1_55, "USD")
61
-
62
- "$100 USD".to_money.should == Money.new(100_00, "USD")
63
- "$1,194.59 USD".to_money.should == Money.new(1_194_59, "USD")
64
- "$-1,955 USD".to_money.should == Money.new(-1_955_00, "USD")
65
- "$1,194.5900 USD".to_money.should == Money.new(1_194_59, "USD")
66
- "$-1,955.000 USD".to_money.should == Money.new(-1_955_00, "USD")
67
- "$1.99000 USD".to_money.should == Money.new(1_99, "USD")
68
- end
69
-
70
- specify "String#to_money ignores unrecognized data" do
71
- "hello 2000 world".to_money.should == Money.new(2000_00)
72
- end
73
- end
1
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
2
+ require 'money/core_extensions'
3
+
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
+ "20.15".to_money.should == Money.new(20_15)
17
+ "100".to_money.should == Money.new(100_00)
18
+ "100.37".to_money.should == Money.new(100_37)
19
+ "100,37".to_money.should == Money.new(100_37)
20
+ "100 000".to_money.should == Money.new(100_000_00)
21
+ "100,000.00".to_money.should == Money.new(100_000_00)
22
+ "1,000".to_money.should == Money.new(1_000_00)
23
+ "-1,000".to_money.should == Money.new(-1_000_00)
24
+ "1,000.5".to_money.should == Money.new(1_000_50)
25
+ "1,000.51".to_money.should == Money.new(1_000_51)
26
+ "1,000.505".to_money.should == Money.new(1_000_51)
27
+ "1,000.504".to_money.should == Money.new(1_000_50)
28
+ "1,000.0000".to_money.should == Money.new(1_000_00)
29
+ "1,000.5000".to_money.should == Money.new(1_000_50)
30
+ "1,000.5099".to_money.should == Money.new(1_000_51)
31
+ "1.550".to_money.should == Money.new(1_55)
32
+ "25.".to_money.should == Money.new(25_00)
33
+ ".75".to_money.should == Money.new(75)
34
+
35
+ "100 USD".to_money.should == Money.new(100_00, "USD")
36
+ "-100 USD".to_money.should == Money.new(-100_00, "USD")
37
+ "100 EUR".to_money.should == Money.new(100_00, "EUR")
38
+ "100.37 EUR".to_money.should == Money.new(100_37, "EUR")
39
+ "100,37 EUR".to_money.should == Money.new(100_37, "EUR")
40
+ "100,000.00 USD".to_money.should == Money.new(100_000_00, "USD")
41
+ "100.000,00 EUR".to_money.should == Money.new(100_000_00, "EUR")
42
+ "1,000 USD".to_money.should == Money.new(1_000_00, "USD")
43
+ "-1,000 USD".to_money.should == Money.new(-1_000_00, "USD")
44
+ "1,000.5500 USD".to_money.should == Money.new(1_000_55, "USD")
45
+ "-1,000.6500 USD".to_money.should == Money.new(-1_000_65, "USD")
46
+ "1.550 USD".to_money.should == Money.new(1_55, "USD")
47
+
48
+ "USD 100".to_money.should == Money.new(100_00, "USD")
49
+ "EUR 100".to_money.should == Money.new(100_00, "EUR")
50
+ "EUR 100.37".to_money.should == Money.new(100_37, "EUR")
51
+ "CAD -100.37".to_money.should == Money.new(-100_37, "CAD")
52
+ "EUR 100,37".to_money.should == Money.new(100_37, "EUR")
53
+ "EUR -100,37".to_money.should == Money.new(-100_37, "EUR")
54
+ "USD 100,000.00".to_money.should == Money.new(100_000_00, "USD")
55
+ "EUR 100.000,00".to_money.should == Money.new(100_000_00, "EUR")
56
+ "USD 1,000".to_money.should == Money.new(1_000_00, "USD")
57
+ "USD -1,000".to_money.should == Money.new(-1_000_00, "USD")
58
+ "USD 1,000.9000".to_money.should == Money.new(1_000_90, "USD")
59
+ "USD -1,000.090".to_money.should == Money.new(-1_000_09, "USD")
60
+ "USD 1.5500".to_money.should == Money.new(1_55, "USD")
61
+
62
+ "$100 USD".to_money.should == Money.new(100_00, "USD")
63
+ "$1,194.59 USD".to_money.should == Money.new(1_194_59, "USD")
64
+ "$-1,955 USD".to_money.should == Money.new(-1_955_00, "USD")
65
+ "$1,194.5900 USD".to_money.should == Money.new(1_194_59, "USD")
66
+ "$-1,955.000 USD".to_money.should == Money.new(-1_955_00, "USD")
67
+ "$1.99000 USD".to_money.should == Money.new(1_99, "USD")
68
+ end
69
+
70
+ specify "String#to_money ignores unrecognized data" do
71
+ "hello 2000 world".to_money.should == Money.new(2000_00)
72
+ end
73
+ end
@@ -1,45 +1,45 @@
1
- $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
2
- require 'money/variable_exchange_bank'
3
-
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
45
- end
1
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
2
+ require 'money/variable_exchange_bank'
3
+
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
45
+ end