Class: Basho::Prefecture

Inherits:
Data
  • Object
show all
Defined in:
lib/basho/prefecture.rb

Overview

都道府県を表すイミュータブルなデータクラス。

DBバックエンドが有効な場合、クラスメソッドは自動的に DB::Prefecture 経由で検索する。

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#capital_codeObject (readonly)

Returns the value of attribute capital_code

Returns:

  • (Object)

    the current value of capital_code



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/basho/prefecture.rb', line 24

Prefecture = ::Data.define(:code, :name, :name_en, :name_kana, :name_hiragana, :region_name, :type, :capital_code) do
  # 所属する地方を返す。
  #
  # @return [Region]
  def region
    Region.find(region_name)
  end

  # 所属する市区町村の一覧を返す。
  #
  # @return [Array<City>, Array<DB::City>]
  def cities
    City.where(prefecture_code: code)
  end

  # 県庁所在地を返す。
  #
  # @return [City, DB::City, nil]
  def capital
    City.find(capital_code)
  end

  class << self
    # 全47都道府県を返す。
    #
    # @return [Array<Prefecture>, Array<DB::Prefecture>]
    def all
      return DB::Prefecture.all.to_a if Basho.db?

      @all ||= Data::Loader.prefectures.map { |data| new(**data) }.freeze
    end

    # 都道府県を検索する。
    #
    # @overload find(code)
    #   @param code [Integer] 都道府県コード
    # @overload find(name:)
    #   @param name [String] 日本語名(例: "東京都")
    # @overload find(name_en:)
    #   @param name_en [String] 英語名(例: "Tokyo")
    # @return [Prefecture, DB::Prefecture, nil]
    def find(code = nil, **options)
      attrs = code.nil? ? options : { code: code }
      return if attrs.empty?

      key, value = attrs.first
      return DB::Prefecture.find_by(key => value) if Basho.db?

      all.find { |pref| pref.public_send(key) == value }
    end

    # 都道府県を地方名で絞り込む。引数なしで全件返す。
    #
    # @param region [String, nil] 地方名(例: "関東")
    # @return [Array<Prefecture>, Array<DB::Prefecture>]
    def where(region: nil)
      return all unless region
      return DB::Prefecture.where(region_name: region).to_a if Basho.db?

      all.select { |pref| pref.region_name == region }
    end

    # メモリキャッシュをクリアする。
    #
    # @return [void]
    # @api private
    def reset_cache!
      remove_instance_variable(:@all) if defined?(@all)
    end
  end
end

#codeObject (readonly)

Returns the value of attribute code

Returns:

  • (Object)

    the current value of code



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/basho/prefecture.rb', line 24

Prefecture = ::Data.define(:code, :name, :name_en, :name_kana, :name_hiragana, :region_name, :type, :capital_code) do
  # 所属する地方を返す。
  #
  # @return [Region]
  def region
    Region.find(region_name)
  end

  # 所属する市区町村の一覧を返す。
  #
  # @return [Array<City>, Array<DB::City>]
  def cities
    City.where(prefecture_code: code)
  end

  # 県庁所在地を返す。
  #
  # @return [City, DB::City, nil]
  def capital
    City.find(capital_code)
  end

  class << self
    # 全47都道府県を返す。
    #
    # @return [Array<Prefecture>, Array<DB::Prefecture>]
    def all
      return DB::Prefecture.all.to_a if Basho.db?

      @all ||= Data::Loader.prefectures.map { |data| new(**data) }.freeze
    end

    # 都道府県を検索する。
    #
    # @overload find(code)
    #   @param code [Integer] 都道府県コード
    # @overload find(name:)
    #   @param name [String] 日本語名(例: "東京都")
    # @overload find(name_en:)
    #   @param name_en [String] 英語名(例: "Tokyo")
    # @return [Prefecture, DB::Prefecture, nil]
    def find(code = nil, **options)
      attrs = code.nil? ? options : { code: code }
      return if attrs.empty?

      key, value = attrs.first
      return DB::Prefecture.find_by(key => value) if Basho.db?

      all.find { |pref| pref.public_send(key) == value }
    end

    # 都道府県を地方名で絞り込む。引数なしで全件返す。
    #
    # @param region [String, nil] 地方名(例: "関東")
    # @return [Array<Prefecture>, Array<DB::Prefecture>]
    def where(region: nil)
      return all unless region
      return DB::Prefecture.where(region_name: region).to_a if Basho.db?

      all.select { |pref| pref.region_name == region }
    end

    # メモリキャッシュをクリアする。
    #
    # @return [void]
    # @api private
    def reset_cache!
      remove_instance_variable(:@all) if defined?(@all)
    end
  end
