Skip to content

Commit

Permalink
Add method to create prefecture from prefecture code
Browse files Browse the repository at this point in the history
  • Loading branch information
chocoby committed Feb 9, 2021
1 parent 9af2ffa commit ba93569
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 76 deletions.
16 changes: 0 additions & 16 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,20 @@
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 2
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 23

# Offense count: 9
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
# IgnoredMethods: refine
Metrics/BlockLength:
Max: 146

# Offense count: 1
# Configuration parameters: IgnoredMethods.
Metrics/CyclomaticComplexity:
Max: 8

# Offense count: 2
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Metrics/MethodLength:
Max: 18

# Offense count: 1
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
Metrics/ParameterLists:
Max: 6

# Offense count: 2
Style/Documentation:
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'lib/jp_prefecture.rb'
- 'lib/jp_prefecture/base.rb'
50 changes: 22 additions & 28 deletions lib/jp_prefecture/prefecture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,29 @@ module JpPrefecture
class Prefecture
attr_accessor :code, :name, :name_e, :name_h, :name_k, :zips, :area, :type

# 配列から都道府県クラスを生成
# 都道府県コードから都道府県インスタンスを作成
#
# @example
# # コード/名前から都道府県クラスを生成
# JpPrefecture::Prefecture.build(1, '北海道', 'Hokkaido')
# # 都道府県コードから都道府県インスタンスを生成
# JpPrefecture::Prefecture.build_by_code(1)
#
# @param pref [Integer] 都道府県コード
# @param name [String] 都道府県名
# @param name_e [String] 都道府県名(英語表記)
# @param optional name_h [String] 都道府県名(ひらがな表記)
# @param optional name_k [String] 都道府県名(カタカナ表記)
# @param optional area [String] 地方名
# @param zips [Array] 郵便番号の配列 (array of ranges, can be used in ARel, e.g. User.where(zip: prefecture.zips))
def self.build(code, name, name_e, name_h = nil, name_k = nil, area = nil)
# @param code [Integer] 都道府県コード
# @return [JpPrefecture::Prefecture] 都道府県インスタンス
# @return [nil] 都道府県が見つからない場合は nil
def self.build_by_code(code) # rubocop:disable Metrics/AbcSize
result = Mapping.data[code]
return unless result

pref = new

pref.code = code
pref.name = name
pref.name_e = name_e.capitalize
pref.name_h = name_h
pref.name_k = name_k
pref.zips = ZipMapping.data[code]
pref.area = area
pref.type =
pref.code = code
pref.name = result[:name]
pref.name_e = result[:name_e].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]
when '都', '道', '府', '県'
pref.name[-1]
Expand All @@ -41,7 +40,7 @@ def self.build(code, name, name_e, name_h = nil, name_k = nil, area = nil)
pref
end

# すべての都道府県クラスを返す
# すべての都道府県を取得
#
# @example
# # 都道府県の一覧を取得
Expand All @@ -53,14 +52,9 @@ def self.build(code, name, name_e, name_h = nil, name_k = nil, area = nil)
# # collection_select で選択肢を生成(英語表記)
# f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name_e
#
# @return [Array] 都道府県クラスの配列
# @return [Array<JpPrefecture::Prefecture>] 都道府県インスタンスの配列
def self.all
Mapping.data.map do |pref|
names = pref[1]
build(pref[0],
names[:name], names[:name_e],
names[:name_h], names[:name_k], names[:area])
end
Mapping.data.map { |code, _| build_by_code(code) }
end

# 都道府県を検索
Expand Down Expand Up @@ -94,7 +88,7 @@ def self.all
# @param args [Hash<Symbol, String>] :name 漢字表記/:name_e 英語表記/:name_h ひらがな表記/:name_k カタカナ表記
# @param args [Hash<Symbol, Integer>] :zip 郵便番号
# @param args [Hash<Symbol, (String, Integer)>] :all_fields マッピングに定義しているすべてのフィールドから検索
# @return [JpPrefecture::Prefecture] 都道府県が見つかった場合は都道府県クラス
# @return [JpPrefecture::Prefecture] 都道府県が見つかった場合は都道府県インスタンス
# @return [nil] 都道府県が見つからない場合は nil
def self.find(args)
return if args.nil?
Expand Down
14 changes: 2 additions & 12 deletions lib/jp_prefecture/prefecture/finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,11 @@ def initialize
#
# @param field [Symbol] 検索する項目。nil の場合は都道府県コードとして扱う
# @param value [String, Integer] 検索する内容
# @return [JpPrefecture::Prefecture] 都道府県が見つかった場合は都道府県クラス
# @return [JpPrefecture::Prefecture] 都道府県が見つかった場合は都道府県インスタンス
# @return [nil] 都道府県が見つからない場合は nil
def find(field:, value:)
code = find_code(field, value)
prefecture = @mapping[code]
return unless prefecture

JpPrefecture::Prefecture.build(
code,
prefecture[:name],
prefecture[:name_e],
prefecture[:name_h],
prefecture[:name_k],
prefecture[:area]
)
JpPrefecture::Prefecture.build_by_code(code)
end

private
Expand Down
38 changes: 18 additions & 20 deletions spec/prefecture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@
require 'spec_helper'

describe JpPrefecture::Prefecture do
describe '.build' do
let(:pref) { JpPrefecture::Prefecture.build(1, '北海道', 'Hokkaido', 'ほっかいどう', 'ホッカイドウ', '北海道') }
it { expect(pref.code).to eq 1 }
it { expect(pref.name).to eq '北海道' }
it { expect(pref.name_e).to eq 'Hokkaido' }
it { expect(pref.name_h).to eq 'ほっかいどう' }
it { expect(pref.name_k).to eq 'ホッカイドウ' }
it { expect(pref.zips).to eq [10_000..70_895, 400_000..996_509] }
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 [1_000_000..2_080_035] }
it { expect(nil_type_pref.area).to eq '関東' }
it { expect(nil_type_pref.type).to eq nil }
describe '.build_by_code' do
context '都道府県が見つかる' do
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 eq('北海道') }
it { expect(pref.name_e).to eq('Hokkaido') }
it { expect(pref.name_h).to eq('ほっかいどう') }
it { expect(pref.name_k).to eq('ホッカイドウ') }
it { expect(pref.zips).to eq(JpPrefecture::ZipMapping.data[pref.code]) }
it { expect(pref.area).to eq('北海道') }
it { expect(pref.type).to eq('道') }
end

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

describe '.all' do
Expand Down

0 comments on commit ba93569

Please sign in to comment.