big_money 1.1.0 → 1.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.
- data/VERSION.yml +1 -1
- data/big_money.gemspec +5 -5
- data/lib/big_money/exchange.rb +30 -4
- data/test/test_exchange.rb +6 -0
- metadata +13 -6
data/VERSION.yml
CHANGED
data/big_money.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{big_money}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Shane Hanna", "Marshall Roch"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-05-27}
|
13
13
|
s.email = ["shane.hanna@gmail.com", "mroch@cmu.edu"]
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE",
|
@@ -42,14 +42,14 @@ Gem::Specification.new do |s|
|
|
42
42
|
s.homepage = %q{http://github.com/shanna/big_money}
|
43
43
|
s.rdoc_options = ["--charset=UTF-8"]
|
44
44
|
s.require_paths = ["lib"]
|
45
|
-
s.rubygems_version = %q{1.3.
|
45
|
+
s.rubygems_version = %q{1.3.6}
|
46
46
|
s.summary = %q{BigDecimal backed amount of money in a particular currency.}
|
47
47
|
s.test_files = [
|
48
48
|
"test/test_exchange.rb",
|
49
|
-
"test/helper.rb",
|
50
49
|
"test/test_currency.rb",
|
51
50
|
"test/test_big_money.rb",
|
52
|
-
"test/test_parser.rb"
|
51
|
+
"test/test_parser.rb",
|
52
|
+
"test/helper.rb"
|
53
53
|
]
|
54
54
|
|
55
55
|
if s.respond_to? :specification_version then
|
data/lib/big_money/exchange.rb
CHANGED
@@ -36,13 +36,31 @@ class BigMoney
|
|
36
36
|
|
37
37
|
# Find the exchange rate between two currencies.
|
38
38
|
#
|
39
|
-
#
|
40
|
-
|
41
|
-
#
|
39
|
+
# ==== Notes
|
40
|
+
#
|
41
|
+
# No caching is done by default. You can set one though using anything that behaves like a Hash for example the
|
42
|
+
# moneta library.
|
43
|
+
#
|
44
|
+
# ==== Example
|
45
|
+
#
|
46
|
+
# require 'bigmoney/exchanage'
|
47
|
+
# require 'moneta/memcache'
|
48
|
+
#
|
49
|
+
# BigMoney::Exchange.cache = Moneta::Memcache.new('localhost', default_ttl: 3_600)
|
42
50
|
class Exchange
|
43
51
|
class ConversionError < StandardError; end
|
44
52
|
|
45
53
|
class << self
|
54
|
+
def cache=(store)
|
55
|
+
raise "Cache object #{store.class} does not respond to [] and []=." \
|
56
|
+
unless store.respond_to?(:'[]') and store.respond_to?(:'[]=')
|
57
|
+
@@cache = store
|
58
|
+
end
|
59
|
+
|
60
|
+
def cache
|
61
|
+
@@cache ||= nil
|
62
|
+
end
|
63
|
+
|
46
64
|
@@services = []
|
47
65
|
def inherited(service) #:nodoc:
|
48
66
|
@@services << service
|
@@ -62,12 +80,20 @@ class BigMoney
|
|
62
80
|
exchange << (Currency.find(to) or raise ArgumentError.new("Unknown +to+ currency #{to.inspect}."))
|
63
81
|
return BigDecimal.new(1.to_s) if exchange.uniq.size == 1 || exchange.find{|c| c == Currency::XXX}
|
64
82
|
|
83
|
+
id = exchange.map{|c| c.code}.join(':')
|
84
|
+
if cache && rate = cache[id]
|
85
|
+
return rate
|
86
|
+
end
|
87
|
+
|
65
88
|
service = @@services.reverse.find do |service|
|
66
89
|
!!exchange.reject{|c| service.currencies.include?(c)}
|
67
90
|
end
|
68
91
|
|
69
92
|
service or raise ConversionError # TODO: Message?
|
70
|
-
BigDecimal.new(service.read_rate(*exchange).to_s)
|
93
|
+
rate = BigDecimal.new(service.read_rate(*exchange).to_s)
|
94
|
+
|
95
|
+
cache[id] = rate if cache
|
96
|
+
rate
|
71
97
|
end
|
72
98
|
|
73
99
|
protected
|
data/test/test_exchange.rb
CHANGED
@@ -33,5 +33,11 @@ class TestExchange < Test::Unit::TestCase
|
|
33
33
|
assert_equal BigDecimal.new('1'), BigMoney::Exchange.rate(:usd, :xxx)
|
34
34
|
assert_equal BigDecimal.new('1'), BigMoney::Exchange.rate(:xxx, :usd)
|
35
35
|
end
|
36
|
+
|
37
|
+
should 'be cacheable' do
|
38
|
+
BigMoney::Exchange.cache = {}
|
39
|
+
BigMoney::Exchange.rate(:usd, :aud)
|
40
|
+
assert_equal BigDecimal.new('2'), BigMoney::Exchange.cache.values.first
|
41
|
+
end
|
36
42
|
end
|
37
43
|
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: big_money
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 1.2.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Shane Hanna
|
@@ -10,7 +15,7 @@ autorequire:
|
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
17
|
|
13
|
-
date: 2010-
|
18
|
+
date: 2010-05-27 00:00:00 +10:00
|
14
19
|
default_executable:
|
15
20
|
dependencies: []
|
16
21
|
|
@@ -61,24 +66,26 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
66
|
requirements:
|
62
67
|
- - ">="
|
63
68
|
- !ruby/object:Gem::Version
|
69
|
+
segments:
|
70
|
+
- 0
|
64
71
|
version: "0"
|
65
|
-
version:
|
66
72
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
73
|
requirements:
|
68
74
|
- - ">="
|
69
75
|
- !ruby/object:Gem::Version
|
76
|
+
segments:
|
77
|
+
- 0
|
70
78
|
version: "0"
|
71
|
-
version:
|
72
79
|
requirements: []
|
73
80
|
|
74
81
|
rubyforge_project:
|
75
|
-
rubygems_version: 1.3.
|
82
|
+
rubygems_version: 1.3.6
|
76
83
|
signing_key:
|
77
84
|
specification_version: 3
|
78
85
|
summary: BigDecimal backed amount of money in a particular currency.
|
79
86
|
test_files:
|
80
87
|
- test/test_exchange.rb
|
81
|
-
- test/helper.rb
|
82
88
|
- test/test_currency.rb
|
83
89
|
- test/test_big_money.rb
|
84
90
|
- test/test_parser.rb
|
91
|
+
- test/helper.rb
|