end

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/basho/prefecture.rb', line 24

Prefecture = ::Data.define(:code, :name, :name_en, :name_kana, :name_hiragana, :region_name, :type, :capital_code) do
  # 所属する地方を返す。
  #
  # @return [Region]
  def region
    Region.find(region_name)
  end

  # 所属する市区町村の一覧を返す。
  #
  # @return [Array<City>, Array<DB::City>]
  def cities
    City.where(prefecture_code: code)
  end

  # 県庁所在地を返す。
  #
  # @return [City, DB::City, nil]
  def capital
    City.find(capital_code)
  end

  class << self
    # 全47都道府県を返す。
    #
    # @return [Array<Prefecture>, Array<DB::Prefecture>]
    def all
      return DB::Prefecture.all.to_a if Basho.db?

      @all ||= Data::Loader.prefectures.map { |data| new(**data) }.freeze
    end

    # 都道府県を検索する。
    #
    # @overload find(code)
    #   @param code [Integer] 都道府県コード
    # @overload find(name:)
    #   @param name [String] 日本語名(例: "東京都")
    # @overload find(name_en:)
    #   @param name_en [String] 英語名(例: "Tokyo")
    # @return [Prefecture, DB::Prefecture, nil]
    def find(code = nil, **options)
      attrs = code.nil? ? options : { code: code }
      return if attrs.empty?

      key, value = attrs.first
      return DB::Prefecture.find_by(key => value) if Basho.db?

      all.find { |pref| pref.public_send(key) == value }
    end

    # 都道府県を地方名で絞り込む。引数なしで全件返す。
    #
    # @param region [String, nil] 地方名(例: "関東")
    # @return [Array<Prefecture>, Array<DB::Prefecture>]
    def where(region: nil)
      return all unless region
      return DB::Prefecture.where(region_name: region).to_a if Basho.db?

      all.select { |pref| pref.region_name == region }
    end

    # メモリキャッシュをクリアする。
    #
    # @return [void]
    # @api private
    def reset_cache!
      remove_instance_variable(:@all) if defined?(@all)
    end
  end
end

#name_enObject (readonly)

Returns the value of attribute name_en

Returns:

  • (Object)

    the current value of name_en



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/basho/prefecture.rb', line 24

Prefecture = ::Data.define(:code, :name, :name_en, :name_kana, :name_hiragana, :region_name, :type, :capital_code) do
  # 所属する地方を返す。
  #
  # @return [Region]
  def region
    Region.find(region_name)
  end

  # 所属する市区町村の一覧を返す。
  #
  # @return [Array<City>, Array<DB::City>]
  def cities
    City.where(prefecture_code: code)
  end

  # 県庁所在地を返す。
  #
  # @return [City, DB::City, nil]
  def capital
    City.find(capital_code)
  end

  class << self
    # 全47都道府県を返す。
    #
    # @return [Array<Prefecture>, Array<DB::Prefecture>]
    def all
      return DB::Prefecture.all.to_a if Basho.db?

      @all ||= Data::Loader.prefectures.map { |data| new(**data) }.freeze
    end

    # 都道府県を検索する。
    #
    # @overload find(code)
    #   @param code [Integer] 都道府県コード
    # @overload find(name:)
    #   @param name [String] 日本語名(例: "東京都")
    # @overload find(name_en:)
    #   @param name_en [String] 英語名(例: "Tokyo")
    # @return [Prefecture, DB::Prefecture, nil]
    def find(code = nil, **options)
      attrs = code.nil? ? options : { code: code }
      return if attrs.empty?

      key, value = attrs.first
      return DB::Prefecture.find_by(key => value) if Basho.db?

      all.find { |pref| pref.public_send(key) == value }
    end

    # 都道府県を地方名で絞り込む。引数なしで全件返す。
    #
    # @param region [String, nil] 地方名(例: "関東")
    # @return [Array<Prefecture>, Array<DB::Prefecture>]
    def where(region: nil)
      return all unless region
      return DB::Prefecture.where(region_name: region).to_a if Basho.db?

      all.select { |pref| pref.region_name == region }
    end

    # メモリキャッシュをクリアする。
    #
    # @return [void]
    # @api private
    def reset_cache!
      remove_instance_variable(:@all) if defined?(@all)
    end
  end
