jp_prefecture 0.8.0 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
data/data/zip.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  # { prefecture_code: [[from_zip_1, to_zip_1], [from_zip_2, to_zip_2], ...], ... }
2
+ # Last updated: 2021-02-10 05:59:09 UTC
2
3
  ---
3
4
  1:
4
5
  - - 10000
@@ -41,6 +42,8 @@
41
42
  - 3491221
42
43
  10:
43
44
  - - 3700000
45
+ - 3701506
46
+ - - 3701511
44
47
  - 3792314
45
48
  - - 3840097
46
49
  - 3840097
@@ -51,6 +54,8 @@
51
54
  - 3491216
52
55
  - - 3500001
53
56
  - 3691912
57
+ - - 3701507
58
+ - 3701507
54
59
  12:
55
60
  - - 2600000
56
61
  - 2995506
@@ -110,14 +115,14 @@
110
115
  - - 4314121
111
116
  - 4314121
112
117
  - - 4400001
113
- - 4970058
114
- - - 4980000
118
+ - 4980000
119
+ - - 4980001
115
120
  - 4980069
116
121
  24:
117
122
  - - 4980000
118
123
  - 4980000
119
124
  - - 4980801
120
- - 4980823
125
+ - 4980824
121
126
  - - 5100000
122
127
  - 5195835
123
128
  - - 6471321
@@ -231,8 +236,8 @@
231
236
  - - 8391421
232
237
  - 8391421
233
238
  - - 8700001
234
- - 8710000
235
- - - 8710001
239
+ - 8701223
240
+ - - 8710000
236
241
  - 8710208
237
242
  - - 8710311
238
243
  - 8710716
data/lib/jp_prefecture.rb CHANGED
@@ -1,8 +1,9 @@
1
- # coding: utf-8
2
- require "jp_prefecture/base"
3
- require "jp_prefecture/config"
4
- require "jp_prefecture/prefecture"
5
- require "jp_prefecture/version"
1
+ # frozen_string_literal: true
2
+
3
+ require 'jp_prefecture/base'
4
+ require 'jp_prefecture/config'
5
+ require 'jp_prefecture/prefecture'
6
+ require 'jp_prefecture/version'
6
7
 
7
8
  module JpPrefecture
8
9
  @config = Config.new
@@ -1,4 +1,5 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module JpPrefecture
3
4
  module Base
4
5
  # モデル内で使用するための設定を行う
@@ -34,7 +35,7 @@ module JpPrefecture
34
35
  method_name = options[:method_name] || :prefecture
35
36
 
36
37
  define_method method_name do
37
- JpPrefecture::Prefecture.find(self.send(column_name))
38
+ JpPrefecture::Prefecture.find(send(column_name))
38
39
  end
39
40
  end
40
41
  end
@@ -1,9 +1,7 @@
1
- # -*- coding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  module JpPrefecture
4
4
  class Config
5
- attr_accessor :mapping_data
6
- attr_accessor :zip_mapping_data
5
+ attr_accessor :mapping_data, :zip_mapping_data
7
6
  end
8
7
  end
9
-
@@ -1,10 +1,10 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'yaml'
3
4
 
4
5
  module JpPrefecture
5
6
  # コードと都道府県のマッピング
6
7
  module Mapping
7
-
8
8
  filepath = File.join(File.dirname(__FILE__), '../../data/prefecture.yml')
9
9
  @data = YAML.load_file(filepath)
10
10
 
@@ -1,91 +1,46 @@
1
- # coding: utf-8
2
- require "jp_prefecture/mapping"
3
- require "jp_prefecture/zip_mapping"
1
+ # frozen_string_literal: true
2
+
3
+ require 'jp_prefecture/prefecture/finder'
4
+ require 'jp_prefecture/mapping'
5
+ require 'jp_prefecture/zip_mapping'
4
6
 
5
7
  module JpPrefecture
6
8
  # 都道府県のコードと名前を扱うクラス
7
9
  class Prefecture
10
+ attr_accessor :code, :name, :name_e, :name_h, :name_k, :zips, :area, :type
8
11
 
