Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add type to prefecture #23

Merged
merged 3 commits into from
Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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