end

#name_hiraganaObject (readonly)

Returns the value of attribute name_hiragana

Returns:

  • (Object)

    the current value of name_hiragana



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/basho/prefecture.rb', line 24

Prefecture = ::Data.define(:code, :name, :name_en, :name_kana, :name_hiragana, :region_name, :type, :capital_code) do
  # 所属する地方を返す。
  #
  # @return [Region]
  def region
    Region.find(region_name)
  end

  # 所属する市区町村の一覧を返す。
  #
  # @return [Array<City>, Array<DB::City>]
  def cities
    City.where(prefecture_code: code)
  end

  # 県庁所在地を返す。
  #
  # @return [City, DB::City, nil]
  def capital
    City.find(capital_code)
  end

  class << self
    # 全47都道府県を返す。
    #
    # @return [Array<Prefecture>, Array<DB::Prefecture>]
    def all
      return DB::Prefecture.all.to_a if Basho.db?

      @all ||= Data::Loader.prefectures.map { |data| new(**data) }.freeze
    end

    # 都道府県を検索する。
    #
    # @overload find(code)
    #   @param code [Integer] 都道府県コード
    # @overload find(name:)
    #   @param name [String] 日本語名(例: "東京都")
    # @overload find(name_en:)
    #   @param name_en [String] 英語名(例: "Tokyo")
    # @return [Prefecture, DB::Prefecture, nil]
    def find(code = nil, **options)
      attrs = code.nil? ? options : { code: code }
      return if attrs.empty?

      key, value = attrs.first
      return DB::Prefecture.find_by(key => value) if Basho.db?

      all.find { |pref| pref.public_send(key) == value }
    end

    # 都道府県を地方名で絞り込む。引数なしで全件返す。
    #
    # @param region [String, nil] 地方名(例: "関東")
    # @return [Array<Prefecture>, Array<DB::Prefecture>]
    def where(region: nil)
      return all unless region
      return DB::Prefecture.where(region_name: region).to_a if Basho.db?

      all.select { |pref| pref.region_name == region }
    end

    # メモリキャッシュをクリアする。
    #
    # @return [void]
    # @api private
    def reset_cache!
      remove_instance_variable(:@all) if defined?(@all)
    end
  end
end

#name_kanaObject (readonly)

Returns the value of attribute name_kana

Returns:

  • (Object)

    the current value of name_kana



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/basho/prefecture.rb', line 24

