currencies 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ruby@currencies
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+
3
+ gem 'jeweler', '=1.4.0'
4
+ gem 'rspec', '=1.3.1'
5
+
6
+ # /* vim: set filetype=ruby : */
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ gemcutter (0.6.1)
5
+ git (1.2.5)
6
+ jeweler (1.4.0)
7
+ gemcutter (>= 0.1.0)
8
+ git (>= 1.2.5)
9
+ rubyforge (>= 2.0.0)
10
+ json_pure (1.4.6)
11
+ rspec (1.3.1)
12
+ rubyforge (2.0.4)
13
+ json_pure (>= 1.1.7)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ jeweler (= 1.4.0)
20
+ rspec (= 1.3.1)
data/Rakefile CHANGED
@@ -31,8 +31,6 @@ Spec::Rake::SpecTask.new(:rcov) do |spec|
31
31
  spec.rcov = true
32
32
  end
33
33
 
34
- task :spec => :check_dependencies
35
-
36
34
  task :default => :spec
37
35
 
38
36
  begin
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -1,61 +1,68 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{currencies}
8
- s.version = "0.2.1"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["hexorx"]
12
- s.date = %q{2010-03-30}
12
+ s.date = %q{2011-01-10}
13
13
  s.description = %q{If you are tracking any kind of assets the currencies gem is for you. It contains every currency in the ISO 4217 standard and allows you to add your own as well. So if you decide to take sparkly buttons as a form of payment you can use currencies to display the shiny button unicode symbol ☼ (disclaimer: ☼ may not look like a shiny button to everyone.) when used with something like the money gem. Speaking of the money gem, currencies gives you an ExchangeBank that the money gem can use to convert from one currency to another. There are plans to have ExchangeRate provider plugin system. Right now the rates are either set manually or pulled from Yahoo Finance.}
14
14
  s.email = %q{hexorx@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README.markdown"
17
+ "README.markdown"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
- ".gitignore",
22
- "LICENSE",
23
- "README.markdown",
24
- "Rakefile",
25
- "VERSION",
26
- "currencies.gemspec",
27
- "lib/currencies.rb",
28
- "lib/currencies/currency.rb",
29
- "lib/currencies/exchange_bank.rb",
30
- "lib/currencies/extentions.rb",
31
- "lib/data/iso4217.yaml",
32
- "spec/currency_spec.rb",
33
- "spec/exchange_bank_spec.rb",
34
- "spec/spec_helper.rb"
21
+ ".rvmrc",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE",
25
+ "README.markdown",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "currencies.gemspec",
29
+ "lib/currencies.rb",
30
+ "lib/currencies/currency.rb",
31
+ "lib/currencies/exchange_bank.rb",
32
+ "lib/currencies/extentions.rb",
33
+ "lib/data/iso4217.yaml",
34
+ "spec/currency_spec.rb",
35
+ "spec/exchange_bank_spec.rb",
36
+ "spec/spec_helper.rb"
35
37
  ]
