Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleanup some of the rewrite

  • Loading branch information...
commit 935161f8ad08f04ee318fbdf21d6486b0ec41c06 1 parent e548145
@jim authored
View
1  .gitignore
@@ -5,3 +5,4 @@ tmp
.DS_Store
.bundle/*
Gemfile.lock
+.rvmrc
View
2  lib/carmen.rb
@@ -18,5 +18,5 @@ class << self
attr_accessor :data_path
end
- self.data_path = Pathname.new(File.expand_path('../../data', __FILE__))
+ self.data_path = Pathname.new(File.expand_path('../../iso_data', __FILE__))
end
View
2  lib/carmen/country.rb
@@ -8,6 +8,6 @@ module Country
extend Querying
extend SingleForwardable
- def_delegator 'Carmen::World', :subregions
+ def_delegator 'Carmen::World', :subregions
end
end
View
10 lib/carmen/querying.rb
@@ -7,8 +7,16 @@ module Querying
#
# Returns a region with the supplied code, or nil ir none is found.
def coded(code)
+ attribute = case code.to_s.size
+ when 2
+ :alpha_2_code
+ when 3
+ :alpha_3_code
+ else
+ fail "coded only accepts 2 or 3 character codes"
+ end
subregions.find do |region|
- region.code == code
+ region.send(attribute) == code
end
end
View
31 lib/carmen/region.rb
@@ -9,33 +9,34 @@ class Region
attr_reader :type
attr_reader :name
- attr_reader :code
+ attr_reader :alpha_2_code
+ attr_reader :alpha_3_code
attr_reader :parent
- def initialize(type, name, code, parent=nil)
- @type = type
- @name = name
- @code = code
- @parent = parent
+ def initialize(data={})
+ @type = data['type']
+ @name = data['name']
+ @alpha_2_code = data['alpha_2_code']
+ @alpha_3_code = data['alpha_3_code']
+ @parent = data['parent']
end
def subregions
@subregions ||= load_subregions
end
- def self.load_from_file(path)
+ def self.load_from_file(path, parent=nil)
regions = YAML.load_file(path).map do |data|
- self.new(:region, *data)
+ self.new(data.merge('parent' => parent))
end
RegionCollection.new(regions)
end
- private
-
def subregion_data_path
if @parent
- @parent.subregion_data_path.gsub('.yml', "/#{code}.yml")
+ path = @parent.subregion_data_path.dirname + "regions/#{alpha_2_code.downcase}.yml"
+ Pathname.new(path)
else
Carmen::data_path + 'world.yml'
end
@@ -45,9 +46,15 @@ def subregions?
File.exist?(subregion_data_path)
end
+ def inspect
+ "<##{self.class} type=#{type} subregions?=#{subregions?}>"
+ end
+
+ private
+
def load_subregions
if subregions?
- self.class.load_from_file(subregion_data_path)
+ self.class.load_from_file(subregion_data_path, self)
end
end
end
View
3  lib/carmen/world.rb
@@ -2,5 +2,6 @@
require 'carmen/region'
module Carmen
- World = Region.new(:world, 'World', nil)
+ World = Region.new('type' => :world,
+ 'name' => 'World')
end
View
3  script/update_data.rb
@@ -40,7 +40,8 @@
'numeric_code' => country['numeric_code'],
'common_name' => country['common_name'],
'name' => country['name'],
- 'official_name' => country['official_name']
+ 'official_name' => country['official_name'],
+ 'type' => 'country'
}
end
View
12 spec/carmen/country_spec.rb
@@ -7,14 +7,20 @@
us.instance_of?(Carmen::Region).must_equal true
end
+ it "provides an API for finding countries by code" do
+ us = Carmen::Country.coded('US')
+ us.instance_of?(Carmen::Region).must_equal true
+ end
+
describe "with a country" do
before do
@us = Carmen::Country.coded('US')
end
- it "provides an API for finding countries by code" do
- @us.instance_of?(Carmen::Region).must_equal true
+ it 'has a subregion data path' do
+ expected = Carmen.data_path + 'regions/us.yml'
+ @us.subregion_data_path.must_equal expected
end
it "has subregions" do
@@ -22,7 +28,7 @@
end
it "is of type :country" do
- @us.type.must_equal :country
+ @us.type.must_equal 'country'
end
end
View
2  spec/carmen/world_spec.rb
@@ -7,7 +7,7 @@
end
it 'has 200 subregions' do
- Carmen::World.subregions.size.must_equal(252)
+ Carmen::World.subregions.size.must_equal(249)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.