Prefecture = ::Data.define(:code, :name, :name_en, :name_kana, :name_hiragana, :region_name, :type, :capital_code) do
  # 所属する地方を返す。
  #
  # @return [Region]
  def region
    Region.find(region_name)
  end

  # 所属する市区町村の一覧を返す。
  #
  # @return [Array<City>, Array<DB::City>]
  def cities
    City.where(prefecture_code: code)
  end

  # 県庁所在地を返す。
  #
  # @return [City, DB::City, nil]
  def capital
    City.find(capital_code)
  end

  class << self
    # 全47都道府県を返す。
    #
    # @return [Array<Prefecture>, Array<DB::Prefecture>]
    def all
      return DB::Prefecture.all.to_a if Basho.db?

      @all ||= Data::Loader.prefectures.map { |data| new(**data) }.freeze
    end

    # 都道府県を検索する。
    #
    # @overload find(code)
    #   @param code [Integer] 都道府県コード
    # @overload find(name:)
    #   @param name [String] 日本語名(例: "東京都")
    # @overload find(name_en:)
    #   @param name_en [String] 英語名(例: "Tokyo")
    # @return [Prefecture, DB::Prefecture, nil]
    def find(code = nil, **options)
      attrs = code.nil? ? options : { code: code }
      return if attrs.empty?

      key, value = attrs.first
      return DB::Prefecture.find_by(key => value) if Basho.db?

      all.find { |pref| pref.public_send(key) == value }
    end

    # 都道府県を地方名で絞り込む。引数なしで全件返す。
    #
    # @param region [String, nil] 地方名(例: "関東")
    # @return [Array<Prefecture>, Array<DB::Prefecture>]
    def where(region: nil)
      return all unless region
      return DB::Prefecture.where(region_name: region).to_a if Basho.db?

      all.select { |pref| pref.region_name == region }
    end

    # メモリキャッシュをクリアする。
    #
    # @return [void]
    # @api private
    def reset_cache!
      remove_instance_variable(:@all) if defined?(@all)
    end
  end
end

#region_nameObject (readonly)

Returns the value of attribute region_name

Returns:

  • (Object)

    the current value of region_name



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/basho/prefecture.rb', line 24

Prefecture = ::Data.define(:code, :name, :name_en, :name_kana, :name_hiragana, :region_name, :type, :capital_code) do
  # 所属する地方を返す。
  #
  # @return [Region]
  def region
    Region.find(region_name)
  end

  # 所属する市区町村の一覧を返す。
  #
  # @return [Array<City>, Array<DB::City>]
  def cities
    City.where(prefecture_code: code)
  end

  # 県庁所在地を返す。
  #
  # @return [City, DB::City, nil]
  def capital
    City.find(capital_code)
  end

  class << self
    # 全47都道府県を返す。
    #
    # @return [Array<Prefecture>, Array<DB::Prefecture>]
    def all
      return DB::Prefecture.all.to_a if Basho.db?

      @all ||= Data::Loader.prefectures.map { |data| new(**data) }.freeze
    end

    # 都道府県を検索する。
    #
    # @overload find(code)
    #   @param code [Integer] 都道府県コード
    # @overload find(name:)
    #   @param name [String] 日本語名(例: "東京都")
    # @overload find(name_en:)
    #   @param name_en [String] 英語名(例: "Tokyo")
    # @return [Prefecture, DB::Prefecture, nil]
    def find(code = nil, **options)
      attrs = code.nil? ? options : { code: code }
      return if attrs.empty?

      key, value = attrs.first
      return DB::Prefecture.find_by(key => value) if Basho.db?

      all.find { |pref| pref.public_send(key) == value }
    end

    # 都道府県を地方名で絞り込む。引数なしで全件返す。
    #
    # @param region [String, nil] 地方名(例: "関東")
    # @return [Array<Prefecture>, Array<DB::Prefecture>]
    def where(region: nil)
      return all unless region
      return DB::Prefecture.where(region_name: region).to_a if Basho.db?

      all.select { |pref| pref.region_name == region }
    end

    # メモリキャッシュをクリアする。
    #
    # @return [void]
    # @api private
    def reset_cache!
      remove_instance_variable(:@all) if defined?(@all)
    end
  end
end

#typeObject (readonly)

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/basho/prefecture.rb', line 24