36
38
  s.homepage = %q{http://github.com/hexorx/currencies}
37
- s.rdoc_options = ["--charset=UTF-8"]
38
39
  s.require_paths = ["lib"]
39
- s.rubygems_version = %q{1.3.6}
40
+ s.rubygems_version = %q{1.3.7}
40
41
  s.summary = %q{Simple gem for working with currencies. It is extracted from the countries gem and contains all the currency information in the ISO 4217 standard.}
41
42
  s.test_files = [
42
43
  "spec/currency_spec.rb",
43
- "spec/exchange_bank_spec.rb",
44
- "spec/spec_helper.rb"
44
+ "spec/exchange_bank_spec.rb",
45
+ "spec/spec_helper.rb"
45
46
  ]
46
47
 
47
48
  if s.respond_to? :specification_version then
48
49
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
50
  s.specification_version = 3
50
51
 
51
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
52
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
+ s.add_runtime_dependency(%q<jeweler>, ["= 1.4.0"])
54
+ s.add_runtime_dependency(%q<rspec>, ["= 1.3.1"])
52
55
  s.add_development_dependency(%q<rspec>, [">= 0"])
53
56
  s.add_development_dependency(%q<yard>, [">= 0"])
54
57
  else
58
+ s.add_dependency(%q<jeweler>, ["= 1.4.0"])
59
+ s.add_dependency(%q<rspec>, ["= 1.3.1"])
55
60
  s.add_dependency(%q<rspec>, [">= 0"])
56
61
  s.add_dependency(%q<yard>, [">= 0"])
57
62
  end
58
63
  else
64
+ s.add_dependency(%q<jeweler>, ["= 1.4.0"])
65
+ s.add_dependency(%q<rspec>, ["= 1.3.1"])
59
66
  s.add_dependency(%q<rspec>, [">= 0"])
60
67
  s.add_dependency(%q<yard>, [">= 0"])
61
68
  end
@@ -2,6 +2,7 @@ class Currency
2
2
  class << self
3
3
  attr_accessor :currencies
4
4
  attr_accessor :base_currency
5
+ attr_accessor :major_codes
5
6
  attr_accessor :import_exchange_rates
6
7
  end
7
8
 
@@ -46,7 +47,41 @@ class Currency
46
47
  def self.from_code(code)
47
48
  self.currencies[code.to_s.upcase]
48
49
  end
49
-
50
+
51
+ def self.major_currencies_selection(currencies)
52
+ currencies.select { |code, currency| Currency.major_codes.include?(code) }.first
53
+ end
54
+
55
+ def self.best_from_currencies(currencies)
56
+ return if currencies.nil? || currencies.empty?
57
+ self.major_currencies_selection(currencies) ? self.major_currencies_selection(currencies)[1] : currencies.first[1]
58
+ end
59
+
60
+ def self.list_from_name(name)
61
+ self.currencies.select { |code, currency| currency.name == name }
62
+ end
63
+
64
+ def self.list_from_symbol(symbol)
65
+ self.currencies.select { |code, currency| currency.symbol == symbol }
66
+ end
67
+
68
+ def self.best_from_name(name)
69
+ self.best_from_currencies(self.list_from_name(name))
70
+ end
71
+
72
+ def self.best_from_symbol(symbol)
73
+ self.best_from_currencies(self.list_from_symbol(symbol))
74
+ end
75
+
76
+ def self.best_guess(str)
77
+ return if str.nil? || str.empty?
78
+ self.from_code(str) || self.best_from_symbol(str) || self.best_from_name(str)
79
+ end
80
+
81
+ def self.code_from_best_guess(str)
82
+ self.best_guess(str).try(:code)
83
+ end
84
+
50
85
  def self.add(new_currency)
51
86
  self.currencies ||= {}
52
87
  self.currencies[new_currency.code] = new_currency
@@ -54,5 +89,6 @@ class Currency
54
89
 
55
90
  load_file(File.join(File.dirname(__FILE__), '..', 'data', 'iso4217.yaml'))
56
91
  self.base_currency = 'USD'
92
+ self.major_codes = [ "USD", "EUR", "GBP" ]
57
93
  self.import_exchange_rates = true
58
- end
94
+ end
@@ -63,4 +63,278 @@ describe Currency do
63
63
  Currency.from_code(Currency.base_currency).exchange_rate.should == 1.0
64
64
  end
65
65
  end
66
- end
66
+
67
+ describe ".major_currencies_selection" do
68
+ let(:usd) { [ "USD", mock ] }
69
+ let(:aud) { [ "AUD", mock ] }
70
+ let(:currencies) { [ usd, aud ] }
71
+
72
+ context "with default settings" do
73
+ subject do
74
+ Currency.major_currencies_selection(currencies)
75
+ end
76
+
77
+ it { should == usd }
78
+ end
79
+
80
+ context "with changed major currency to AUD" do
81
+ subject do
82
+ Currency.major_codes = [ "AUD" ]
83
+ Currency.major_currencies_selection(currencies)
84
+ end
85
+
86
+ it { should == aud }
87
+ end
88
+ end
89
+
90
+ describe ".best_from_currencies" do
91
+ let(:usd) { mock }
92
+ let(:pln) { mock }
93
+ let(:aud) { mock }
94
+
95
+ context "when given currencies is nil" do
96
+ subject { Currency.best_from_currencies(nil) }
97
+
98
+ it { should be_nil }
99
+ end
100
+
101
+ context "when given currencies is empty array" do
102
+ subject { Currency.best_from_currencies([]) }
103
+
104
+ it { should be_nil }
105
+ end
106
+
107
+ context "when major currency exist within currencies" do
108
+ let(:valid_currencies) { mock(:nil? => false, :empty? => false) }
109
+ subject do
110
+ Currency.stub!(:major_currencies_selection => [mock, usd])
111
+ Currency.best_from_currencies(valid_currencies)
112
+ end
113
+
114
+ it { should == usd }
115
+ end
116
+
117
+ context "when major currency does not exist within currencies" do
118
+ let(:valid_currencies) { [ [mock, pln], [mock, aud] ] }
119
+ subject do
120
+ Currency.stub!(:major_currencies_selection => nil)
121
+ Currency.best_from_currencies(valid_currencies)
122
+ end
123
+
124
+ it { should == pln }
125
+ end
126
+ end
127
+
128
+ describe ".list_from_name" do
129
+ let(:euro_currency) { mock(:name => "Euro") }
130
+ let(:euro) { [ [ "EUR", euro_currency ] ] }
131
+ let(:dollar_currency) { mock(:name => "Dollars") }
132
+ let(:dollars) { [
133
+ [ "USD", dollar_currency ],
134
+ [ "AUD", dollar_currency ]
135
+ ] }
136
+ let(:currencies) { euro + dollars }
137
+
138
+ subject do
139
+ Currency.stub!(:currencies => currencies)
140
+ Currency.list_from_name("Dollars")
141
+ end
142
+
143
+ it { should == dollars }
144
+ end
145
+
146
+ describe ".list_from_symbol" do
147
+ let(:euro_currency) { mock(:symbol => "€") }
148
+ let(:euro) { [ [ "EUR", euro_currency ] ] }
149
+ let(:dollar_currency) { mock(:symbol => "$") }
150
+ let(:dollars) { [
151
+ [ "USD", dollar_currency ],
152
+ [ "AUD", dollar_currency ]
153
+ ] }
154
+ let(:currencies) { euro + dollars }
155
+
156
+ subject do
157
+ Currency.stub!(:currencies => currencies)
158
+ Currency.list_from_symbol("$")
159
+ end
160
+
161
+ it { should == dollars }
162
+ end
163
+
164
+ describe ".best_from_name" do
165
+ let(:name) { mock }
166
+ let(:list_from_name) { mock }
167
+ describe "behavior" do
168
+ before do
169
+ Currency.stub!(
170
+ :best_from_currencies => nil,
171
+ :list_from_name => list_from_name
172
+ )
173
+ end
174
+ after { Currency.best_from_name(name) }
175
+
176
+ it "should select best from list of currencies with given name" do
177
+ Currency.should_receive(:best_from_currencies).with(list_from_name)
178
+ end
179
+
180
+ it "should select list of currencies with given name" do
181
+ Currency.should_receive(:list_from_name).with(name).and_return(list_from_name)
182
+ end
183
+ end
184
+
185
+ describe "returns" do
186
+ let(:best_from_currencies) { mock }
187
+ subject do
188
+ Currency.stub!(
189
+ :best_from_currencies => best_from_currencies,
190
+ :list_from_name => list_from_name
191
+ )
192
+ Currency.best_from_name(name)
193
+ end
194
+
195
+ it { should == best_from_currencies }
196
+ end
197
+ end
198
+
199
+ describe ".best_from_symbol" do
200
+ let(:symbol) { mock }
201
+ let(:list_from_symbol) { mock }
202
+ describe "behavior" do
203
+ before do
204
+ Currency.stub!(
205
+ :best_from_currencies => nil,
206
+ :list_from_symbol => list_from_symbol
207
+ )
208
+ end
209
+ after { Currency.best_from_symbol(symbol) }
210
+
211
+ it "should select best from list of currencies with given symbol" do
212
+ Currency.should_receive(:best_from_currencies).with(list_from_symbol)
213
+ end
214
+
215
+ it "should select list of currencies with given symbol" do
216
+ Currency.should_receive(:list_from_symbol).with(symbol).and_return(list_from_symbol)
217
+ end
218
+ end
219
+
220
+ describe "returns" do
221
+ let(:best_from_currencies) { mock }
222
+ subject do
223
+ Currency.stub!(
224
+ :best_from_currencies => best_from_currencies,
225
+ :list_from_symbol => list_from_symbol
226
+ )
227
+ Currency.best_from_symbol(symbol)
228
+ end
229
+
230
+ it { should == best_from_currencies }
231
+ end
232
+ end
233
+
234
+ describe ".best_guess" do
235
+ let(:eur) { mock }
236
+ let(:string) { mock(:nil? => false, :empty? => false) }
237
+
238
+ context "when string not given" do
239
+ subject { Currency.best_guess(nil) }
240
+
241
+ it { should be_nil }
242
+ end
243
+
244
+ context "when given empty string" do
245
+ subject { Currency.best_guess("") }
246
+
247
+ it { should be_nil }
248
+ end
249
+
250
+ context "when code equal to string exist" do
251
+ subject do
252
+ Currency.stub!(:from_code).with(string).and_return(eur)
253
+ Currency.best_guess(string)
254
+ end
255
+
256
+ it { should == eur }
257
+ end
258
+
259
+ context "when best symbol equal to string exist" do
260
+ subject do
261
+ Currency.stub!(:from_code).with(string).and_return(nil)
262
+ Currency.stub!(:best_from_symbol).with(string).and_return(eur)
263
+ Currency.best_guess(string)
264
+ end
265
+
266
+ it { should == eur }
267
+ end
268
+
269
+ context "when best name equal to string exist" do
270
+ subject do
271
+ Currency.stub!(:from_code).with(string).and_return(nil)
272
+ Currency.stub!(:best_from_symbol).with(string).and_return(nil)
273
+ Currency.stub!(:best_from_name).with(string).and_return(eur)
274
+ Currency.best_guess(string)
275
+ end
276
+
277
+ it { should == eur }
278
+ end
279
+
280
+ context "when string not exist in any form" do
281
+ subject do
282
+ Currency.stub!(:from_code).with(string).and_return(nil)
283
+ Currency.stub!(:best_from_symbol).with(string).and_return(nil)
284
+ Currency.stub!(:best_from_name).with(string).and_return(nil)
285
+ Currency.best_guess(string)
286
+ end
287
+
288
+ it { should be_nil }
289
+ end
290
+
291
+ describe "behavior" do
292
+ it "should run methods in proper order" do
293
+ Currency.should_receive(:from_code).ordered.with("string").and_return(nil)
294
+ Currency.should_receive(:best_from_symbol).ordered.with("string").and_return(nil)
295
+ Currency.should_receive(:best_from_name).ordered.with("string").and_return(nil)
296
+ Currency.best_guess("string")
297
+ end
298
+ end
299
+ end
300
+
301
+ describe ".code_from_best_guess" do
302
+ let(:string) { mock }
303
+ let(:code) { mock }
304
+ let(:best_guess) { mock(:try => code) }
305
+
306
+ context "when there is best guess" do
307
+ subject do
308
+ Currency.stub!(:best_guess).with(string).and_return(best_guess)
309
+ Currency.code_from_best_guess(string)
310
+ end
311
+
312
+ it { should == code }
313
+ end
314
+
315
+ context "when there is no best guess" do
316
+ let(:best_guess) { mock(:try => nil) }
317
+
318
+ subject do
319
+ Currency.stub!(:best_guess).with(string).and_return(best_guess)
320
+ Currency.code_from_best_guess(string)
321
+ end
322
+
323
+ it { should be_nil }
324
+ end
325
+
326
+ describe "behavior" do
327
+ before { Currency.stub!(:best_guess).with(string).and_return(best_guess) }
328
+ after { Currency.code_from_best_guess(string) }
329
+
330
+ it "should call .best_guess" do
331
+ Currency.should_receive(:best_guess).with(string).and_return(best_guess)
332
+ end
333
+
334
+ it "should call #code on best guessed" do
335
+ best_guess.should_receive(:try).with(:code)
336
+ end
337
+ end
338
+ end
339
+
340
+ end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: currencies
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 19
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
- - 2
8
- - 1
9
- version: 0.2.1
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - hexorx
@@ -14,33 +15,69 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-03-30 00:00:00 -06:00
18
+ date: 2011-01-10 00:00:00 -07:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ prerelease: false
24
+ name: jeweler
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - "="
29
+ - !ruby/object:Gem::Version
30
+ hash: 7
31
+ segments:
32
+ - 1
33
+ - 4
34
+ - 0
35
+ version: 1.4.0
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ type: :runtime
39
+ prerelease: false
21
40
  name: rspec
41
+ version_requirements: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - "="
45
+ - !ruby/object:Gem::Version
46
+ hash: 25
47
+ segments:
48
+ - 1
49
+ - 3
50
+ - 1
51
+ version: 1.3.1
52
+ requirement: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ type: :development
22
55
  prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
56
+ name: rspec
57
+ version_requirements: &id003 !ruby/object:Gem::Requirement
58
+ none: false
24
59
  requirements:
25
60
  - - ">="
26
61
  - !ruby/object:Gem::Version
62
+ hash: 3
27
63
  segments:
28
64
  - 0
29
65
  version: "0"
30
- type: :development
31
- version_requirements: *id001
66
+ requirement: *id003
32
67
  - !ruby/object:Gem::Dependency
33
- name: yard
68
+ type: :development
34
69
  prerelease: false
35
- requirement: &id002 !ruby/object:Gem::Requirement
70
+ name: yard
71
+ version_requirements: &id004 !ruby/object:Gem::Requirement
72
+ none: false
36
73
  requirements:
37
74
  - - ">="
38
75
  - !ruby/object:Gem::Version
76
+ hash: 3
39
77
  segments:
40
78
  - 0
41
79
  version: "0"
42
- type: :development
43
- version_requirements: *id002
80
+ requirement: *id004
44
81
  description: "If you are tracking any kind of assets the currencies gem is for you. It contains every currency in the ISO 4217 standard and allows you to add your own as well. So if you decide to take sparkly buttons as a form of payment you can use currencies to display the shiny button unicode symbol \xE2\x98\xBC (disclaimer: \xE2\x98\xBC may not look like a shiny button to everyone.) when used with something like the money gem. Speaking of the money gem, currencies gives you an ExchangeBank that the money gem can use to convert from one currency to another. There are plans to have ExchangeRate provider plugin system. Right now the rates are either set manually or pulled from Yahoo Finance."
45
82
  email: hexorx@gmail.com
46
83
  executables: []
@@ -52,7 +89,9 @@ extra_rdoc_files:
52
89
  - README.markdown
53
90
  files:
54
91
  - .document
55
- - .gitignore
92
+ - .rvmrc
93
+ - Gemfile
94
+ - Gemfile.lock
56
95
  - LICENSE
57
96
  - README.markdown
58
97
  - Rakefile
@@ -71,28 +110,32 @@ homepage: http://github.com/hexorx/currencies
71
110
  licenses: []
72
111
 
73
112
  post_install_message:
74
- rdoc_options:
75
- - --charset=UTF-8
113
+ rdoc_options: []
114
+
76
115
  require_paths:
77
116
  - lib
78
117
  required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
79
119
  requirements:
80
120
  - - ">="
81
121
  - !ruby/object:Gem::Version
122
+ hash: 3
82
123
  segments:
83
124
  - 0
84
125
  version: "0"
85
126
  required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
86
128
  requirements:
87
129
  - - ">="
88
130
  - !ruby/object:Gem::Version
131
+ hash: 3
89
132
  segments:
90
133
  - 0
91
134
  version: "0"
92
135
  requirements: []
93
136
 
94
137
  rubyforge_project:
95
- rubygems_version: 1.3.6
138
+ rubygems_version: 1.3.7
96
139
  signing_key:
97
140
  specification_version: 3
98
141
  summary: Simple gem for working with currencies. It is extracted from the countries gem and contains all the currency information in the ISO 4217 standard.
data/.gitignore DELETED
@@ -1,6 +0,0 @@
1
- *.sw?
2
- .DS_Store
3
- coverage
4
- rdoc
5
- pkg
6
- .rake_tasks~