countries 0.6.1 → 0.7.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/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/Rakefile CHANGED
@@ -9,7 +9,7 @@ begin
9
9
  gem.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.'
10
10
  gem.email = "hexorx@gmail.com"
11
11
  gem.homepage = "http://github.com/hexorx/countries"
12
- gem.authors = ["hexorx"]
12
+ gem.authors = ["hexorx", "Joe Corcoran"]
13
13
  gem.add_dependency('currencies', '>= 0.2.0')
14
14
  gem.add_development_dependency "rspec"
15
15
  gem.add_development_dependency "yard"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.1
1
+ 0.7.0
data/countries.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{countries}
8
- s.version = "0.6.1"
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
- s.authors = [%q{hexorx}]
12
- s.date = %q{2011-09-16}
11
+ s.authors = ["hexorx", "Joe Corcoran"]
12
+ s.date = %q{2011-12-06}
13
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
14
  s.email = %q{hexorx@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -230,18 +230,19 @@ Gem::Specification.new do |s|
230
230
  "spec/spec_helper.rb"
231
231
  ]
232
232
  s.homepage = %q{http://github.com/hexorx/countries}
233
- s.require_paths = [%q{lib}]
234
- s.rubygems_version = %q{1.8.6}
233
+ s.require_paths = ["lib"]
234
+ s.rubygems_version = %q{1.3.7}
235
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
@@ -11,12 +11,12 @@ module ActionView
11
11
  country_options = ""
12
12
 
13
13
  if priority_countries
14
- priority_countries = [*priority_countries].map {|x| [x,Country::NameIndex[x]] }
14
+ priority_countries = [*priority_countries].map {|x| [x,ISO3166::Country::NameIndex[x]] }
15
15
  country_options += options_for_select(priority_countries, selected)
16
16
  country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
17
17
  end
18
18
 
19
- return country_options + options_for_select(Country::Names, selected)
19
+ return country_options + options_for_select(ISO3166::Country::Names, selected)
20
20
  end
21
21
  end
22
22
 
@@ -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),
@@ -40,4 +40,4 @@ module ActionView
40
40
  end
41
41
  end
42
42
  end
43
- end
43
+ end
@@ -201,7 +201,7 @@ AO:
201
201
  AQ:
202
202
  alpha2: AQ
203
203
  alpha3: ATA
204
- country_code: ""
204
+ country_code: "672"
205
205
  currency: ~
206
206
  international_prefix: ""
207
207
  latitude: "90 00 S"
@@ -350,7 +350,7 @@ AW:
350
350
  AX:
351
351
  alpha2: AX
352
352
  alpha3: ALA
353
- country_code: ""
353
+ country_code: "358"
354
354
  currency: ~
355
355
  international_prefix: ""
356
356
  latitude: ""
@@ -597,7 +597,7 @@ BJ:
597
597
  BL:
598
598
  alpha2: BL
599
599
  alpha3: BLM
600
- country_code: ""
600
+ country_code: "590"
601
601
  currency: ~
602
602
  international_prefix: ""
603
603
  latitude: "17 90 N"
@@ -963,9 +963,9 @@ CI:
963
963
  international_prefix: "00"
964
964
  latitude: "8 00 N"
965
965
  longitude: "5 00 W"
966
- name: "Côte D\\'Ivoire"
966
+ name: "Côte D'Ivoire"
967
967
  names:
968
- - "Côte D\\'Ivoire"
968
+ - "Côte D'Ivoire"
969
969
  national_destination_code_lengths:
970
970
  - 2
971
971
  national_number_lengths:
@@ -1153,7 +1153,7 @@ CV:
1153
1153
  CX:
1154
1154
  alpha2: CX
1155
1155
  alpha3: CXR
1156
- country_code: "618"
1156
+ country_code: "61"
1157
1157
  currency: AUD
1158
1158
  international_prefix: "0011"
1159
1159
  latitude: "10 30 S"
@@ -1431,7 +1431,7 @@ EG:
1431
1431
  EH:
1432
1432
  alpha2: EH
1433
1433
  alpha3: ESH
1434
- country_code: ""
1434
+ country_code: "212"
1435
1435
  currency: MAD
1436
1436
  international_prefix: ""
1437
1437
  latitude: "24 30 N"
@@ -1777,7 +1777,7 @@ GF:
1777
1777
  GG:
1778
1778
  alpha2: GG
