Class: JpAddressComplement::Repositories::CsvPostalCodeRepository
- Inherits:
-
PostalCodeRepository
- Object
- PostalCodeRepository
- JpAddressComplement::Repositories::CsvPostalCodeRepository
- Includes:
- KenAll
- Defined in:
- lib/jp_address_complement/repositories/csv_postal_code_repository.rb
Overview
UTF-8 版 KEN_ALL(utf_ken_all.csv)形式の CSV ファイルを直接読み込んで検索を行う Repository 実装
- ActiveRecord や DB に依存せず、純粋に CSV から AddressRecord を構築する
- 初回アクセス時に CSV 全体を読み込み、インメモリでインデックスを構築する(2回目以降はメモリ検索のみ)
- CSV は日本郵便公式の KEN_ALL.CSV(UTF-8 版 utf_ken_all.csv)と同じ列構成を前提とする
典型的な利用例:
require 'jp_address_complement/repositories/csv_postal_code_repository'
JpAddressComplement.configure do |c|
c.repository = JpAddressComplement::Repositories::CsvPostalCodeRepository.new('/path/to/utf_ken_all.csv')
end
Constant Summary
Constants included from KenAll
KenAll::COL_CITY, KenAll::COL_HAS_ALIAS, KenAll::COL_IS_LARGE_OFFICE, KenAll::COL_IS_PARTIAL, KenAll::COL_KANA_CITY, KenAll::COL_KANA_PREF, KenAll::COL_KANA_TOWN, KenAll::COL_POSTAL_CODE, KenAll::COL_PREF, KenAll::COL_PREF_CODE, KenAll::COL_TOWN
Instance Method Summary collapse
- #find_by_code(code) ⇒ Object
- #find_by_prefix(prefix) ⇒ Object
- #find_postal_codes_by_address(pref:, city:, town: nil) ⇒ Object
-
#initialize(csv_path) ⇒ CsvPostalCodeRepository
constructor
A new instance of CsvPostalCodeRepository.
Constructor Details
#initialize(csv_path) ⇒ CsvPostalCodeRepository
Returns a new instance of CsvPostalCodeRepository.
31 32 33 34 35 36 37 |
# File 'lib/jp_address_complement/repositories/csv_postal_code_repository.rb', line 31 def initialize(csv_path) super() @csv_path = csv_path @loaded = false @records = [] # : Array[AddressRecord] @by_code = Hash.new { |h, k| h[k] = [] } # : Hash[String, Array[AddressRecord]] end |
Instance Method Details
#find_by_code(code) ⇒ Object
40 41 42 43 |
# File 'lib/jp_address_complement/repositories/csv_postal_code_repository.rb', line 40 def find_by_code(code) ensure_loaded @by_code[code] || [] end |
#find_by_prefix(prefix) ⇒ Object
46 47 48 49 |
# File 'lib/jp_address_complement/repositories/csv_postal_code_repository.rb', line 46 def find_by_prefix(prefix) ensure_loaded @records.select { |r| r.postal_code.start_with?(prefix) } end |
#find_postal_codes_by_address(pref:, city:, town: nil) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/jp_address_complement/repositories/csv_postal_code_repository.rb', line 52 def find_postal_codes_by_address(pref:, city:, town: nil) ensure_loaded return [] if blank?(pref) || blank?(city) town_query = town&.to_s&.strip pref_s = pref.to_s city_s = city.to_s @records.select { |record| address_match?(record, pref_s, city_s, town_query) } end |