Class: Basho::PostalCode
- Inherits:
-
Data
- Object
- Data
- Basho::PostalCode
- Defined in:
- lib/basho/postal_code.rb
Overview
郵便番号を表すイミュータブルなデータクラス。 常にJSONファイルから読み込む(DBバックエンド対象外)。
Instance Attribute Summary collapse
-
#city_name ⇒ Object
readonly
Returns the value of attribute city_name.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#prefecture_code ⇒ Object
readonly
Returns the value of attribute prefecture_code.
-
#town ⇒ Object
readonly
Returns the value of attribute town.
Class Method Summary collapse
-
.find(code) ⇒ PostalCode?
郵便番号で検索する。ハイフン有無どちらも可。.
-
.where(code:) ⇒ Array<PostalCode>
郵便番号で検索し、配列で返す。共有郵便番号の場合は複数件。.
Instance Method Summary collapse
-
#formatted_code ⇒ String
ハイフン付きの郵便番号を返す。.
-
#prefecture ⇒ Prefecture, ...
所属する都道府県を返す。.
-
#prefecture_name ⇒ String?
都道府県名を返す。.
Instance Attribute Details
#city_name ⇒ Object (readonly)
Returns the value of attribute city_name
15 16 17 18 19 20 21 22 23 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 |
# File 'lib/basho/postal_code.rb', line 15 PostalCode = ::Data.define(:code, :prefecture_code, :city_name, :town) do # ハイフン付きの郵便番号を返す。 # # @return [String] 例: "154-0011" def formatted_code "#{code[0..2]}-#{code[3..]}" end # 都道府県名を返す。 # # @return [String, nil] 例: "東京都" def prefecture_name prefecture&.name end # 所属する都道府県を返す。 # # @return [Prefecture, DB::Prefecture, nil] def prefecture Prefecture.find(prefecture_code) end class << self # 郵便番号で検索する。ハイフン有無どちらも可。 # # @param code [String] 郵便番号(例: "154-0011", "1540011") # @return [PostalCode, nil] def find(code) where(code: code).first end # 郵便番号で検索し、配列で返す。共有郵便番号の場合は複数件。 # # @param code [String] 郵便番号 # @return [Array<PostalCode>] def where(code:) normalized = code.to_s.delete("-") return [] unless normalized.match?(/\A\d{7}\z/) prefix = normalized[0..2] Data::Loader.postal_codes(prefix) .filter_map { |data| new(**data) if data[:code] == normalized } end end end |
#code ⇒ Object (readonly)
Returns the value of attribute code
15 16 17 18 19 20 21 22 23 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 |
# File 'lib/basho/postal_code.rb', line 15 PostalCode = ::Data.define(:code, :prefecture_code, :city_name, :town) do # ハイフン付きの郵便番号を返す。 # # @return [String] 例: "154-0011" def formatted_code "#{code[0..2]}-#{code[3..]}" end # 都道府県名を返す。 # # @return [String, nil] 例: "東京都" def prefecture_name prefecture&.name end # 所属する都道府県を返す。 # # @return [Prefecture, DB::Prefecture, nil] def prefecture Prefecture.find(prefecture_code) end class << self # 郵便番号で検索する。ハイフン有無どちらも可。 # # @param code [String] 郵便番号(例: "154-0011", "1540011") # @return [PostalCode, nil] def find(code) where(code: code).first end # 郵便番号で検索し、配列で返す。共有郵便番号の場合は複数件。 # # @param code [String] 郵便番号 # @return [Array<PostalCode>] def where(code:) normalized = code.to_s.delete("-") return [] unless normalized.match?(/\A\d{7}\z/) prefix = normalized[0..2] Data::Loader.postal_codes(prefix) .filter_map { |data| new(**data) if data[:code] == normalized } end end end |
#prefecture_code ⇒ Object (readonly)
Returns the value of attribute prefecture_code
15 16 17 18 19 20 21 22 23 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 |
# File 'lib/basho/postal_code.rb', line 15 PostalCode = ::Data.define(:code, :prefecture_code, :city_name, :town) do # ハイフン付きの郵便番号を返す。 # # @return [String] 例: "154-0011" def formatted_code "#{code[0..2]}-#{code[3..]}" end # 都道府県名を返す。 # # @return [String, nil] 例: "東京都" def prefecture_name prefecture&.name end # 所属する都道府県を返す。 # # @return [Prefecture, DB::Prefecture, nil] def prefecture Prefecture.find(prefecture_code) end class << self # 郵便番号で検索する。ハイフン有無どちらも可。 # # @param code [String] 郵便番号(例: "154-0011", "1540011") # @return [PostalCode, nil] def find(code) where(code: code).first end # 郵便番号で検索し、配列で返す。共有郵便番号の場合は複数件。 # # @param code [String] 郵便番号 # @return [Array<PostalCode>] def where(code:) normalized = code.to_s.delete("-") return [] unless normalized.match?(/\A\d{7}\z/) prefix = normalized[0..2] Data::Loader.postal_codes(prefix) .filter_map { |data| new(**data) if data[:code] == normalized } end end end |
#town ⇒ Object (readonly)
Returns the value of attribute town
15 16 17 18 19 20 21 22 23 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 |
# File 'lib/basho/postal_code.rb', line 15 PostalCode = ::Data.define(:code, :prefecture_code, :city_name, :town) do # ハイフン付きの郵便番号を返す。 # # @return [String] 例: "154-0011" def formatted_code "#{code[0..2]}-#{code[3..]}" end # 都道府県名を返す。 # # @return [String, nil] 例: "東京都" def prefecture_name prefecture&.name end # 所属する都道府県を返す。 # # @return [Prefecture, DB::Prefecture, nil] def prefecture Prefecture.find(prefecture_code) end class << self # 郵便番号で検索する。ハイフン有無どちらも可。 # # @param code [String] 郵便番号(例: "154-0011", "1540011") # @return [PostalCode, nil] def find(code) where(code: code).first end # 郵便番号で検索し、配列で返す。共有郵便番号の場合は複数件。 # # @param code [String] 郵便番号 # @return [Array<PostalCode>] def where(code:) normalized = code.to_s.delete("-") return [] unless normalized.match?(/\A\d{7}\z/) prefix = normalized[0..2] Data::Loader.postal_codes(prefix) .filter_map { |data| new(**data) if data[:code] == normalized } end end end |
Class Method Details
.find(code) ⇒ PostalCode?
郵便番号で検索する。ハイフン有無どちらも可。
42 43 44 |
# File 'lib/basho/postal_code.rb', line 42 def find(code) where(code: code).first end |
.where(code:) ⇒ Array<PostalCode>
郵便番号で検索し、配列で返す。共有郵便番号の場合は複数件。
50 51 52 53 54 55 56 57 |
# File 'lib/basho/postal_code.rb', line 50 def where(code:) normalized = code.to_s.delete("-") return [] unless normalized.match?(/\A\d{7}\z/) prefix = normalized[0..2] Data::Loader.postal_codes(prefix) .filter_map { |data| new(**data) if data[:code] == normalized } end |
Instance Method Details
#formatted_code ⇒ String
ハイフン付きの郵便番号を返す。
19 20 21 |
# File 'lib/basho/postal_code.rb', line 19 def formatted_code "#{code[0..2]}-#{code[3..]}" end |
#prefecture ⇒ Prefecture, ...
所属する都道府県を返す。
33 34 35 |
# File 'lib/basho/postal_code.rb', line 33 def prefecture Prefecture.find(prefecture_code) end |
#prefecture_name ⇒ String?
都道府県名を返す。
26 27 28 |
# File 'lib/basho/postal_code.rb', line 26 def prefecture_name prefecture&.name end |