iso-country 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.DS_Store +0 -0
- data/data/.DS_Store +0 -0
- data/iso-country.gemspec +1 -1
- data/lib/.DS_Store +0 -0
- data/lib/iso-country/.DS_Store +0 -0
- data/lib/iso-country/data.rb +12 -0
- data/lib/iso-country/data/.DS_Store +0 -0
- data/lib/iso-country/data/20120215/.DS_Store +0 -0
- data/lib/iso-country/data/20120215/ISO_3166-1.core.json +1247 -0
- data/lib/iso-country/data/20120215/ISO_3166-1.i18n.json +749 -0
- data/lib/iso-country/exceptions.rb +8 -0
- data/lib/iso-country/finders.rb +87 -0
- data/lib/iso-country/instance.rb +37 -0
- data/lib/iso-country/mongoid.rb +17 -0
- data/lib/iso-country/version.rb +1 -1
- metadata +15 -2
@@ -0,0 +1,87 @@
|
|
1
|
+
module Iso
|
2
|
+
module Country
|
3
|
+
module Finders
|
4
|
+
def all
|
5
|
+
@countries.values || {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def find(*args)
|
9
|
+
unless args.first.is_a?(Symbol)
|
10
|
+
raise ArgumentError, 'Find takes one or more symbol IDs. Try #where'
|
11
|
+
end
|
12
|
+
|
13
|
+
case args.first
|
14
|
+
when :all
|
15
|
+
send(args.first)
|
16
|
+
else
|
17
|
+
find_with_ids(*args)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def find_with_ids(*ids)
|
22
|
+
case ids.size
|
23
|
+
when 0
|
24
|
+
raise "Couldn't find Country without an ID"
|
25
|
+
when 1
|
26
|
+
find_one(ids.first)
|
27
|
+
else
|
28
|
+
find_some(ids)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def find_one(id)
|
33
|
+
unless @countries[id]
|
34
|
+
raise CountryNotFound, "Couldn't find Country with id=#{id}"
|
35
|
+
end
|
36
|
+
|
37
|
+
@countries[id]
|
38
|
+
end
|
39
|
+
|
40
|
+
def find_some(ids)
|
41
|
+
ids.map {|id| find_one(id) }
|
42
|
+
end
|
43
|
+
|
44
|
+
def where(opts)
|
45
|
+
if opts.is_a?(Array) || opts.is_a?(Symbol)
|
46
|
+
find(opts)
|
47
|
+
elsif opts.is_a?(String) && opts.length == 2
|
48
|
+
find_with_alpha2(opts)
|
49
|
+
elsif opts.is_a?(String) && opts.length == 3
|
50
|
+
find_with_alpha3(opts)
|
51
|
+
elsif opts.is_a?(String) && opts.length > 3
|
52
|
+
find_with_name(opts)
|
53
|
+
elsif opts.is_a?(Numeric)
|
54
|
+
find_with_numeric(opts)
|
55
|
+
else
|
56
|
+
if opts[:alpha2]
|
57
|
+
find_with_alpha2(opts[:alpha2])
|
58
|
+
elsif opts[:alpha3]
|
59
|
+
find_with_alpha3(opts[:alpha3])
|
60
|
+
elsif opts[:numeric]
|
61
|
+
find_with_numeric(opts[:numeric])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def find_with_alpha2(alpha2)
|
67
|
+
all.find {|country| country.alpha2 == alpha2.to_s.upcase }
|
68
|
+
end
|
69
|
+
|
70
|
+
def find_with_alpha3(alpha3)
|
71
|
+
all.find {|country| country.alpha3 == alpha3.to_s.upcase }
|
72
|
+
end
|
73
|
+
|
74
|
+
def find_with_numeric(numeric)
|
75
|
+
all.find {|country| country.numeric == numeric.to_i }
|
76
|
+
end
|
77
|
+
|
78
|
+
def find_with_name(name, locale = nil)
|
79
|
+
if !locale
|
80
|
+
all.find {|country| country.names.values.include?(name) }
|
81
|
+
else
|
82
|
+
all.find {|country| country.names[locale] == name }
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Iso
|
2
|
+
module Country
|
3
|
+
module InstanceMethods
|
4
|
+
attr_reader :id, :alpha2, :alpha3, :numeric
|
5
|
+
|
6
|
+
def initialize(alpha2, alpha3, numeric, localizedNames = {})
|
7
|
+
@id = alpha2.downcase.to_sym
|
8
|
+
@alpha2 = alpha2
|
9
|
+
@alpha3 = alpha3
|
10
|
+
@numeric = numeric.to_i
|
11
|
+
@localizedNames = localizedNames
|
12
|
+
end
|
13
|
+
|
14
|
+
def names
|
15
|
+
@localizedNames
|
16
|
+
end
|
17
|
+
|
18
|
+
def name(locale = :en)
|
19
|
+
if @localizedNames.empty?
|
20
|
+
"#{alpha3} (#{"%03d" % numeric})"
|
21
|
+
else
|
22
|
+
@localizedNames[locale]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
alias :__to_s :to_s
|
27
|
+
|
28
|
+
def to_s
|
29
|
+
name
|
30
|
+
end
|
31
|
+
|
32
|
+
def inspect
|
33
|
+
__to_s()
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/iso-country/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iso-country
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -18,15 +18,28 @@ executables: []
|
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
+
- .DS_Store
|
21
22
|
- .gitignore
|
22
23
|
- Gemfile
|
23
24
|
- LICENSE
|
24
25
|
- README.md
|
25
26
|
- Rakefile
|
27
|
+
- data/.DS_Store
|
26
28
|
- iso-country.gemspec
|
29
|
+
- lib/.DS_Store
|
27
30
|
- lib/iso-country.rb
|
31
|
+
- lib/iso-country/.DS_Store
|
32
|
+
- lib/iso-country/data.rb
|
33
|
+
- lib/iso-country/data/.DS_Store
|
34
|
+
- lib/iso-country/data/20120215/.DS_Store
|
35
|
+
- lib/iso-country/data/20120215/ISO_3166-1.core.json
|
36
|
+
- lib/iso-country/data/20120215/ISO_3166-1.i18n.json
|
37
|
+
- lib/iso-country/exceptions.rb
|
38
|
+
- lib/iso-country/finders.rb
|
39
|
+
- lib/iso-country/instance.rb
|
40
|
+
- lib/iso-country/mongoid.rb
|
28
41
|
- lib/iso-country/version.rb
|
29
|
-
homepage:
|
42
|
+
homepage: https://github.com/farski/iso-country
|
30
43
|
licenses: []
|
31
44
|
post_install_message:
|
32
45
|
rdoc_options: []
|