Prefecture = ::Data.define(:code, :name, :name_en, :name_kana, :name_hiragana, :region_name, :type, :capital_code) do
  # 所属する地方を返す。
  #
  # @return [Region]
  def region
    Region.find(region_name)
  end

  # 所属する市区町村の一覧を返す。
  #
  # @return [Array<City>, Array<DB::City>]
  def cities
    City.where(prefecture_code: code)
  end

  # 県庁所在地を返す。
  #
  # @return [City, DB::City, nil]
  def capital
    City.find(capital_code)
  end

  class << self
    # 全47都道府県を返す。
    #
    # @return [Array<Prefecture>, Array<DB::Prefecture>]
    def all
      return DB::Prefecture.all.to_a if Basho.db?

      @all ||= Data::Loader.prefectures.map { |data| new(**data) }.freeze
    end

    # 都道府県を検索する。
    #
    # @overload find(code)
    #   @param code [Integer] 都道府県コード
    # @overload find(name:)
    #   @param name [String] 日本語名(例: "東京都")
    # @overload find(name_en:)
    #   @param name_en [String] 英語名(例: "Tokyo")
    # @return [Prefecture, DB::Prefecture, nil]
    def find(code = nil, **options)
      attrs = code.nil? ? options : { code: code }
      return if attrs.empty?

      key, value = attrs.first
      return DB::Prefecture.find_by(key => value) if Basho.db?

      all.find { |pref| pref.public_send(key) == value }
    end

    # 都道府県を地方名で絞り込む。引数なしで全件返す。
    #
    # @param region [String, nil] 地方名(例: "関東")
    # @return [Array<Prefecture>, Array<DB::Prefecture>]
    def where(region: nil)
      return all unless region
      return DB::Prefecture.where(region_name: region).to_a if Basho.db?

      all.select { |pref| pref.region_name == region }
    end

    # メモリキャッシュをクリアする。
    #
    # @return [void]
    # @api private
    def reset_cache!
      remove_instance_variable(:@all) if defined?(@all)
    end
  end
end

Class Method Details

.allArray<Prefecture>, Array<DB::Prefecture>

全47都道府県を返す。

Returns:



50
51
52
53
54
# File 'lib/basho/prefecture.rb', line 50

def all
  return DB::Prefecture.all.to_a if Basho.db?

  @all ||= Data::Loader.prefectures.map { |data| new(**data) }.freeze
end

.find(code) ⇒ Prefecture, ... .find(name:) ⇒ Prefecture, ... .find(name_en:) ⇒ Prefecture, ...

都道府県を検索する。

Overloads:

  • .find(code) ⇒ Prefecture, ...

    Parameters:

    • code (Integer)

      都道府県コード

  • .find(name:) ⇒ Prefecture, ...

    Parameters:

    • name (String)

      日本語名(例: "東京都")

  • .find(name_en:) ⇒ Prefecture, ...

    Parameters:

    • name_en (String)

      英語名(例: "Tokyo")

Returns:



65
66
67
68
69
70
71
72
73
# File 'lib/basho/prefecture.rb', line 65

def find(code = nil, **options)
  attrs = code.nil? ? options : { code: code }
  return if attrs.empty?

  key, value = attrs.first
  return DB::Prefecture.find_by(key => value) if Basho.db?

  all.find { |pref| pref.public_send(key) == value }
end

.reset_cache!void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

メモリキャッシュをクリアする。



90
91
92
# File 'lib/basho/prefecture.rb', line 90

def reset_cache!
  remove_instance_variable(:@all) if defined?(@all)
end

.where(region: nil) ⇒ Array<Prefecture>, Array<DB::Prefecture>

都道府県を地方名で絞り込む。引数なしで全件返す。

Parameters:

  • region (String, nil) (defaults to: nil)

    地方名(例: "関東")

Returns:



79
80
81
82
83
84
# File 'lib/basho/prefecture.rb', line 79

def where(region: nil)
  return all unless region
  return DB::Prefecture.where(region_name: region).to_a if Basho.db?

  all.select { |pref| pref.region_name == region }
end

Instance Method Details

#capitalCity, ...

県庁所在地を返す。

Returns:



42
43
44
# File 'lib/basho/prefecture.rb', line 42

def capital
  City.find(capital_code)
end

#citiesArray<City>, Array<DB::City>

所属する市区町村の一覧を返す。

Returns:



35
36
37
# File 'lib/basho/prefecture.rb', line 35

def cities
  City.where(prefecture_code: code)
end

#regionRegion

所属する地方を返す。

Returns:



28
29
30
# File 'lib/basho/prefecture.rb', line 28

def region
  Region.find(region_name)
end