-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from chocoby/issue24-search
文字列による検索は項目を指定して検索する
- Loading branch information
Showing
6 changed files
with
252 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'jp_prefecture/mapping' | ||
require 'jp_prefecture/zip_mapping' | ||
|
||
module JpPrefecture | ||
class Prefecture | ||
# 都道府県の検索を行うクラス | ||
class Finder | ||
def initialize | ||
@mapping = Mapping.data | ||
end | ||
|
||
# 指定した項目を検索 | ||
# | ||
# @param field [Symbol] 検索する項目。nil の場合は都道府県コードとして扱う | ||
# @param value [String, Integer] 検索する内容 | ||
# @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] | ||
) | ||
end | ||
|
||
private | ||
|
||
# @param field [Symbol] 検索する項目 | ||
# @param value [String, Integer] 検索する内容 | ||
# @return [Integer] 見つかった場合は都道府県コード | ||
# @return [nil] 見つからない場合は nil | ||
def find_code(field, value) | ||
return value.to_i if field.nil? | ||
|
||
case field | ||
when :all_fields | ||
find_code_by_name_from_all_fields(value) | ||
when :name, :name_h, :name_k, :name_e | ||
find_code_by_name(field, value) | ||
when :code | ||
value.to_i | ||
when :zip | ||
ZipMapping.code_for_zip(value.to_i) | ||
end | ||
end | ||
|
||
# すべての項目を前方一致で検索 | ||
def find_code_by_name_from_all_fields(value) | ||
return if value.nil? || value.empty? | ||
|
||
value = value.downcase | ||
|
||
@mapping.each do |m| | ||
m[1].each_value do |v| | ||
return m[0] if v.start_with?(value) | ||
end | ||
end | ||
end | ||
|
||
# 指定した項目を前方一致で検索 | ||
def find_code_by_name(field, value) | ||
return if value.nil? || value.empty? | ||
|
||
value = value.downcase | ||
|
||
@mapping.each do |m| | ||
return m[0] if m[1][field].start_with?(value) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe JpPrefecture::Prefecture::Finder do | ||
describe '#find' do | ||
shared_examples '都道府県が見つかる' do |field, value, expected_result| | ||
let(:result) { JpPrefecture::Prefecture::Finder.new.find(field: field, value: value) } | ||
it { expect(result.name).to eq(expected_result) } | ||
end | ||
|
||
shared_examples '都道府県が見つからない' do |field, value| | ||
let(:result) { JpPrefecture::Prefecture::Finder.new.find(field: field, value: value) } | ||
it { expect(result).to be_nil } | ||
end | ||
|
||
describe 'field に all_fields (すべての項目) を指定する' do | ||
# area の「東北」が最初にマッチする | ||
it_behaves_like '都道府県が見つかる', :all_fields, '東', '青森県' | ||
it_behaves_like '都道府県が見つからない', :all_fields, '饂飩' | ||
end | ||
|
||
describe 'field に name (漢字表記) を指定する' do | ||
it_behaves_like '都道府県が見つかる', :name, '北海道', '北海道' | ||
it_behaves_like '都道府県が見つかる', :name, '東京', '東京都' | ||
it_behaves_like '都道府県が見つかる', :name, '京都', '京都府' | ||
it_behaves_like '都道府県が見つからない', :name, '饂飩' | ||
end | ||
|
||
describe 'field に name_e (英語表記) を指定する' do | ||
it_behaves_like '都道府県が見つかる', :name_e, 'HOKKAIDO', '北海道' | ||
it_behaves_like '都道府県が見つかる', :name_e, 'Hokkaido', '北海道' | ||
it_behaves_like '都道府県が見つかる', :name_e, 'hokkaido', '北海道' | ||
it_behaves_like '都道府県が見つからない', :name_e, 'Udon' | ||
end | ||
|
||
describe 'field に name_h (ひらがな表記) を指定する' do | ||
it_behaves_like '都道府県が見つかる', :name_h, 'ほっかいどう', '北海道' | ||
it_behaves_like '都道府県が見つかる', :name_h, 'ほっかい', '北海道' | ||
it_behaves_like '都道府県が見つからない', :name_h, 'うどん' | ||
end | ||
|
||
describe 'field に name_k (カタカナ表記) を指定する' do | ||
it_behaves_like '都道府県が見つかる', :name_k, 'ホッカイドウ', '北海道' | ||
it_behaves_like '都道府県が見つかる', :name_k, 'ホッカイ', '北海道' | ||
it_behaves_like '都道府県が見つからない', :name_k, 'ウドン' | ||
end | ||
|
||
describe 'field に code (都道府県コード) を指定する' do | ||
it_behaves_like '都道府県が見つかる', :code, 1, '北海道' | ||
it_behaves_like '都道府県が見つかる', :code, '1', '北海道' | ||
it_behaves_like '都道府県が見つかる', :code, '01', '北海道' | ||
it_behaves_like '都道府県が見つからない', :code, 999 | ||
it_behaves_like '都道府県が見つからない', :code, '999' | ||
end | ||
|
||
describe 'field に zip (郵便番号) を指定する' do | ||
it_behaves_like '都道府県が見つかる', :zip, 10_000, '北海道' | ||
it_behaves_like '都道府県が見つかる', :zip, '010000', '北海道' | ||
it_behaves_like '都道府県が見つからない', :zip, 999_999 | ||
it_behaves_like '都道府県が見つからない', :zip, '999999' | ||
end | ||
|
||
describe 'field を指定しない' do | ||
it_behaves_like '都道府県が見つかる', nil, 1, '北海道' | ||
it_behaves_like '都道府県が見つかる', nil, '1', '北海道' | ||
it_behaves_like '都道府県が見つかる', nil, '01', '北海道' | ||
it_behaves_like '都道府県が見つからない', nil, 999 | ||
it_behaves_like '都道府県が見つからない', nil, '999' | ||
end | ||
end | ||
end |
Oops, something went wrong.