Class: Basho::Prefecture
- Inherits:
-
Data
- Object
- Data
- Basho::Prefecture
- Defined in:
- lib/basho/prefecture.rb
Overview
都道府県を表すイミュータブルなデータクラス。
DBバックエンドが有効な場合、クラスメソッドは自動的に DB::Prefecture 経由で検索する。
Instance Attribute Summary collapse
-
#capital_code ⇒ Object
readonly
Returns the value of attribute capital_code.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#name_en ⇒ Object
readonly
Returns the value of attribute name_en.
-
#name_hiragana ⇒ Object
readonly
Returns the value of attribute name_hiragana.
-
#name_kana ⇒ Object
readonly
Returns the value of attribute name_kana.
-
#region_name ⇒ Object
readonly
Returns the value of attribute region_name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.all ⇒ Array<Prefecture>, Array<DB::Prefecture>
全47都道府県を返す。.
-
.find(code = nil, **options) ⇒ Prefecture, ...
都道府県を検索する。.
-
.reset_cache! ⇒ void
private
メモリキャッシュをクリアする。.
-
.where(region: nil) ⇒ Array<Prefecture>, Array<DB::Prefecture>
都道府県を地方名で絞り込む。引数なしで全件返す。.
Instance Method Summary collapse
-
#capital ⇒ City, ...
県庁所在地を返す。.
-
#cities ⇒ Array<City>, Array<DB::City>
所属する市区町村の一覧を返す。.
-
#region ⇒ Region
所属する地方を返す。.
Instance Attribute Details
#capital_code ⇒ Object (readonly)
Returns the value of attribute 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, **) attrs = code.nil? ? : { 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 |
#code ⇒ Object (readonly)
Returns the value of attribute 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, **) attrs = code.nil? ? : { 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 ⇒ Object (readonly)
Returns the value of attribute 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, **) attrs = code.nil? ? : { 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_en ⇒ Object (readonly)
Returns the value of attribute 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, **) attrs = code.nil? ? : { 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_hiragana ⇒ Object (readonly)
Returns the value of attribute 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, **) attrs = code.nil? ? : { 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_kana ⇒ Object (readonly)
Returns the value of attribute 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, **) attrs = code.nil? ? : { 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_name ⇒ Object (readonly)
Returns the value of attribute 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, **) attrs = code.nil? ? : { 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 |
#type ⇒ Object (readonly)
Returns the value of attribute 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, **) attrs = code.nil? ? : { 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
.all ⇒ Array<Prefecture>, Array<DB::Prefecture>
全47都道府県を返す。
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, ...
都道府県を検索する。
65 66 67 68 69 70 71 72 73 |
# File 'lib/basho/prefecture.rb', line 65 def find(code = nil, **) attrs = code.nil? ? : { 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>
都道府県を地方名で絞り込む。引数なしで全件返す。
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
#capital ⇒ City, ...
県庁所在地を返す。
42 43 44 |
# File 'lib/basho/prefecture.rb', line 42 def capital City.find(capital_code) end |