uncharted 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+
3
+ require 'csv'
4
+ require 'uncharted/territory'
5
+
6
+ module Uncharted
7
+ Territory.load("#{File.dirname(__FILE__)}/data/territories-iso3166.csv")
8
+ end
@@ -32,15 +32,19 @@ module Uncharted
32
32
  class Territory
33
33
 
34
34
  attr_reader :abbr, :code, :country, :country_code, :type
35
+
36
+ TYPES = [:administration, :atol, :authority, :borough, :canton, :capital, :city, :collectivity, :commune, :council, :county,
37
+ :department, :dependency, :district, :emirate, :entity, :governorate, :island, :metropolis, :municipality,
38
+ :parish, :part, :prefecture, :province, :region, :republic, :sector, :state, :territory, :unit, :voivodship, :zone]
35
39
 
36
- def initialize(code, type, name)
40
+ def initialize(code, name, type)
37
41
  @code = code
38
- @type = type
39
42
  @name = name
40
43
  @country_code, @abbr = code.split('-')
41
44
  @country = Country.find(@country_code)
42
45
  Territory.data[code] = self
43
46
  @country.subdivisions << self if @country
47
+ @type = TYPES.detect {|t| /#{t}/ =~ type} || :territory
44
48
  end
45
49
 
46
50
  def name(options = {})
@@ -55,6 +59,10 @@ module Uncharted
55
59
  @abbr
56
60
  end
57
61
 
62
+ def inspect
63
+ "#{@code} - #{@name} (#{@type} - #{@country.inspect})"
64
+ end
65
+
58
66
  def self.find(object)
59
67
  territory = object && object.is_a?(Territory) ? object : data[object]
60
68
  end
@@ -79,7 +87,20 @@ module Uncharted
79
87
  find(object) || find_by_name(object)
80
88
  end
81
89
 
90
+ # Columns are expected in this order
91
+ # 1. Territory ISO 3166-2 code
92
+ # 2. Territory name
93
+ # 3. Territory level name (state, territory, parish, etc)
82
94
 
95
+ def self.load(path = "data/territories.csv", options = {:encoding => "UTF-8", :locale => 'pt-BR'})
96
+ CSV.foreach(path, :encoding => options[:encoding]) do |row|
97
+ code, name, type = row.collect {|c| c && c.strip}
98
+ type && type.downcase!
99
+ puts "Importing: #{code} :: #{name} :: #{type}"
100
+ new(code, name, type) if code =~ /^[A-Z]{2}-[A-Z0-9]{1,3}$/ && name && name.size > 0
101
+ end
102
+ Territory.data.each {|code, t| puts t.inspect}
103
+ end
104
+
83
105
  end
84
-
85
- end
106
+ end
@@ -1,4 +1,4 @@
1
1
 
2
2
  module Uncharted
3
- VERSION = '0.0.12'
3
+ VERSION = '0.0.13'
4
4
  end
@@ -27,6 +27,7 @@ class TestCountry < MiniTest::Unit::TestCase
27
27
 
28
28
  def test_territories
29
29
  df = Territory.find('BR-DF')
30
+ refute_nil df
30
31
  assert_equal @br, df.country
31
32
  assert_equal [df], @br.districts
32
33
  assert_equal 26, @br.states.count
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uncharted
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-23 00:00:00.000000000 Z
12
+ date: 2011-11-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n
16
- requirement: &2152785820 !ruby/object:Gem::Requirement
16
+ requirement: &2152901460 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2152785820
24
+ version_requirements: *2152901460
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: minitest
27
- requirement: &2152784260 !ruby/object:Gem::Requirement
27
+ requirement: &2152900420 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2152784260
35
+ version_requirements: *2152900420
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: purdytest
38
- requirement: &2152783100 !ruby/object:Gem::Requirement
38
+ requirement: &2152899860 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2152783100
46
+ version_requirements: *2152899860
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: simplecov
49
- requirement: &2152781840 !ruby/object:Gem::Requirement
49
+ requirement: &2152899140 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2152781840
57
+ version_requirements: *2152899140
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: simplecov-rcov
60
- requirement: &2152780420 !ruby/object:Gem::Requirement
60
+ requirement: &2152898200 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2152780420
68
+ version_requirements: *2152898200
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: yard
71
- requirement: &2152779780 !ruby/object:Gem::Requirement
71
+ requirement: &2152897260 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2152779780
79
+ version_requirements: *2152897260
80
80
  description: Provide ISO 3166 codes and english name for countries and their political
81
81
  divisions
82
82
  email:
@@ -95,8 +95,10 @@ files:
95
95
  - lib/uncharted.rb
96
96
  - lib/uncharted/config/br.rb
97
97
  - lib/uncharted/config/countries.rb
98
+ - lib/uncharted/config/data/territories-iso3166.csv
98
99
  - lib/uncharted/config/locales/en.yml
99
100
  - lib/uncharted/config/locales/pt-BR.yml
101
+ - lib/uncharted/config/territories.rb
100
102
  - lib/uncharted/country.rb
101
103
  - lib/uncharted/extensions/mongoid.rb
102
104
  - lib/uncharted/extensions/rails.rb