9
- attr_accessor :code, :name, :name_e, :name_h, :name_k, :zips, :area
10
-
11
- # 配列から都道府県クラスを生成
12
+ # 都道府県コードから都道府県インスタンスを作成
12
13
  #
13
14
  # @example
14
- # # コード/名前から都道府県クラスを生成
15
- # JpPrefecture::Prefecture.build(1, '北海道', 'Hokkaido')
16
- #
17
- # @param pref [Integer] 都道府県コード
18
- # @param name [String] 都道府県名
19
- # @param name_e [String] 都道府県名(英語表記)
20
- # @param optional name_h [String] 都道府県名(ひらがな表記)
21
- # @param optional name_k [String] 都道府県名(カタカナ表記)
22
- # @param optional area [String] 地方名
23
- # @param zips [Array] 郵便番号の配列 (array of ranges, can be used in ARel, e.g. User.where(zip: prefecture.zips))
24
- def self.build(code, name, name_e, name_h = nil, name_k = nil, area = nil)
25
- pref = self.new
26
-
27
- pref.code = code
28
- pref.name = name
29
- pref.name_e = name_e.capitalize
30
- pref.name_h = name_h
31
- pref.name_k = name_k
32
- pref.zips = ZipMapping.data[code]
33
- pref.area = area
34
-
35
- pref
36
- end
37
-
38
- # 都道府県を検索
39
- #
40
- # @example
41
- # # 都道府県コードから検索
42
- # JpPrefecture::Prefecture.find(1)
43
- # JpPrefecture::Prefecture.find(code: 1)
15
+ # # 都道府県コードから都道府県インスタンスを生成
16
+ # JpPrefecture::Prefecture.build_by_code(1)
44
17
  #
45
- # # 都道府県名から検索
46
- # JpPrefecture::Prefecture.find(name: '北海道')
47
- #
48
- # # 都道府県名から検索(前方一致)
49
- # JpPrefecture::Prefecture.find(name: '東京')
50
- #
51
- # # 英語表記の都道府県名から検索
52
- # JpPrefecture::Prefecture.find(name: 'Hokkaido')
53
- # JpPrefecture::Prefecture.find(name: 'hokkaido')
54
- #
55
- # @param args [Integer] 都道府県コード
56
- # @param [Hash] args 検索条件
57
- # @option args [Integer] :code 都道府県コード
58
- # @option args [String] :name 都道府県名/英語/ひらがな/カタカナ表記の都道府県名
59
- # @option args [Integer] :zip 郵便番号
60
- # @return [JpPrefecture::Prefecture] 都道府県が見つかった場合は都道府県クラス
18
+ # @param code [Integer] 都道府県コード
19
+ # @return [JpPrefecture::Prefecture] 都道府県インスタンス
61
20
  # @return [nil] 都道府県が見つからない場合は nil
62
- def self.find(args)
63
- return if args.nil?
64
-
65
- if args.is_a?(Integer) || args.is_a?(String)
66
- code = args.to_i
67
- else
68
- code =
69
- case args.keys[0]
70
- when :name
71
- self.find_code_by_name(args[:name])
72
- when :code
73
- args[:code].to_i
74
- when :zip
75
- ZipMapping.code_for_zip(args[:zip].to_i)
76
- end
77
- end
78
-
79
- names = Mapping.data[code]
80
-
81
- return unless names
21
+ def self.build_by_code(code) # rubocop:disable Metrics/AbcSize
22
+ result = Mapping.data[code]
23
+ return unless result
24
+
25
+ pref = new
26
+
27
+ pref.code = code
28
+ pref.name = result[:name]
29
+ pref.name_e = result[:name_e].capitalize
30
+ pref.name_h = result[:name_h]
31
+ pref.name_k = result[:name_k]
32
+ pref.zips = ZipMapping.data[code]
33
+ pref.area = result[:area]
34
+ pref.type =
35
+ case pref.name[-1]
36
+ when '都', '道', '府', '県'
37
+ pref.name[-1]
38
+ end
82
39
 
