Skip to content

Commit

Permalink
Merge pull request #23 from gazayas/master
Browse files Browse the repository at this point in the history
add type to prefecture
  • Loading branch information
chocoby authored Jan 10, 2017
2 parents 43fac87 + e6c1754 commit f2a5a35
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pref.name_k
# => "トウキョウト"
pref.area
# => "関東"
pref.type
# => "都"
```

以下のように書くことも可能です:
Expand Down
9 changes: 8 additions & 1 deletion lib/jp_prefecture/prefecture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module JpPrefecture
# 都道府県のコードと名前を扱うクラス
class Prefecture

attr_accessor :code, :name, :name_e, :name_h, :name_k, :zips, :area
attr_accessor :code, :name, :name_e, :name_h, :name_k, :zips, :area, :type

# 配列から都道府県クラスを生成
#
Expand All @@ -31,6 +31,13 @@ def self.build(code, name, name_e, name_h = nil, name_k = nil, area = nil)
pref.name_k = name_k
pref.zips = ZipMapping.data[code]
pref.area = area
pref.type =
case pref.name[-1]
when "都", "道", "府", "県"
pref.name[-1]
else
nil
end

pref
end
Expand Down
12 changes: 12 additions & 0 deletions spec/prefecture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
it { expect(pref.name_k).to eq 'ホッカイドウ' }
it { expect(pref.zips).to eq [10000..70895, 400000..996509] }
it { expect(pref.area).to eq '北海道' }
it { expect(pref.type).to eq '道' }

let(:nil_type_pref) { JpPrefecture::Prefecture.build(13, '東京', 'Tokyo', 'とうきょう', 'トウキョウ', '関東') }
it { expect(nil_type_pref.code).to eq 13 }
it { expect(nil_type_pref.name).to eq '東京' }
it { expect(nil_type_pref.name_e).to eq 'Tokyo' }
it { expect(nil_type_pref.name_h).to eq 'とうきょう' }
it { expect(nil_type_pref.name_k).to eq 'トウキョウ' }
it { expect(nil_type_pref.zips).to eq [1000000..2080035] }
it { expect(nil_type_pref.area).to eq '関東' }
it { expect(nil_type_pref.type).to eq nil }
end

describe '.find' do
Expand All @@ -24,6 +35,7 @@
it { expect(pref.name_k).to eq 'ホッカイドウ' }
it { expect(pref.zips).to eq [10000..70895, 400000..996509] }
it { expect(pref.area).to eq '北海道' }
it { expect(pref.type).to eq '道' }
end

shared_examples '都道府県が見つからない' do |arg|
Expand Down

0 comments on commit f2a5a35

Please sign in to comment.