Class: Basho::PostalCode

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

Overview

郵便番号を表すイミュータブルなデータクラス。 常にJSONファイルから読み込む(DBバックエンド対象外)。

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#city_nameObject (readonly)

Returns the value of attribute city_name

Returns:

  • (Object)

    the current value of 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

#codeObject (readonly)

Returns the value of attribute code

Returns:

  • (Object)

    the current value of 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_codeObject (readonly)

Returns the value of attribute prefecture_code

Returns:

  • (Object)

    the current value of 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

#townObject (readonly)

Returns the value of attribute town

Returns:

  • (Object)

    the current value of 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?

郵便番号で検索する。ハイフン有無どちらも可。

Parameters:

  • code (String)

    郵便番号(例: "154-0011", "1540011")

Returns:



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

def find(code)
  where(code: code).first
end

.where(code:) ⇒ Array<PostalCode>

郵便番号で検索し、配列で返す。共有郵便番号の場合は複数件。

Parameters:

  • code (String)

    郵便番号

Returns:



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_codeString

ハイフン付きの郵便番号を返す。

Returns:

  • (String)

    例: "154-0011"



19
20
21
# File 'lib/basho/postal_code.rb', line 19

def formatted_code
  "#{code[0..2]}-#{code[3..]}"
end

#prefecturePrefecture, ...

所属する都道府県を返す。

Returns:



33
34
35
# File 'lib/basho/postal_code.rb', line 33

def prefecture
  Prefecture.find(prefecture_code)
end

#prefecture_nameString?

都道府県名を返す。

Returns:

  • (String, nil)

    例: "東京都"



26
27
28
# File 'lib/basho/postal_code.rb', line 26

def prefecture_name
  prefecture&.name
end