83
- self.build(code,
84
- names[:name], names[:name_e],
85
- names[:name_h], names[:name_k], names[:area])
40
+ pref
86
41
  end
87
42
 
88
- # すべての都道府県クラスを返す
43
+ # すべての都道府県を取得
89
44
  #
90
45
  # @example
91
46
  # # 都道府県の一覧を取得
@@ -97,36 +52,56 @@ module JpPrefecture
97
52
  # # collection_select で選択肢を生成(英語表記)
98
53
  # f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name_e
99
54
  #
100
- # @return [Array] 都道府県クラスの配列
55
+ # @return [Array<JpPrefecture::Prefecture>] 都道府県インスタンスの配列
101
56
  def self.all
102
- Mapping.data.map do |pref|
103
- names = pref[1]
104
- self.build(pref[0],
105
- names[:name], names[:name_e],
106
- names[:name_h], names[:name_k], names[:area])
107
- end
57
+ Mapping.data.map { |code, _| build_by_code(code) }
108
58
  end
109
59
 
110
- protected
111
-
112
- # 名前から都道府県コードを前方一致で検索
60
+ # 都道府県を検索
61
+ #
62
+ # 文字列は前方一致で検索する
63
+ #
64
+ # @example
65
+ # # 都道府県コードを検索
66
+ # JpPrefecture::Prefecture.find(1)
67
+ # JpPrefecture::Prefecture.find(code: 1)
68
+ #
69
+ # # 都道府県名を検索
70
+ # JpPrefecture::Prefecture.find(name: '北海道')
71
+ # JpPrefecture::Prefecture.find(name: '東京')
113
72
  #
114
- # @param name [String] 検索する都道府県名
115
- # @return [Integer] 見つかった場合は都道府県コード
116
- # @return [nil] 見つからない場合は nil
117
- def self.find_code_by_name(name)
118
- name = name.downcase
73
+ # # 英語表記の都道府県名を検索
74
+ # JpPrefecture::Prefecture.find(name_e: 'Hokkaido')
75
+ # JpPrefecture::Prefecture.find(name_e: 'hokkaido')
76
+ #
77
+ # # ひらがな表記の都道府県名を検索
78
+ # JpPrefecture::Prefecture.find(name_h: 'ほっかいどう')
79
+ #
80
+ # # カタカナ表記の都道府県名を検索
81
+ # JpPrefecture::Prefecture.find(name_k: 'ホッカイドウ')
82
+ #
83
+ # # すべての項目を検索
84
+ # JpPrefecture::Prefecture.find(all_fields: '東')
85
+ #
86
+ # @param args [Integer] 都道府県コード
87
+ # @param args [Hash<Symbol, Integer>] :code 都道府県コード
88
+ # @param args [Hash<Symbol, String>] :name 漢字表記/:name_e 英語表記/:name_h ひらがな表記/:name_k カタカナ表記
89
+ # @param args [Hash<Symbol, Integer>] :zip 郵便番号
90
+ # @param args [Hash<Symbol, (String, Integer)>] :all_fields マッピングに定義しているすべてのフィールドから検索
91
+ # @return [JpPrefecture::Prefecture] 都道府県が見つかった場合は都道府県インスタンス
92
+ # @return [nil] 都道府県が見つからない場合は nil
93
+ def self.find(args)
94
+ return if args.nil?
119
95
 
120
- Mapping.data.each do |m|
121
- m[1].values.each do |v|
122
- if v.start_with?(name)
123
- return m[0]
124
- end
125
- end
126
- end
96
+ case args
97
+ when Integer, String
98
+ JpPrefecture::Prefecture::Finder.new.find(field: nil, value: args)
99
+ when Hash
100
+ search_field = args.keys.first
101
+ search_value = args.values.first
127
102
 
128
- nil
103
+ JpPrefecture::Prefecture::Finder.new.find(field: search_field, value: search_value)
104
+ end
129
105
  end
130
-
131
106
  end
132
107
  end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jp_prefecture/mapping'