1779
1779
  alpha3: GGY
1780
- country_code: ""
1780
+ country_code: "44"
1781
1781
  currency: GGP
1782
1782
  international_prefix: ""
1783
1783
  latitude: "49 28 N"
@@ -1975,7 +1975,7 @@ GR:
1975
1975
  GS:
1976
1976
  alpha2: GS
1977
1977
  alpha3: SGS
1978
- country_code: ""
1978
+ country_code: "500"
1979
1979
  currency: ~
1980
1980
  international_prefix: ""
1981
1981
  latitude: "54 30 S"
@@ -2290,7 +2290,7 @@ IL:
2290
2290
  IM:
2291
2291
  alpha2: IM
2292
2292
  alpha3: IMN
2293
- country_code: ""
2293
+ country_code: "44"
2294
2294
  currency: IMP
2295
2295
  international_prefix: ""
2296
2296
  latitude: "54 15 N"
@@ -2337,7 +2337,7 @@ IN:
2337
2337
  IO:
2338
2338
  alpha2: IO
2339
2339
  alpha3: IOT
2340
- country_code: ""
2340
+ country_code: "246"
2341
2341
  currency: USD
2342
2342
  international_prefix: ""
2343
2343
  latitude: "6 00 S"
@@ -2452,7 +2452,7 @@ IT:
2452
2452
  JE:
2453
2453
  alpha2: JE
2454
2454
  alpha3: JEY
2455
- country_code: ""
2455
+ country_code: "44"
2456
2456
  currency: JEP
2457
2457
  international_prefix: ""
2458
2458
  latitude: "49 15 N"
@@ -2803,7 +2803,7 @@ LA:
2803
2803
  international_prefix: "00"
2804
2804
  latitude: "18 00 N"
2805
2805
  longitude: "105 00 E"
2806
- name: "Lao People\\'s Democratic Republic"
2806
+ name: "Lao People's Democratic Republic"
2807
2807
  names:
2808
2808
  - Laos
2809
2809
  - Laos
@@ -3133,7 +3133,7 @@ ME:
3133
3133
  MF:
3134
3134
  alpha2: MF
3135
3135
  alpha3: MAF
3136
- country_code: ""
3136
+ country_code: "590"
3137
3137
  currency: EUR
3138
3138
  international_prefix: ""
3139
3139
  latitude: "18 05 N"
@@ -4043,7 +4043,7 @@ PM:
4043
4043
  PN:
4044
4044
  alpha2: PN
4045
4045
  alpha3: PCN
4046
- country_code: "872"
4046
+ country_code: ""
4047
4047
  currency: NZD
4048
4048
  international_prefix: "00"
4049
4049
  latitude: "25 04 S"
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,23 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: countries
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
5
- prerelease:
4
+ hash: 3
5
+ prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 6
9
- - 1
10
- version: 0.6.1
8
+ - 7
9
+ - 0
10
+ segments_generated: true
11
+ version: 0.7.0
11
12
  platform: ruby
12
13
  authors:
13
14
  - hexorx
15
+ - Joe Corcoran
14
16
  autorequire:
15
17
  bindir: bin
16
18
  cert_chain: []
17
19
 
18
- date: 2011-09-16 00:00:00 Z
20
+ date: 2011-12-06 00:00:00 -07:00
21
+ default_executable:
19
22
  dependencies:
20
23
  - !ruby/object:Gem::Dependency
24
+ name: currencies
21
25
  version_requirements: &id001 !ruby/object:Gem::Requirement
22
26
  none: false
23
27
  requirements:
@@ -28,12 +32,13 @@ dependencies:
28
32
  - 0
29
33
  - 4
30
34
  - 0
35
+ segments_generated: true
31
36
  version: 0.4.0
32
- name: currencies
33
- type: :runtime
34
37
  prerelease: false
35
38
  requirement: *id001
39
+ type: :runtime
36
40
  - !ruby/object:Gem::Dependency
41
+ name: jeweler
37
42
  version_requirements: &id002 !ruby/object:Gem::Requirement
38
43
  none: false
39
44
  requirements:
@@ -44,12 +49,13 @@ dependencies:
44
49
  - 1
45
50
  - 6
46
51
  - 4
52
+ segments_generated: true
47
53
  version: 1.6.4
48
- name: jeweler
49
- type: :development
50
54
  prerelease: false
51
55
  requirement: *id002
