Class: JpPrefecture::Prefecture

Inherits:
Object
  • Object
show all
Defined in:
lib/jp_prefecture/prefecture.rb,
lib/jp_prefecture/prefecture/finder.rb

Overview

都道府県のコードと名前を扱うクラス

Defined Under Namespace

Classes: Finder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#areaObject

Returns the value of attribute area.



11
12
13
# File 'lib/jp_prefecture/prefecture.rb', line 11

def area
  @area
end

#codeObject

Returns the value of attribute code.



11
12
13
# File 'lib/jp_prefecture/prefecture.rb', line 11

def code
  @code
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/jp_prefecture/prefecture.rb', line 11

def name
  @name
end

#name_eObject

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_hObject

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_kObject

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_rObject

Returns the value of attribute name_r.



11
12
13
# File 'lib/jp_prefecture/prefecture.rb', line 11

def name_r
  @name_r
end

#typeObject

Returns the value of attribute type.



11
12
13
# File 'lib/jp_prefecture/prefecture.rb', line 11

def type
  @type
end

#zipsObject

Returns the value of attribute zips.



11
12
13
# File 'lib/jp_prefecture/prefecture.rb', line 11

def zips
  @zips
end

Class Method Details

.allArray<JpPrefecture::Prefecture>

すべての都道府県を取得

Examples:

# 都道府県の一覧を取得
JpPrefecture::Prefecture.all

# collection_select で選択肢を生成
f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name

# collection_select で選択肢を生成(英語表記)
f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name_e

Returns:



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?

都道府県コードから都道府県インスタンスを作成

Examples:

# 都道府県コードから都道府県インスタンスを生成
JpPrefecture::Prefecture.build_by_code(1)

Parameters:

  • code (Integer)

    都道府県コード

Returns:

  • (JpPrefecture::Prefecture)

    都道府県インスタンス

  • (nil)

    都道府県が見つからない場合は nil



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 になる予定

Examples:

# 都道府県コードを検索
JpPrefecture::Prefecture.find(1)
JpPrefecture::Prefecture.find(code: 1)

# 都道府県名を検索
JpPrefecture::Prefecture.find(name: '北海道')
JpPrefecture::Prefecture.find(name: '東京')

# 英語表記の都道府県名を検索
JpPrefecture::Prefecture.find(name_e: 'Hokkaido')
JpPrefecture::Prefecture.find(name_e: 'hokkaido')

# ローマ字表記の都道府県名を検索
JpPrefecture::Prefecture.find(name_r: 'Hokkaidō')
JpPrefecture::Prefecture.find(name_r: 'hokkaidō')

# ひらがな表記の都道府県名を検索
JpPrefecture::Prefecture.find(name_h: 'ほっかいどう')

# カタカナ表記の都道府県名を検索
JpPrefecture::Prefecture.find(name_k: 'ホッカイドウ')

# すべての項目を検索
JpPrefecture::Prefecture.find(all_fields: '')

Parameters:

  • args (Integer)

    都道府県コード

  • args (Hash<Symbol, Integer>)

    :code 都道府県コード

  • args (Hash<Symbol, String>)

    :name 漢字表記/:name_e 英語表記/:name_r ローマ字表記/:name_h ひらがな表記/:name_k カタカナ表記

  • args (Hash<Symbol, Integer>)

    :zip 郵便番号

  • args (Hash<Symbol, String>)

    :all_fields マッピングに定義しているすべてのフィールドから検索

Returns:

  • (JpPrefecture::Prefecture)

    都道府県が見つかった場合は都道府県インスタンス

  • (nil)

    都道府県が見つからない場合は nil



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>

都道府県を検索し、一致したすべての都道府県を返す

文字列は前方一致で検索する

Examples:

# 複数の都道府県に一致する検索
JpPrefecture::Prefecture.where(name: '')
# => [山形県, 山梨県, 山口県]

# 英語表記で検索
JpPrefecture::Prefecture.where(name_e: 'o')
# => [大阪府, 岡山県, 大分県, 沖縄県]

Parameters:

  • args (Hash<Symbol, String>)

    :name 漢字表記/:name_e 英語表記/:name_r ローマ字表記/:name_h ひらがな表記/:name_k カタカナ表記

Returns:

  • (Array<JpPrefecture::Prefecture>)

    一致した都道府県インスタンスの配列 (コード順)。一致しない場合は空配列

Raises:

  • (ArgumentError)

    対応していない項目、または項目が 1 つでない場合



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