4
+ require 'jp_prefecture/zip_mapping'
5
+
6
+ module JpPrefecture
7
+ class Prefecture
8
+ # 都道府県の検索を行うクラス
9
+ class Finder
10
+ def initialize
11
+ @mapping = Mapping.data
12
+ end
13
+
14
+ # 指定した項目を検索
15
+ #
16
+ # @param field [Symbol] 検索する項目。nil の場合は都道府県コードとして扱う
17
+ # @param value [String, Integer] 検索する内容
18
+ # @return [JpPrefecture::Prefecture] 都道府県が見つかった場合は都道府県インスタンス
19
+ # @return [nil] 都道府県が見つからない場合は nil
20
+ def find(field:, value:)
21
+ code = find_code(field, value)
22
+ JpPrefecture::Prefecture.build_by_code(code)
23
+ end
24
+
25
+ private
26
+
27
+ # @param field [Symbol] 検索する項目
28
+ # @param value [String, Integer] 検索する内容
29
+ # @return [Integer] 見つかった場合は都道府県コード
30
+ # @return [nil] 見つからない場合は nil
31
+ def find_code(field, value)
32
+ return value.to_i if field.nil?
33
+
34
+ case field
35
+ when :all_fields
36
+ find_code_by_name_from_all_fields(value)
37
+ when :name, :name_h, :name_k, :name_e
38
+ find_code_by_name(field, value)
39
+ when :code
40
+ value.to_i
41
+ when :zip
42
+ ZipMapping.code_for_zip(value.to_i)
43
+ end
44
+ end
45
+
46
+ # すべての項目を前方一致で検索
47
+ def find_code_by_name_from_all_fields(value)
48
+ return if value.nil? || value.empty?
49
+
50
+ value = value.downcase
51
+
52
+ @mapping.each do |m|
53
+ m[1].each_value do |v|
54
+ return m[0] if v.start_with?(value)
55
+ end
56
+ end
57
+ end
58
+
59
+ # 指定した項目を前方一致で検索
60
+ def find_code_by_name(field, value)
61
+ return if value.nil? || value.empty?
62
+
63
+ value = value.downcase
64
+
65
+ @mapping.each do |m|
66
+ return m[0] if m[1][field].start_with?(value)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module JpPrefecture
2
- VERSION = '0.8.0'
4
+ VERSION = '1.0.0.rc1'
3
5
  end
@@ -1,21 +1,21 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'yaml'
3
4
 
4
5
  module JpPrefecture
5
6
  # コードと郵便番号のマッピング
6
7
  module ZipMapping
7
-
8
8
  filepath = File.join(File.dirname(__FILE__), '../../data/zip.yml')
9
9
  @data = YAML.load_file(filepath)
10
- @data = Hash[*@data.collect{|code, arr|[code, arr.collect{|zip_from, zip_to|zip_from..zip_to}]}.flatten(1)]
10
+ @data = Hash[*@data.collect { |code, arr| [code, arr.collect { |zip_from, zip_to| zip_from..zip_to }] }.flatten(1)]
11
11
 
12
12
  def self.data
13
13
  JpPrefecture.config.zip_mapping_data || @data
14
14
  end
15
15
 
16
- def self.code_for_zip zip
17
- self.data.select do |code, zip_ranges|
18
- zip_ranges.find{|range|range.include? zip}
16
+ def self.code_for_zip(zip)
17
+ data.select do |_code, zip_ranges|
18
+ zip_ranges.find { |range| range.include? zip }
19
19
  end.keys.first
20
20
  end
21
21
  end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jp_prefecture
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 1.0.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
- - chocoby
8
- autorequire:
7
+ - "@chocoby"
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-07 00:00:00.000000000 Z
11
+ date: 2021-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: appraisal
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: rake
15
43
  requirement: !ruby/object:Gem::Requirement
@@ -28,30 +56,100 @@ dependencies:
28
56
  name: rspec
29
57
  requirement: !ruby/object:Gem::Requirement
30
58
  requirements:
31
- - - "~>"
59
+ - - ">="
32
60
  - !ruby/object:Gem::Version
33
- version: '3.1'
61
+ version: '0'
34
62
  type: :development
35
63
  prerelease: false
36
64
  version_requirements: !ruby/object:Gem::Requirement
