Skip to content

Commit

Permalink
Merge pull request #59 from chocoby/fix-custom-mappings
Browse files Browse the repository at this point in the history
マッピングに項目が存在しない場合にエラーが発生する問題を修正
  • Loading branch information
chocoby authored Mar 25, 2022
2 parents 96dab29 + a25d1ca commit f6cfd5a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/jp_prefecture/prefecture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ def self.build_by_code(code) # rubocop:disable Metrics/AbcSize

pref.code = code
pref.name = result[:name]
pref.name_e = result[:name_e].capitalize
pref.name_r = result[:name_r].capitalize
pref.name_e = result[:name_e].try(:capitalize)
pref.name_r = result[:name_r].try(:capitalize)
pref.name_h = result[:name_h]
pref.name_k = result[:name_k]
pref.zips = ZipMapping.data[code]
pref.area = result[:area]
pref.type =
case pref.name[-1]
case pref.name.try(:slice, -1)
when '都', '道', '府', '県'
pref.name[-1]
end
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/prefecture_without_values.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# 中身が空白
---
1:
:dummy:
4 changes: 4 additions & 0 deletions spec/fixtures/zip_without_values.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# 中身が空白
---
dummy:
-
29 changes: 29 additions & 0 deletions spec/prefecture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,35 @@
it { expect(pref.type).to eq('道') }
end

context 'カスタマイズしたマッピング情報に定義されていない項目が存在する' do
before do
JpPrefecture.setup do |config|
config.mapping_data = YAML.load_file(File.join(File.dirname(__FILE__),
'fixtures/prefecture_without_values.yml'))
config.zip_mapping_data = YAML.load_file(File.join(File.dirname(__FILE__), 'fixtures/zip_without_values.yml'))
end
end

after do
JpPrefecture.setup do |config|
config.mapping_data = nil
config.zip_mapping_data = nil
end
end

let(:pref) { JpPrefecture::Prefecture.build_by_code(1) }
it { expect(pref).to be_an_instance_of(JpPrefecture::Prefecture) }
it { expect(pref.code).to eq(1) }
it { expect(pref.name).to be_nil }
it { expect(pref.name_e).to be_nil }
it { expect(pref.name_r).to be_nil }
it { expect(pref.name_h).to be_nil }
it { expect(pref.name_k).to be_nil }
it { expect(pref.zips).to be_nil }
it { expect(pref.area).to be_nil }
it { expect(pref.type).to be_nil }
end

context '都道府県が見つからない' do
let(:pref) { JpPrefecture::Prefecture.build_by_code(99) }
it { expect(pref).to be_nil }
Expand Down

0 comments on commit f6cfd5a

Please sign in to comment.