countries 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -5,6 +5,6 @@ gem 'currencies', '=0.4.0', :require => 'iso4217'
5
5
  group :development, :test do
6
6
  gem 'jeweler', '>=1.6.4'
7
7
  gem 'rspec', '=1.3.1'
8
- gem 'rake'
8
+ gem 'rake', '0.8.7'
9
9
  gem 'yard', '=0.6.4'
10
10
  end
data/Gemfile.lock CHANGED
@@ -17,6 +17,6 @@ PLATFORMS
17
17
  DEPENDENCIES
18
18
  currencies (= 0.4.0)
19
19
  jeweler (>= 1.6.4)
20
- rake
20
+ rake (= 0.8.7)
21
21
  rspec (= 1.3.1)
22
22
  yard (= 0.6.4)
data/README.markdown CHANGED
@@ -35,7 +35,19 @@ Simply load a new country object using Country.new(*alpha2*) or the shortcut Cou
35
35
 
36
36
  c = Country.new('US')
37
37
  c = Country['US']
38
-
38
+
39
+ Attribute-Based Finder Methods
40
+ ------------
41
+
42
+ You can lookup a country or an array of countries using any of the data attributes via the find\_country\_by_*attribute* dynamic methods:
43
+
44
+ c = Country.find_country_by_name('united states')
45
+ list = Country.find_all_countries_by_region('Americas')
46
+ c = Country.find_country_by_alpha3('can')
47
+
48
+ For a list of available attributes please see ISO3166::Country::AttrReaders.
49
+ Note: searches are *case insensitive*.
50
+
39
51
  Country Info
40
52
  ------------
41
53
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.3
1
+ 0.7.0
data/countries.gemspec CHANGED
@@ -4,14 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = "countries"
8
- s.version = "0.6.3"
7
+ s.name = %q{countries}
8
+ s.version = "0.7.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", "Joe Corcoran"]
12
- s.date = "2011-11-22"
13
- s.description = "All sorts of useful information about every country packaged as pretty little country objects. It includes data from ISO 3166 (countries and subdivisions), ISO 4217 (currency), and E.164 (phone numbers). As a bonus it even adds a country_select helper to rails projects."
14
- s.email = "hexorx@gmail.com"
12
+ s.date = %q{2011-12-06}
13
+ s.description = %q{All sorts of useful information about every country packaged as pretty little country objects. It includes data from ISO 3166 (countries and subdivisions), ISO 4217 (currency), and E.164 (phone numbers). As a bonus it even adds a country_select helper to rails projects.}
14
+ s.email = %q{hexorx@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
17
  "README.markdown"
@@ -229,19 +229,20 @@ Gem::Specification.new do |s|
229
229
  "spec/country_spec.rb",
230
230
  "spec/spec_helper.rb"
231
231
  ]