37
65
  requirements:
38
- - - "~>"
66
+ - - ">="
39
67
  - !ruby/object:Gem::Version
40
- version: '3.1'
68
+ version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
- name: activerecord
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-performance
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rake
43
99
  requirement: !ruby/object:Gem::Requirement
44
100
  requirements:
45
101
  - - ">="
46
102
  - !ruby/object:Gem::Version
47
- version: 3.2.0
103
+ version: '0'
48
104
  type: :development
49
105
  prerelease: false
50
106
  version_requirements: !ruby/object:Gem::Requirement
51
107
  requirements:
52
108
  - - ">="
53
109
  - !ruby/object:Gem::Version
54
- version: 3.2.0
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov-lcov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
55
153
  - !ruby/object:Gem::Dependency
56
154
  name: sqlite3
57
155
  requirement: !ruby/object:Gem::Requirement
@@ -66,44 +164,32 @@ dependencies:
66
164
  - - ">="
67
165
  - !ruby/object:Gem::Version
68
166
  version: '0'
69
- description: Convert japan prefecture code(JIS X 0402 based) into prefecture name.
167
+ description: Convert japan prefecture code (JIS X 0402 based) into prefecture name.
70
168
  email:
71
169
  - chocoby@gmail.com
72
170
  executables: []
73
171
  extensions: []
74
172
  extra_rdoc_files: []
75
173
  files:
76
- - ".gitignore"
77
- - ".rspec"
78
- - ".travis.yml"
79
- - ".yardopts"
80
174
  - CHANGELOG.md
81
- - Gemfile
82
175
  - LICENSE
83
176
  - README.md
84
- - Rakefile
177
+ - README_EN.md
85
178
  - data/prefecture.yml
86
179
  - data/zip.yml
87
- - jp_prefecture.gemspec
88
180
  - lib/jp_prefecture.rb
89
181
  - lib/jp_prefecture/base.rb
90
182
  - lib/jp_prefecture/config.rb
91
183
  - lib/jp_prefecture/mapping.rb
92
184
  - lib/jp_prefecture/prefecture.rb
185
+ - lib/jp_prefecture/prefecture/finder.rb
93
186
  - lib/jp_prefecture/version.rb
94
187
  - lib/jp_prefecture/zip_mapping.rb
95
- - spec/base_spec.rb
96
- - spec/config_spec.rb
97
- - spec/fixtures/prefecture.yml
98
- - spec/jp_prefecture_spec.rb
99
- - spec/mapping_spec.rb
100
- - spec/prefecture_spec.rb
101
- - spec/spec_helper.rb
102
- - spec/zip_mapping_spec.rb
103
188
  homepage: https://github.com/chocoby/jp_prefecture
104
- licenses: []
189
+ licenses:
190
+ - MIT
105
191
  metadata: {}
106
- post_install_message:
192
+ post_install_message:
107
193
  rdoc_options: []
108
194
  require_paths:
109
195
  - lib
@@ -111,24 +197,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
111
197
  requirements:
112
198
  - - ">="
113
199
  - !ruby/object:Gem::Version
114
- version: '0'
200
+ version: 2.4.0
115
201
  required_rubygems_version: !ruby/object:Gem::Requirement
116
202
  requirements:
117
- - - ">="
203
+ - - ">"
118
204
  - !ruby/object:Gem::Version
119
- version: '0'
205
+ version: 1.3.1
120
206
  requirements: []
121
- rubyforge_project:
122
- rubygems_version: 2.2.2
123
- signing_key:
207
+ rubygems_version: 3.1.4
208
+ signing_key:
124
209
  specification_version: 4
125
210
  summary: Convert japan prefecture code into prefecture name
126
- test_files:
127
- - spec/base_spec.rb
128
- - spec/config_spec.rb
129
- - spec/fixtures/prefecture.yml
130
- - spec/jp_prefecture_spec.rb
131
- - spec/mapping_spec.rb
132
- - spec/prefecture_spec.rb
133
- - spec/spec_helper.rb
134
- - spec/zip_mapping_spec.rb
211
+ test_files: []