money 3.1.0.pre2 → 3.1.0.pre3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,8 @@
1
+ == Money 3.1.0.pre3
2
+ * added google_currency.gem to list of Currency Exchange Implementations
3
+ * updated HKD :subunit_to_unit from 10 to 100 (closes #20)
4
+ * added #(export/import)_rates to Money::Bank::VariableExchange (closes #21)
5
+
1
6
  == Money 3.1.0.pre2
2
7
  * moved @rounding_method from Money::Bank::VariableExchange to
3
8
  Money::Bank::Base (closes #18)
@@ -182,12 +182,13 @@ www.xe.com for the current rates or just returns <tt>rand(2)</tt>:
182
182
 
183
183
  Money.default_bank = ExchangeBankWhichScrapesXeDotCom.new
184
184
 
185
- === Currency Exchange Implementations
185
+ === Currency Exchange (Money::Bank::*) Implementations
186
186
 
187
187
  The following is a list of Money.gem compatible currency exchange rate
188
188
  implementations.
189
189
 
190
190
  * eu_central_bank.gem: http://github.com/RubyMoney/eu_central_bank
191
+ * google_currency.gem: http://github.com/RubyMoney/google_currency
191
192
 
192
193
  === Ruby on Rails
193
194
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.1.0.pre2
1
+ 3.1.0.pre3
@@ -1,7 +1,12 @@
1
1
  require 'money/bank/base'
2
+ require 'json'
3
+ require 'yaml'
2
4
 
3
5
  class Money
4
6
  module Bank
7
+ # Thrown by VariableExchange#export_rates and VariableExchange#import_rates
8
+ # when an unknown rate format is requested.
9
+ class UnknownRateFormat < StandardError; end
5
10
 
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
@@ -22,6 +27,8 @@ class Money
22
27
  #
23
28
  class VariableExchange < Base
24
29
 
30
+ RATE_FORMATS = [:json, :ruby, :yaml]
31
+
25
32
  def setup
26
33
  @rates = {}
27
34
  @mutex = Mutex.new
@@ -71,12 +78,60 @@ class Money
71
78
  @mutex.synchronize { @rates[rate_key_for(from, to)] }
72
79
  end
73
80
 
81
+ # Return the known rates as a string in the format specified. If +file+
82
+ # is given will also write the string out to the file specified.
83
+ # Available formats are +:json+, +:ruby+ and +:yaml+.
84
+ #
85
+ # Raises +Money::Bank::UnknownRateFormat+ if format is unknown.
86
+ def export_rates(format, file=nil)
87
+ raise Money::Bank::UnknownRateFormat unless
88
+ RATE_FORMATS.include? format
89
+
90
+ s = ""
91
+ @mutex.synchronize {
92
+ s = case format
93
+ when :json
94
+ @rates.to_json
95
+ when :ruby
96
+ Marshal.dump(@rates)
97
+ when :yaml
98
+ @rates.to_yaml
99
+ end
100
+
101
+ unless file.nil?
102
+ File.open(file, "w").write(s)
103
+ end
104
+ }
105
+ s
106
+ end
107
+
108
+ # Loads rates provided in +s+ given the specified format. Available
109
+ # formats are +:json+, +:ruby+ and +:yaml+.
110
+ #
111
+ # Raises +Money::Bank::UnknownRateFormat+ if format is unknown.
112
+ def import_rates(format, s)
113
+ raise Money::Bank::UnknownRateFormat unless
114
+ RATE_FORMATS.include? format
115
+
116
+ @mutex.synchronize {
117
+ @rates = case format
118
+ when :json
119
+ JSON.load(s)
120
+ when :ruby
121
+ Marshal.load(s)
122
+ when :yaml
123
+ YAML.load(s)
124
+ end
125
+ }
126
+ self
127
+ end
128
+
74
129
  private
75
130
 
76
- # Return the rate hashkey for the given currencies.
77
- def rate_key_for(from, to)
78
- "#{Currency.wrap(from).iso_code}_TO_#{Currency.wrap(to).iso_code}".upcase
79
- end
131
+ # Return the rate hashkey for the given currencies.
132
+ def rate_key_for(from, to)
133
+ "#{Currency.wrap(from).iso_code}_TO_#{Currency.wrap(to).iso_code}".upcase
134
+ end
80
135
 
81
136
  end
82
137
 
@@ -82,7 +82,7 @@ class Money
82
82
  :gnf => { :priority => 100, :iso_code => "GNF", :name => "Guinean Franc", :symbol => "Fr", :subunit => "Centime", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
83
83
  :gtq => { :priority => 100, :iso_code => "GTQ", :name => "Guatemalan Quetzal", :symbol => "Q", :subunit => "Centavo", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
84
84
  :gyd => { :priority => 100, :iso_code => "GYD", :name => "Guyanese Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
85
- :hkd => { :priority => 100, :iso_code => "HKD", :name => "Hong Kong Dollar", :symbol => "$", :subunit => "Ho", :subunit_to_unit => 10, :separator => ".", :delimiter => "," },
85
+ :hkd => { :priority => 100, :iso_code => "HKD", :name => "Hong Kong Dollar", :symbol => "$", :subunit => "Ho", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
86
86
  :hnl => { :priority => 100, :iso_code => "HNL", :name => "Honduran Lempira", :symbol => "L", :subunit => "Centavo", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
87
87
  :hrk => { :priority => 100, :iso_code => "HRK", :name => "Croatian Kuna", :symbol => "kn", :subunit => "Lipa", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
88
88
  :htg => { :priority => 100, :iso_code => "HTG", :name => "Haitian Gourde", :symbol => "G", :subunit => "Centime", :subunit_to_unit => 100, :separator => ".", :delimiter => "," },
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{money}
8
- s.version = "3.1.0.pre2"
8
+ s.version = "3.1.0.pre3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tobias Luetke", "Hongli Lai", "Jeremy McNevin", "Shane Emmons", "Simone Carletti"]
12
- s.date = %q{2010-08-10}
12
+ s.date = %q{2010-08-31}
13
13
  s.description = %q{Money and currency exchange support library.}
14
14
  s.email = %q{hongli@phusion.nl}
15
15
  s.extra_rdoc_files = [
@@ -1,4 +1,6 @@
1
1
  require "spec_helper"
2
+ require "json"
3
+ require "yaml"
2
4
 
3
5
  describe Money::Bank::VariableExchange do
4
6
 
@@ -165,6 +167,85 @@ describe Money::Bank::VariableExchange do
165
167
  end
166
168
  end
167
169
 
170
+ describe '#export_rates' do
171
+ before :each do
172
+ @bank.set_rate('USD', 'EUR', 1.25)
173
+ @bank.set_rate('USD', 'JPY', 2.55)
174
+ end
175
+
176
+ describe 'with format == :json' do
177
+ it 'should return rates formatted as json' do
178
+ rates_as_json = '{"USD_TO_EUR":1.25,"USD_TO_JPY":2.55}'
179
+ @bank.export_rates(:json).should == rates_as_json
180
+ end
181
+ end
182
+
183
+ describe 'with format == :ruby' do
184
+ it 'should return rates formatted as ruby objects' do
185
+ rates = {"USD_TO_EUR"=>1.25,"USD_TO_JPY"=>2.55}
186
+ @bank.export_rates(:ruby).should == Marshal.dump(rates)
187
+ end
188
+ end
189
+
190
+ describe 'with format == :yaml' do
191
+ it 'should return rates formatted as yaml' do
192
+ rates_as_yaml = "--- \nUSD_TO_EUR: 1.25\nUSD_TO_JPY: 2.55\n"
193
+ @bank.export_rates(:yaml).should == rates_as_yaml
194
+ end
195
+ end
196
+
197
+ describe 'with unknown format' do
198
+ it 'should raise `UnknownRateFormat`' do
199
+ lambda{@bank.export_rates(:foo)}.should raise_error Money::Bank::UnknownRateFormat
200
+ end
201
+ end
202
+
203
+ describe 'with :file provided' do
204
+ it 'should write rates to file' do
205
+ f = mock('IO')
206
+ File.should_receive(:open).with('null', 'w').and_return(f)
207
+ f.should_receive(:write).with('{"USD_TO_EUR":1.25,"USD_TO_JPY":2.55}')
208
+
209
+ @bank.export_rates(:json, 'null')
210
+ end
211
+ end
212
+ end
213
+
214
+ describe '#import_rates' do
215
+ describe 'with format == :json' do
216
+ it 'should load the rates provided' do
217
+ s = '{"USD_TO_EUR":1.25,"USD_TO_JPY":2.55}'
218
+ @bank.import_rates(:json, s)
219
+ @bank.get_rate('USD', 'EUR').should == 1.25
220
+ @bank.get_rate('USD', 'JPY').should == 2.55
221
+ end
222
+ end
223
+
224
+ describe 'with format == :ruby' do
225
+ it 'should load the rates provided' do
226
+ s = Marshal.dump({"USD_TO_EUR"=>1.25,"USD_TO_JPY"=>2.55})
227
+ @bank.import_rates(:ruby, s)
228
+ @bank.get_rate('USD', 'EUR').should == 1.25
229
+ @bank.get_rate('USD', 'JPY').should == 2.55
230
+ end
231
+ end
232
+
233
+ describe 'with format == :yaml' do
234
+ it 'should load the rates provided' do
235
+ s = "--- \nUSD_TO_EUR: 1.25\nUSD_TO_JPY: 2.55\n"
236
+ @bank.import_rates(:yaml, s)
237
+ @bank.get_rate('USD', 'EUR').should == 1.25
238
+ @bank.get_rate('USD', 'JPY').should == 2.55
239
+ end
240
+ end
241
+
242
+ describe 'with unknown format' do
243
+ it 'should raise `UnknownRateFormat`' do
244
+ lambda{@bank.import_rates(:foo, "")}.should raise_error Money::Bank::UnknownRateFormat
245
+ end
246
+ end
247
+ end
248
+
168
249
  describe '#rate_key_for' do
169
250
  it 'should accept str/str' do
170
251
  lambda{@bank.send(:rate_key_for, 'USD', 'EUR')}.should_not raise_exception
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money
3
3
  version: !ruby/object:Gem::Version
4
- hash: 341127967
4
+ hash: -502734836
5
5
  prerelease: true
6
6
  segments:
7
7
  - 3
8
8
  - 1
9
9
  - 0
10
- - pre2
11
- version: 3.1.0.pre2
10
+ - pre3
11
+ version: 3.1.0.pre3
12
12
  platform: ruby
13
13
  authors:
14
14
  - Tobias Luetke
@@ -20,7 +20,7 @@ autorequire:
20
20
  bindir: bin
21
21
  cert_chain: []
22
22
 
23
- date: 2010-08-10 00:00:00 -04:00
23
+ date: 2010-08-31 00:00:00 -04:00
24
24
  default_executable:
25
25
  dependencies:
26
26
  - !ruby/object:Gem::Dependency