56
+ type: :development
52
57
  - !ruby/object:Gem::Dependency
58
+ name: rspec
53
59
  version_requirements: &id003 !ruby/object:Gem::Requirement
54
60
  none: false
55
61
  requirements:
@@ -60,26 +66,30 @@ dependencies:
60
66
  - 1
61
67
  - 3
62
68
  - 1
69
+ segments_generated: true
63
70
  version: 1.3.1
64
- name: rspec
65
- type: :development
66
71
  prerelease: false
67
72
  requirement: *id003
73
+ type: :development
68
74
  - !ruby/object:Gem::Dependency
75
+ name: rake
69
76
  version_requirements: &id004 !ruby/object:Gem::Requirement
70
77
  none: false
71
78
  requirements:
72
- - - ">="
79
+ - - "="
73
80
  - !ruby/object:Gem::Version
74
- hash: 3
81
+ hash: 49
75
82
  segments:
76
83
  - 0
77
- version: "0"
78
- name: rake
79
- type: :development
84
+ - 8
85
+ - 7
86
+ segments_generated: true
87
+ version: 0.8.7
80
88
  prerelease: false
81
89
  requirement: *id004
90
+ type: :development
82
91
  - !ruby/object:Gem::Dependency
92
+ name: yard
83
93
  version_requirements: &id005 !ruby/object:Gem::Requirement
84
94
  none: false
85
95
  requirements:
@@ -90,12 +100,13 @@ dependencies:
90
100
  - 0
91
101
  - 6
92
102
  - 4
103
+ segments_generated: true
93
104
  version: 0.6.4
94
- name: yard
95
- type: :development
96
105
  prerelease: false
97
106
  requirement: *id005
107
+ type: :development
98
108
  - !ruby/object:Gem::Dependency
109
+ name: currencies
99
110
  version_requirements: &id006 !ruby/object:Gem::Requirement
100
111
  none: false
101
112
  requirements:
@@ -106,12 +117,13 @@ dependencies:
106
117
  - 0
107
118
  - 2
108
119
  - 0
120
+ segments_generated: true
109
121
  version: 0.2.0
110
- name: currencies
111
- type: :runtime
112
122
  prerelease: false
113
123
  requirement: *id006
124
+ type: :runtime
114
125
  - !ruby/object:Gem::Dependency
126
+ name: rspec
115
127
  version_requirements: &id007 !ruby/object:Gem::Requirement
116
128
  none: false
117
129
  requirements:
@@ -120,12 +132,13 @@ dependencies:
120
132
  hash: 3
121
133
  segments:
122
134
  - 0
135
+ segments_generated: true
123
136
  version: "0"
124
- name: rspec
125
- type: :development
126
137
  prerelease: false
127
138
  requirement: *id007
139
+ type: :development
128
140
  - !ruby/object:Gem::Dependency
141
+ name: yard
129
142
  version_requirements: &id008 !ruby/object:Gem::Requirement
130
143
  none: false
131
144
  requirements:
@@ -134,11 +147,11 @@ dependencies:
134
147
  hash: 3
135
148
  segments:
136
149
  - 0
150
+ segments_generated: true
137
151
  version: "0"
138
- name: yard
139
- type: :development
140
152
  prerelease: false
141
153
  requirement: *id008
154
+ type: :development
142
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.
143
156
  email: hexorx@gmail.com
144
157
  executables: []
@@ -360,6 +373,7 @@ files:
360
373
  - lib/iso3166.rb
361
374
  - spec/country_spec.rb
362
375
  - spec/spec_helper.rb
376
+ has_rdoc: true
363
377
  homepage: http://github.com/hexorx/countries
364
378
  licenses: []
365
379
 
@@ -376,6 +390,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
376
390
  hash: 3
377
391
  segments:
378
392
  - 0
393
+ segments_generated: true
379
394
  version: "0"
380
395
  required_rubygems_version: !ruby/object:Gem::Requirement
381
396
  none: false
@@ -385,11 +400,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
385
400
  hash: 3
386
401
  segments:
387
402
  - 0
403
+ segments_generated: true
388
404
  version: "0"
389
405
  requirements: []
390
406
 
391
407
  rubyforge_project:
392
- rubygems_version: 1.8.6
408
+ rubygems_version: 1.3.7
393
409
  signing_key:
394
410
  specification_version: 3
395
411
  summary: Gives you a country object full of all sorts of useful information.