Class: JpPrefecture::Prefecture
- Inherits:
-
Object
- Object
- JpPrefecture::Prefecture
- Defined in:
- lib/jp_prefecture/prefecture.rb,
lib/jp_prefecture/prefecture/finder.rb
Overview
都道府県のコードと名前を扱うクラス
Defined Under Namespace
Classes: Finder
Instance Attribute Summary collapse
-
#area ⇒ Object
Returns the value of attribute area.
-
#code ⇒ Object
Returns the value of attribute code.
-
#name ⇒ Object
Returns the value of attribute name.
-
#name_e ⇒ Object
Returns the value of attribute name_e.
-
#name_h ⇒ Object
Returns the value of attribute name_h.
-
#name_k ⇒ Object
Returns the value of attribute name_k.
-
#name_r ⇒ Object
Returns the value of attribute name_r.
-
#type ⇒ Object
Returns the value of attribute type.
-
#zips ⇒ Object
Returns the value of attribute zips.
Class Method Summary collapse
-
.all ⇒ Array<JpPrefecture::Prefecture>
すべての都道府県を取得.
-
.build_by_code(code) ⇒ JpPrefecture::Prefecture?
都道府県コードから都道府県インスタンスを作成.
-
.find(args) ⇒ JpPrefecture::Prefecture?
都道府県を検索.
-
.where(args) ⇒ Array<JpPrefecture::Prefecture>
都道府県を検索し、一致したすべての都道府県を返す.
Instance Attribute Details
#area ⇒ Object
Returns the value of attribute area.
11 12 13 |
# File 'lib/jp_prefecture/prefecture.rb', line 11 def area @area end |
#code ⇒ Object
Returns the value of attribute code.
11 12 13 |
# File 'lib/jp_prefecture/prefecture.rb', line 11 def code @code end |
#name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'lib/jp_prefecture/prefecture.rb', line 11 def name @name end |
#name_e ⇒ Object
Returns the value of attribute name_e.
11 12 13 |
# File 'lib/jp_prefecture/prefecture.rb', line 11 def name_e @name_e end |
#name_h ⇒ Object
Returns the value of attribute name_h.
11 12 13 |
# File 'lib/jp_prefecture/prefecture.rb', line 11 def name_h @name_h end |
#name_k ⇒ Object
Returns the value of attribute name_k.
11 12 13 |
# File 'lib/jp_prefecture/prefecture.rb', line 11 def name_k @name_k end |
#name_r ⇒ Object
Returns the value of attribute name_r.
11 12 13 |
# File 'lib/jp_prefecture/prefecture.rb', line 11 def name_r @name_r end |
#type ⇒ Object
Returns the value of attribute type.
11 12 13 |
# File 'lib/jp_prefecture/prefecture.rb', line 11 def type @type end |
#zips ⇒ Object
Returns the value of attribute zips.
11 12 13 |
# File 'lib/jp_prefecture/prefecture.rb', line 11 def zips @zips end |
Class Method Details
.all ⇒ Array<JpPrefecture::Prefecture>
すべての都道府県を取得
62 63 64 |
# File 'lib/jp_prefecture/prefecture.rb', line 62 def self.all Mapping.data.map { |code, _| build_by_code(code) } end |
.build_by_code(code) ⇒ JpPrefecture::Prefecture?
都道府県コードから都道府県インスタンスを作成
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/jp_prefecture/prefecture.rb', line 26 def self.build_by_code(code) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength result = Mapping.data[code] return unless result pref = new pref.code = code pref.name = result[:name] pref.name_e = result[:name_e]&.capitalize pref.name_r = result[:name_r]&.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&.slice(-1) when '都', '道', '府', '県' pref.name[-1] end pref end |
.find(args) ⇒ JpPrefecture::Prefecture?
都道府県を検索
文字列は前方一致で検索する
複数の都道府県に一致する場合は、都道府県コード順で最初の 1 件を返す。 一致したすべての都道府県が必要な場合は where を使用する
対応していない項目を指定した場合、または項目がちょうど 1 つでない場合は非推奨の警告を出力する。 jp_prefecture 2.0.0 では ArgumentError になる予定
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/jp_prefecture/prefecture.rb', line 109 def self.find(args) return if args.nil? case args when Integer, String JpPrefecture::Prefecture::Finder.new.find(field: nil, value: args) when Hash warn_unsupported_args(args) search_field = args.keys.first search_value = args.values.first JpPrefecture::Prefecture::Finder.new.find(field: search_field, value: search_value) end end |
.where(args) ⇒ Array<JpPrefecture::Prefecture>
都道府県を検索し、一致したすべての都道府県を返す
文字列は前方一致で検索する
141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/jp_prefecture/prefecture.rb', line 141 def self.where(args) unless args.is_a?(Hash) && args.size == 1 raise ArgumentError, "expected a Hash with exactly one key, got: #{inspect_args(args)}" end field, value = args.first unless Finder::NAME_FIELDS.include?(field) raise ArgumentError, "unsupported field: #{field.inspect} (supported: #{Finder::NAME_FIELDS.join(', ')})" end JpPrefecture::Prefecture::Finder.new.where(field: field, value: value) end |