Permalink
Browse files
Move territory methods to territory.rb
- Loading branch information...
Showing
with
34 additions
and
21 deletions.
-
+0
−2
lib/uncharted.rb
-
+2
−15
lib/uncharted/country.rb
-
+26
−1
lib/uncharted/territory.rb
-
+6
−3
test/country_test.rb
|
@@ -3,8 +3,6 @@ |
|
|
require 'uncharted/data/countries'
|
|
|
require 'uncharted/data/br'
|
|
|
|
|
|
-Country = Uncharted::Country
|
|
|
-
|
|
|
module Uncharted
|
|
|
|
|
|
end
|
|
|
|
@@ -11,21 +11,7 @@ def initialize(alpha2, alpha3, name) |
|
|
Country.countries[alpha2] = self
|
|
|
end
|
|
|
|
|
|
- def subdivisions
|
|
|
- @subdivisions ||= {}
|
|
|
- end
|
|
|
-
|
|
|
- def territories
|
|
|
- @territories ||= subdivisions.values.flatten
|
|
|
- end
|
|
|
-
|
|
|
- def states
|
|
|
- @states ||= subdivisions[:state] || {}
|
|
|
- end
|
|
|
-
|
|
|
- def districts
|
|
|
- @districts ||= subdivisions[:district] || {}
|
|
|
- end
|
|
|
+
|
|
|
|
|
|
def to_s
|
|
|
@alpha2
|
|
@@ -38,6 +24,7 @@ def self.count |
|
|
def self.find(code)
|
|
|
countries[code]
|
|
|
end
|
|
|
+
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
@@ -1,7 +1,30 @@ |
|
|
-
|
|
|
require 'uncharted/country'
|
|
|
|
|
|
module Uncharted
|
|
|
+
|
|
|
+ class Country
|
|
|
+
|
|
|
+ def subdivisions
|
|
|
+ @subdivisions ||= {}
|
|
|
+ end
|
|
|
+
|
|
|
+ def territories
|
|
|
+ subdivisions[:territory] || []
|
|
|
+ end
|
|
|
+
|
|
|
+ def states
|
|
|
+ subdivisions[:state] || []
|
|
|
+ end
|
|
|
+
|
|
|
+ def districts
|
|
|
+ subdivisions[:district] || []
|
|
|
+ end
|
|
|
+
|
|
|
+ def self.subdivisions
|
|
|
+ @subdivisions ||= {}
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
|
|
|
class Territory
|
|
|
|
|
@@ -13,6 +36,7 @@ def initialize(code, division, name) |
|
|
@name = name
|
|
|
@country_code, @abbr = code.split('-')
|
|
|
@country = Country.find(@country_code)
|
|
|
+ Country.subdivisions[code] = self
|
|
|
@country.subdivisions[@division] ||= []
|
|
|
@country.subdivisions[@division] << self
|
|
|
end
|
|
@@ -23,4 +47,5 @@ def to_s |
|
|
|
|
|
end
|
|
|
|
|
|
+
|
|
|
end
|
|
@@ -6,7 +6,7 @@ |
|
|
class TestCountry < MiniTest::Unit::TestCase
|
|
|
|
|
|
def setup
|
|
|
- @br = Country.find('BR')
|
|
|
+ @br = Uncharted::Country.find('BR')
|
|
|
end
|
|
|
|
|
|
def test_country_lookup
|
|
@@ -21,9 +21,12 @@ def test_to_s |
|
|
end
|
|
|
|
|
|
def test_territories
|
|
|
- assert_equal 27, @br.territories.count
|
|
|
- assert_equal 26, @br.states.count
|
|
|
assert_equal 1, @br.districts.count
|
|
|
+ assert_equal 26, @br.states.count
|
|
|
+ assert @br.territories.empty?
|
|
|
+
|
|
|
+ assert_equal [:district, :state], @br.subdivisions.keys.sort
|
|
|
+ assert_equal 'Paraná', Uncharted::Country.subdivisions['BR-PR'].name
|
|
|
end
|
|
|
|
|
|
end
|
0 comments on commit
6185350