232
- s.homepage = "http://github.com/hexorx/countries"
232
+ s.homepage = %q{http://github.com/hexorx/countries}
233
233
  s.require_paths = ["lib"]
234
- s.rubygems_version = "1.8.10"
235
- s.summary = "Gives you a country object full of all sorts of useful information."
234
+ s.rubygems_version = %q{1.3.7}
235
+ s.summary = %q{Gives you a country object full of all sorts of useful information.}
236
236
 
237
237
  if s.respond_to? :specification_version then
238
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
238
239
  s.specification_version = 3
239
240
 
240
241
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
241
242
  s.add_runtime_dependency(%q<currencies>, ["= 0.4.0"])
242
243
  s.add_development_dependency(%q<jeweler>, [">= 1.6.4"])
243
244
  s.add_development_dependency(%q<rspec>, ["= 1.3.1"])
244
- s.add_development_dependency(%q<rake>, [">= 0"])
245
+ s.add_development_dependency(%q<rake>, ["= 0.8.7"])
245
246
  s.add_development_dependency(%q<yard>, ["= 0.6.4"])
246
247
  s.add_runtime_dependency(%q<currencies>, [">= 0.2.0"])
247
248
  s.add_development_dependency(%q<rspec>, [">= 0"])
@@ -250,7 +251,7 @@ Gem::Specification.new do |s|
250
251
  s.add_dependency(%q<currencies>, ["= 0.4.0"])
251
252
  s.add_dependency(%q<jeweler>, [">= 1.6.4"])
252
253
  s.add_dependency(%q<rspec>, ["= 1.3.1"])
253
- s.add_dependency(%q<rake>, [">= 0"])
254
+ s.add_dependency(%q<rake>, ["= 0.8.7"])
254
255
  s.add_dependency(%q<yard>, ["= 0.6.4"])
255
256
  s.add_dependency(%q<currencies>, [">= 0.2.0"])
256
257
  s.add_dependency(%q<rspec>, [">= 0"])
@@ -260,7 +261,7 @@ Gem::Specification.new do |s|
260
261
  s.add_dependency(%q<currencies>, ["= 0.4.0"])
261
262
  s.add_dependency(%q<jeweler>, [">= 1.6.4"])
262
263
  s.add_dependency(%q<rspec>, ["= 1.3.1"])
263
- s.add_dependency(%q<rake>, [">= 0"])
264
+ s.add_dependency(%q<rake>, ["= 0.8.7"])
264
265
  s.add_dependency(%q<yard>, ["= 0.6.4"])
265
266
  s.add_dependency(%q<currencies>, [">= 0.2.0"])
266
267
  s.add_dependency(%q<rspec>, [">= 0"])
@@ -9,6 +9,7 @@ class ISO3166::Country
9
9
  :number,
10
10
  :alpha2,
11
11
  :alpha3,
12
+ :currency,
12
13
  :name,
13
14
  :names,
14
15
  :latitude,
@@ -31,8 +32,8 @@ class ISO3166::Country
31
32
 
32
33
  attr_reader :data
33
34
 
34
- def initialize(country_code)
35
- @data = Data[country_code]
35
+ def initialize(country_data)
36
+ @data = country_data.is_a?(Hash) ? country_data : Data[country_data]
36
37
  end
37
38
 
38
39
  def valid?
@@ -68,17 +69,33 @@ class ISO3166::Country
68
69
  def [](query)
69
70
  self.search(query)
70
71
  end
71
-
72
- def find_by_name(name)
73
- name.downcase!
74
- Data.select do |k,v|
75
- v["name"].downcase == name || v["names"].map{ |n| n.downcase }.include?(name)
76
- end.first
72
+
73
+ def method_missing(*m)
74
+ if m.first.to_s.match /^find_(country_)?by_(.+)/
75
+ country = self.find_all_by($~[2].downcase, m[1].to_s.downcase).first
76
+ $~[1].nil? ? country : self.new(country.last) if country
77
+ elsif m.first.to_s.match /^find_all_(countries_)?by_(.+)/
78
+ self.find_all_by($~[2].downcase, m[1].to_s.downcase).inject([]) do |list, c|
79
+ list << ($~[1].nil? ? c : self.new(c.last)) if c
80
+ list
81
+ end
82
+ else
83
+ super
84
+ end
77
85
  end
78
-
79
- def find_country_by_name(name)
80
- result = self.find_by_name(name)
81
- result ? self.new(result.first) : nil
86
+
87
+ def find_all_by(attribute, val)
88
+ raise "Invalid attribute name '#{attribute}'" unless AttrReaders.include?(attribute.to_sym)
89
+ attribute = ['name', 'names'] if attribute == 'name'
90
+ Data.select do |k,v|
91
+ Array(attribute).map do |attr|
92
+ if v[attr].kind_of?(Enumerable)
93
+ v[attr].map{ |n| n.downcase }.include?(val)
94
+ else
95
+ v[attr] && v[attr].downcase == val
96
+ end
97
+ end.uniq.include?(true)
98
+ end
82
99
  end
83
100
  end
84
101
  end
@@ -24,7 +24,7 @@ module ActionView
24
24
  def to_country_select_tag(priority_countries, options, html_options)
25
25
  html_options = html_options.stringify_keys
26
26
  add_default_name_and_id(html_options)
27
- value = value(object)
27
+ value = options.delete(:selected) || value(object)
28
28
  content_tag("select",
29
29
  add_options(
30
30
  country_options_for_select(value, priority_countries),
data/lib/iso3166.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  $LOAD_PATH << File.expand_path(File.dirname(__FILE__))
2
2
 
3
- require 'YAML' unless defined?(YAML)
3
+ require 'yaml' unless defined?(YAML::ENGINE)
4
4
  require 'iso4217'
5
5
 
6
6
  require 'countries/select_helper'
data/spec/country_spec.rb CHANGED
@@ -155,7 +155,7 @@ describe ISO3166::Country do
155
155
  end
156
156
  end
157
157
 
158
- describe ".find_by_name" do
158
+ describe "hash finder methods" do
159
159
  context "when search name in 'name'" do
160
160
  subject { ISO3166::Country.find_by_name("Poland") }
161
161
 
@@ -173,9 +173,32 @@ describe ISO3166::Country do
173
173
 
174
174
  its(:first) { should == "PL" }
175
175
  end
176
+
177
+ context "when finding by invalid attribute" do
178
+ it "should raise an error" do
179
+ lambda { ISO3166::Country.find_by_invalid('invalid') }.should raise_error
180
+ end
181
+ end
182
+
183
+ context "when using find_all method" do
184
+ let(:list) { ISO3166::Country.find_all_by_currency('USD') }
185
+
186
+ it "should be an Array of Arrays" do
187
+ list.should be_a(Array)
188
+ list.first.should be_a(Array)
189
+ end
190
+ end
191
+
192
+ context "when using find_by method" do
193
+ subject { ISO3166::Country.find_by_alpha3('CAN') }
194
+
195
+ its(:length) { should == 2 }
196
+ its(:first) { should be_a(String) }
197
+ its(:last) { should be_a(Hash) }
198
+ end
176
199
  end
177
200
 
178
- describe ".find_country_by_name" do
201
+ describe "country finder methods" do
179
202
  context "when search name found" do
180
203
  let(:uk) { ISO3166::Country.find_country_by_name("United Kingdom") }
181
204
 
@@ -201,6 +224,29 @@ describe ISO3166::Country do
201
224
  bogus.should == nil
202
225
  end
203
226
  end
227
+
228
+ context "when finding by invalid attribute" do
229
+ it "should raise an error" do
230
+ lambda { ISO3166::Country.find_country_by_invalid('invalid') }.should raise_error
231
+ end
232
+ end
233
+
234
+ context "when using find_all method" do
235
+ let(:list) { ISO3166::Country.find_all_countries_by_currency('USD') }
236
+
237
+ it "should be an Array of Country objects" do
238
+ list.should be_a(Array)
239
+ list.first.should be_a(ISO3166::Country)
240
+ end
241
+ end
242
+
243
+ context "when using find_by method" do
244
+ let(:country) { ISO3166::Country.find_country_by_alpha3('CAN') }
245
+
246
+ it 'should be a single country object' do
247
+ country.should be_a(ISO3166::Country)
248
+ end
249
+ end
204
250
  end
205
251
 
206
252
  describe "names in Data" do
metadata CHANGED
@@ -1,116 +1,167 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: countries
3
- version: !ruby/object:Gem::Version
4
- version: 0.6.3
5
- prerelease:
3
+ version: !ruby/object:Gem::Version
4
+ hash: 3
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 7
9
+ - 0
10
+ segments_generated: true
11
+ version: 0.7.0
6
12
  platform: ruby
7
- authors:
13
+ authors:
8
14
  - hexorx
9
15
  - Joe Corcoran
10
16
  autorequire:
11
17
  bindir: bin
12
18
  cert_chain: []
13
- date: 2011-11-22 00:00:00.000000000Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
19
+
20
+ date: 2011-12-06 00:00:00 -07:00
21
+ default_executable:
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
16
24
  name: currencies
17
- requirement: &70265478453600 !ruby/object:Gem::Requirement
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
18
26
  none: false
19
- requirements:
20
- - - =
21
- - !ruby/object:Gem::Version
27
+ requirements:
28
+ - - "="
29
+ - !ruby/object:Gem::Version
30
+ hash: 15
31
+ segments:
32
+ - 0
33
+ - 4
34
+ - 0
35
+ segments_generated: true
22
36
  version: 0.4.0
23
- type: :runtime
24
37
  prerelease: false
25
- version_requirements: *70265478453600
26
- - !ruby/object:Gem::Dependency
38
+ requirement: *id001
39
+ type: :runtime
40
+ - !ruby/object:Gem::Dependency
27
41
  name: jeweler
28
- requirement: &70265478453080 !ruby/object:Gem::Requirement
42
+ version_requirements: &id002 !ruby/object:Gem::Requirement
29
43
  none: false
30
- requirements:
31
- - - ! '>='
32
- - !ruby/object:Gem::Version
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 7
48
+ segments:
49
+ - 1
50
+ - 6
51
+ - 4
52
+ segments_generated: true
33
53
  version: 1.6.4
34
- type: :development
35
54
  prerelease: false
36
- version_requirements: *70265478453080
37
- - !ruby/object:Gem::Dependency
55
+ requirement: *id002
56
+ type: :development
57
+ - !ruby/object:Gem::Dependency
38
58
  name: rspec
39
- requirement: &70265478452560 !ruby/object:Gem::Requirement
59
+ version_requirements: &id003 !ruby/object:Gem::Requirement
40
60
  none: false
41
- requirements:
42
- - - =
43
- - !ruby/object:Gem::Version
61
+ requirements:
62
+ - - "="
63
+ - !ruby/object:Gem::Version
64
+ hash: 25
65
+ segments:
66
+ - 1
67
+ - 3
68
+ - 1
69
+ segments_generated: true
44
70
  version: 1.3.1
45
- type: :development
46
71
  prerelease: false
47
- version_requirements: *70265478452560
48
- - !ruby/object:Gem::Dependency
72
+ requirement: *id003
73
+ type: :development
74
+ - !ruby/object:Gem::Dependency
49
75
  name: rake
50
- requirement: &70265478451980 !ruby/object:Gem::Requirement
76
+ version_requirements: &id004 !ruby/object:Gem::Requirement
51
77
  none: false
52
- requirements:
53
- - - ! '>='
54
- - !ruby/object:Gem::Version
55
- version: '0'
56
- type: :development
78
+ requirements:
79
+ - - "="
80
+ - !ruby/object:Gem::Version
81
+ hash: 49
82
+ segments:
83
+ - 0
84
+ - 8
85
+ - 7
86
+ segments_generated: true
87
+ version: 0.8.7
57
88
  prerelease: false
58
- version_requirements: *70265478451980
59
- - !ruby/object:Gem::Dependency
89
+ requirement: *id004
90
+ type: :development
91
+ - !ruby/object:Gem::Dependency
60
92
  name: yard
61
- requirement: &70265478451480 !ruby/object:Gem::Requirement
93
+ version_requirements: &id005 !ruby/object:Gem::Requirement
62
94
  none: false
63
- requirements:
64
- - - =
65
- - !ruby/object:Gem::Version
95
+ requirements:
96
+ - - "="
97
+ - !ruby/object:Gem::Version
98
+ hash: 15
99
+ segments:
100
+ - 0
101
+ - 6
102
+ - 4
103
+ segments_generated: true
66
104
  version: 0.6.4
67
- type: :development
68
105
  prerelease: false
69
- version_requirements: *70265478451480
70
- - !ruby/object:Gem::Dependency
106
+ requirement: *id005
107
+ type: :development
108
+ - !ruby/object:Gem::Dependency
71
109
  name: currencies
72
- requirement: &70265478450880 !ruby/object:Gem::Requirement
110
+ version_requirements: &id006 !ruby/object:Gem::Requirement
73
111
  none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ hash: 23
116
+ segments:
117
+ - 0
118
+ - 2
119
+ - 0
120
+ segments_generated: true
77
121
  version: 0.2.0
78
- type: :runtime
79
122
  prerelease: false
80
- version_requirements: *70265478450880
81
- - !ruby/object:Gem::Dependency
123
+ requirement: *id006
124
+ type: :runtime
125
+ - !ruby/object:Gem::Dependency
82
126
  name: rspec
83
- requirement: &70265478450320 !ruby/object:Gem::Requirement
127
+ version_requirements: &id007 !ruby/object:Gem::Requirement
84
128
  none: false
85
- requirements:
86
- - - ! '>='
87
- - !ruby/object:Gem::Version
88
- version: '0'
89
- type: :development
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ hash: 3
133
+ segments:
134
+ - 0
135
+ segments_generated: true
136
+ version: "0"
90
137
  prerelease: false
91
- version_requirements: *70265478450320
92
- - !ruby/object:Gem::Dependency
138
+ requirement: *id007
139
+ type: :development
140
+ - !ruby/object:Gem::Dependency
93
141
  name: yard
94
- requirement: &70265478449660 !ruby/object:Gem::Requirement
142
+ version_requirements: &id008 !ruby/object:Gem::Requirement
95
143
  none: false
96
- requirements:
97
- - - ! '>='
98
- - !ruby/object:Gem::Version
99
- version: '0'
100
- type: :development
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ hash: 3
148
+ segments:
149
+ - 0
150
+ segments_generated: true
151
+ version: "0"
101
152
  prerelease: false
102
- version_requirements: *70265478449660
103
- description: All sorts of useful information about every country packaged as pretty
104
- little country objects. It includes data from ISO 3166 (countries and subdivisions),
105
- ISO 4217 (currency), and E.164 (phone numbers). As a bonus it even adds a country_select
106
- helper to rails projects.
153
+ requirement: *id008
154
+ type: :development
155
+ description: All sorts of useful information about every country packaged as pretty little country objects. It includes data from ISO 3166 (countries and subdivisions), ISO 4217 (currency), and E.164 (phone numbers). As a bonus it even adds a country_select helper to rails projects.
107
156
  email: hexorx@gmail.com
108
157
  executables: []
158
+
109
159
  extensions: []
110
- extra_rdoc_files:
160
+
161
+ extra_rdoc_files:
111
162
  - LICENSE
112
163
  - README.markdown
113
- files:
164
+ files:
114
165
  - .document
115
166
  - .rvmrc
116
167
  - Gemfile
@@ -322,31 +373,41 @@ files:
322
373
  - lib/iso3166.rb
323
374
  - spec/country_spec.rb
324
375
  - spec/spec_helper.rb
376
+ has_rdoc: true
325
377
  homepage: http://github.com/hexorx/countries
326
378
  licenses: []
379
+
327
380
  post_install_message:
328
381
  rdoc_options: []
329
- require_paths:
382
+
383
+ require_paths:
330
384
  - lib
331
- required_ruby_version: !ruby/object:Gem::Requirement
385
+ required_ruby_version: !ruby/object:Gem::Requirement
332
386
  none: false
333
- requirements:
334
- - - ! '>='
335
- - !ruby/object:Gem::Version
336
- version: '0'
337
- segments:
387
+ requirements:
388
+ - - ">="
389
+ - !ruby/object:Gem::Version
390
+ hash: 3
391
+ segments:
338
392
  - 0
339
- hash: -4335846236134678826
340
- required_rubygems_version: !ruby/object:Gem::Requirement
393
+ segments_generated: true
394
+ version: "0"
395
+ required_rubygems_version: !ruby/object:Gem::Requirement
341
396
  none: false
342
- requirements:
343
- - - ! '>='
344
- - !ruby/object:Gem::Version
345
- version: '0'
397
+ requirements:
398
+ - - ">="
399
+ - !ruby/object:Gem::Version
400
+ hash: 3
401
+ segments:
402
+ - 0
403
+ segments_generated: true
404
+ version: "0"
346
405
  requirements: []
406
+
347
407
  rubyforge_project:
348
- rubygems_version: 1.8.10
408
+ rubygems_version: 1.3.7
349
409
  signing_key:
350
410
  specification_version: 3
351
411
  summary: Gives you a country object full of all sorts of useful information.
352
412
  test_files: []
413
+