Class: JpPrefecture::Prefecture::Finder
- Inherits:
-
Object
- Object
- JpPrefecture::Prefecture::Finder
- Defined in:
- lib/jp_prefecture/prefecture/finder.rb
Overview
都道府県の検索を行うクラス
Constant Summary collapse
- NAME_FIELDS =
名前を前方一致で検索できる項目
%i[name name_e name_r name_h name_k].freeze
- FIELDS =
find で検索できる項目
(NAME_FIELDS + %i[code zip all_fields]).freeze
Instance Method Summary collapse
-
#find(field:, value:) ⇒ JpPrefecture::Prefecture?
指定した項目を検索.
-
#initialize ⇒ Finder
constructor
A new instance of Finder.
-
#where(field:, value:) ⇒ Array<JpPrefecture::Prefecture>
指定した項目を前方一致で検索し、一致したすべての都道府県を返す.
Constructor Details
Instance Method Details
#find(field:, value:) ⇒ JpPrefecture::Prefecture?
指定した項目を検索
26 27 28 29 |
# File 'lib/jp_prefecture/prefecture/finder.rb', line 26 def find(field:, value:) code = find_code(field, value) JpPrefecture::Prefecture.build_by_code(code) end |
#where(field:, value:) ⇒ Array<JpPrefecture::Prefecture>
指定した項目を前方一致で検索し、一致したすべての都道府県を返す
36 37 38 39 40 41 42 43 44 |
# File 'lib/jp_prefecture/prefecture/finder.rb', line 36 def where(field:, value:) return [] unless NAME_FIELDS.include?(field) value = value.to_s.downcase return [] if value.empty? @mapping.select { |_code, names| names[field].start_with?(value) } .map { |code, _names| JpPrefecture::Prefecture.build_by_code(code) } end |