Skip to content
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: 1 addition & 1 deletion lib/jp_prefecture/prefecture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def self.all
# @param args [Hash<Symbol, Integer>] :code 都道府県コード
# @param args [Hash<Symbol, String>] :name 漢字表記/:name_e 英語表記/:name_r ローマ字表記/:name_h ひらがな表記/:name_k カタカナ表記
# @param args [Hash<Symbol, Integer>] :zip 郵便番号
# @param args [Hash<Symbol, (String, Integer)>] :all_fields マッピングに定義しているすべてのフィールドから検索
# @param args [Hash<Symbol, String>] :all_fields マッピングに定義しているすべてのフィールドから検索
# @return [JpPrefecture::Prefecture] 都道府県が見つかった場合は都道府県インスタンス
# @return [nil] 都道府県が見つからない場合は nil
def self.find(args)
Expand Down
10 changes: 4 additions & 6 deletions lib/jp_prefecture/prefecture/finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ def find_code(field, value)

case field
when :all_fields
find_code_by_name_from_all_fields(value)
find_code_by_name_from_all_fields(value.to_s)
when :name, :name_h, :name_k, :name_e, :name_r
find_code_by_name(field, value)
find_code_by_name(field, value.to_s)
when :code
value.to_i
when :zip
Expand All @@ -45,9 +45,8 @@ def find_code(field, value)

# すべての項目を前方一致で検索
def find_code_by_name_from_all_fields(value)
return if value.nil? || value.empty?

value = value.downcase
return if value.empty?

@mapping.each do |m|
m[1].each_value do |v|
Expand All @@ -58,9 +57,8 @@ def find_code_by_name_from_all_fields(value)

# 指定した項目を前方一致で検索
def find_code_by_name(field, value)
return if value.nil? || value.empty?

value = value.downcase
return if value.empty?

@mapping.each do |m|
return m[0] if m[1][field].start_with?(value)
Expand Down
10 changes: 10 additions & 0 deletions spec/prefecture/finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@
it_behaves_like '都道府県が見つからない', :zip, '999999'
end

describe 'value に String 以外を指定する' do
it_behaves_like '都道府県が見つかる', :all_fields, :東, '青森県'
it_behaves_like '都道府県が見つかる', :name, :北海道, '北海道'
it_behaves_like '都道府県が見つかる', :name_e, :hokkaido, '北海道'
it_behaves_like '都道府県が見つからない', :all_fields, 13
it_behaves_like '都道府県が見つからない', :name, 1
it_behaves_like '都道府県が見つからない', :all_fields, nil
it_behaves_like '都道府県が見つからない', :name, nil
end

describe 'field を指定しない' do
it_behaves_like '都道府県が見つかる', nil, 1, '北海道'
it_behaves_like '都道府県が見つかる', nil, '1', '北海道'
Expand Down