jp_prefecture 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a27297e5e749600c092f75152e0999acbeb4fea4
4
- data.tar.gz: a064cc7a1040ae8b8dea0ec30edec261ae5c7ef4
3
+ metadata.gz: 151ed52704cd41c8b9fb1a240bd441306ef38133
4
+ data.tar.gz: 661515a372f6075e08dd954c43eb5ead8cb14ebb
5
5
  SHA512:
6
- metadata.gz: 21afc87b61f2cfe8d42d3ce922e49a9c94bc70bb9e73f151097ec2349e0b02d627dd865e661ec37768dade6154c4f7f6296800a974c6b0364f340d1f57970502
7
- data.tar.gz: a87e5466e139dce67729002a6384d0c734f55a351d1d6cfefba86412022f1483144c1053b174d4e22d29230f1f6ff71dbad31bdc0fc8f789de234893c04a8887
6
+ metadata.gz: 8a9bef0061e91d081adbe2a557e1cd768555ec21970e595ddfe443420aba9ec5877f43ad5c2406800cada9fef99094e1e5d3aa02a0e1a79db2a652e810e8f05d
7
+ data.tar.gz: 38747b5b15cb93374e0c2508639fa6d43b34b5139d07ce63721a05a6f2c665f7d88f4efe60eef73bf268394fd25c8130ae82ad53c67d3477ff5f024180713782
data/CHANGELOG.md CHANGED
@@ -1,38 +1,42 @@
1
- ## 0.4.0 (July 8, 2013) ##
1
+ ## 0.5.0 (Nov 30, 2013)
2
2
 
3
- * 都道府県のマッピングを変更できるようにした (@mizoR)
3
+ * 郵便番号から都道府県を検索する機能の追加 (PR #9/@fruwe)
4
4
 
5
- ## 0.3.2 (June 12, 2013) ##
5
+ ## 0.4.0 (Jul 08, 2013)
6
+
7
+ * 都道府県のマッピングを変更する機能の追加 (PR #8/@mizoR)
8
+
9
+ ## 0.3.2 (Jun 12, 2013)
6
10
 
7
11
  * 文字列を downcase してから都道府県の検索を行うようにした
8
12
 
9
- ## 0.3.1 (June 11, 2013) ##
13
+ ## 0.3.1 (Jun 11, 2013)
10
14
 
11
15
  * name で渡した文字列が変更されるバグを修正
12
16
 
13
- ## 0.3.0 (June 11, 2013) ##
17
+ ## 0.3.0 (Jun 11, 2013)
14
18
 
15
19
  * 都道府県名(英語表記含む)から都道府県を検索できるようにした
16
20
  * 都道府県名からの検索と同様の方法で、都道府県コードを検索できるようにした
17
21
  * String 型の都道府県コードに対応
18
22
 
19
- ## 0.2.0 (June 9, 2013) ##
23
+ ## 0.2.0 (Jun 09, 2013)
20
24
 
21
25
  * 英語表記を追加
22
26
  * Ruby 1.8.7 のサポートを終了
23
27
 
24
- ## 0.1.1 (March 1, 2013) ##
28
+ ## 0.1.1 (Mar 01, 2013)
25
29
 
26
30
  * モデルで使用する時に、生成するメソッド名を指定できるようにした
27
31
  * Configuration モデル/Model モジュールを廃止
28
32
  * extend による使用を廃止
29
33
  * Travis CI によるテストを行うようにした
30
34
 
31
- ## 0.1.0 (February 17, 2013) ##
35
+ ## 0.1.0 (Feb 17, 2013)
32
36
 
33
37
  * モデルで使用する時に、対象のカラム名を指定するようにした
34
38
 
35
- ## 0.0.1 (February 14, 2013) ##
39
+ ## 0.0.1 (Feb 14, 2013)
36
40
 
37
41
  * 都道府県の変換
38
42
  * 簡単な Rails サポート
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.org/chocoby/jp_prefecture.png?branch=master)](https://travis-ci.org/chocoby/jp_prefecture)
4
4
  [![Dependency Status](https://gemnasium.com/chocoby/jp_prefecture.png)](https://gemnasium.com/chocoby/jp_prefecture)
5
+ [![Gem Version](https://badge.fury.io/rb/jp_prefecture.png)](http://badge.fury.io/rb/jp_prefecture)
5
6
 
6
7
  https://rubygems.org/gems/jp_prefecture
7
8
 
@@ -12,63 +13,82 @@ https://rubygems.org/gems/jp_prefecture
12
13
  JIS X 0402 で定義されている都道府県コードをベースに、
13
14
  ゼロから始まるものはゼロを削除して使用しています。
14
15
 
15
- 北海道: 01 -> 1
16
- 東京都: 13 -> 13
16
+ ```
17
+ 北海道: 01 -> 1
18
+ 東京都: 13 -> 13
19
+ ```
17
20
 
18
21
  参考: [Wikipedia: 全国地方公共団体コード](http://ja.wikipedia.org/wiki/%E5%85%A8%E5%9B%BD%E5%9C%B0%E6%96%B9%E5%85%AC%E5%85%B1%E5%9B%A3%E4%BD%93%E3%82%B3%E3%83%BC%E3%83%89#.E9.83.BD.E9.81.93.E5.BA.9C.E7.9C.8C.E3.82.B3.E3.83.BC.E3.83.89)
19
22
 
23
+ 都道府県コードと都道府県名のマッピングは変更することもできます。
24
+ 詳しくは「都道府県のマッピング情報を変更する」の項目を参照してください。
25
+
20
26
  また、Rails のプラグインとして使用することもできます。
21
27
 
22
28
  ## インストール
23
29
 
24
30
  以下の行を `Gemfile` に記述してから:
25
31
 
26
- gem 'jp_prefecture'
32
+ ```
33
+ gem 'jp_prefecture'
34
+ ```
27
35
 
28
36
  `bundle` を実行してください。
29
37
 
30
38
  または、手動でインストールしてください:
31
39
 
32
- $ gem install jp_prefecture
40
+ ```
41
+ $ gem install jp_prefecture
42
+ ```
33
43
 
34
44
  ## 使い方
35
45
 
36
46
  ### ライブラリの読み込み
37
47
 
38
- require 'jp_prefecture'
48
+ ```ruby
49
+ require 'jp_prefecture'
50
+ ```
39
51
 
40
52
  ### 都道府県コードから都道府県を検索
41
53
 
42
54
  単純に都道府県コードを渡すと、都道府県コードから都道府県を検索します:
43
55
 
44
- pref = JpPrefecture::Prefecture.find 13
45
- # => #<JpPrefecture::Prefecture:0x007fd0a3d43fe8 @code=13, @name="東京都", @name_e="Tokyo">
46
- pref.code
47
- # => 13
48
- pref.name
49
- # => "東京都"
50
- pref.name_e
51
- # => "Tokyo"
56
+ ```ruby
57
+ pref = JpPrefecture::Prefecture.find 13
58
+ # => #<JpPrefecture::Prefecture:0x007fd0a3d43fe8 @code=13, @name="東京都", @name_e="Tokyo">
59
+ pref.code
60
+ # => 13
61
+ pref.name
62
+ # => "東京都"
63
+ pref.name_e
64
+ # => "Tokyo"
65
+ ```
52
66
 
53
67
  以下のように渡すことも可能です:
54
68
 
55
- JpPrefecture::Prefecture.find code: 13
69
+ ```ruby
70
+ JpPrefecture::Prefecture.find code: 13
71
+ ```
56
72
 
57
73
  ### 都道府県名から都道府県を検索
58
74
 
59
- JpPrefecture::Prefecture.find name: "東京都"
60
- # => #<JpPrefecture::Prefecture:0x007ff672271800 @code=13, @name="東京都", @name_e="Tokyo">
75
+ ```ruby
76
+ JpPrefecture::Prefecture.find name: "東京都"
77
+ # => #<JpPrefecture::Prefecture:0x007ff672271800 @code=13, @name="東京都", @name_e="Tokyo">
61
78
 
62
- JpPrefecture::Prefecture.find name: "Tokyo"
63
- # => #<JpPrefecture::Prefecture:0x007fb3c2828b10 @code=13, @name="東京都", @name_e="Tokyo">
79
+ JpPrefecture::Prefecture.find name: "Tokyo"
80
+ # => #<JpPrefecture::Prefecture:0x007fb3c2828b10 @code=13, @name="東京都", @name_e="Tokyo">
64
81
 
65
- JpPrefecture::Prefecture.find name: "tokyo"
66
- # => #<JpPrefecture::Prefecture:0x007f965c0c5a40 @code=13, @name="東京都", @name_e="Tokyo">
82
+ JpPrefecture::Prefecture.find name: "tokyo"
83
+ # => #<JpPrefecture::Prefecture:0x007f965c0c5a40 @code=13, @name="東京都", @name_e="Tokyo">
84
+ ```
67
85
 
68
86
  ### 都道府県の一覧を取得
69
87
 
70
- JpPrefecture::Prefecture.all
71
- # => [#<JpPrefecture::Prefecture:0x007fd0a3d78d38 @code=1, @name="北海道", @name_e="Hokkaido">, ...]
88
+ ```ruby
89
+ JpPrefecture::Prefecture.all
90
+ # => [#<JpPrefecture::Prefecture:0x007fd0a3d78d38 @code=1, @name="北海道", @name_e="Hokkaido">, ...]
91
+ ```
72
92
 
73
93
  ### Rails(ActiveRecord) で使用する
74
94
 
@@ -76,38 +96,46 @@ JIS X 0402 で定義されている都道府県コードをベースに、
76
96
 
77
97
  app/models/place.rb:
78
98
 
79
- class Place < ActiveRecord::Base
80
- # prefecture_code:integer
99
+ ```ruby
100
+ class Place < ActiveRecord::Base
101
+ # prefecture_code:integer
81
102
 
82
- include JpPrefecture
83
- jp_prefecture :prefecture_code
84
- end
103
+ include JpPrefecture
104
+ jp_prefecture :prefecture_code
105
+ end
106
+ ```
85
107
 
86
108
  `prefecture` というメソッドが生成され、都道府県コード、都道府県名が参照できるようになります:
87
109
 
88
- place = Place.new
89
- place.prefecture_code = 13
90
- place.prefecture.name
91
- # => "東京都"
110
+ ```ruby
111
+ place = Place.new
112
+ place.prefecture_code = 13
113
+ place.prefecture.name
114
+ # => "東京都"
115
+ ```
92
116
 
93
117
  生成されるメソッド名は `method_name` というオプションで指定することができます:
94
118
 
95
- # model
96
- jp_prefecture :prefecture_code, method_name: :pref
119
+ ```ruby
120
+ # model
121
+ jp_prefecture :prefecture_code, method_name: :pref
97
122
 
98
- place = Place.new
99
- place.prefecture_code = 13
100
- place.pref.name
101
- # => "東京都"
123
+ place = Place.new
124
+ place.prefecture_code = 13
125
+ place.pref.name
126
+ # => "東京都"
127
+ ```
102
128
 
103
129
  ### テンプレートで使用する
104
130
 
105
131
  `collection_select` を使用して、都道府県のセレクトボックスを生成することができます。:
106
132
 
107
- f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name
133
+ ```ruby
134
+ f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name
108
135
 
109
- # 英語表記で出力
110
- f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name_e
136
+ # 英語表記で出力
137
+ f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name_e
138
+ ```
111
139
 
112
140
  ### マイグレーション
113
141
 
@@ -115,22 +143,26 @@ app/models/place.rb:
115
143
 
116
144
  マイグレーションのサンプル:
117
145
 
118
- class AddPrefectureCodeToPlaces < ActiveRecord::Migration
119
- def change
120
- add_column :places, :prefecture_code, :integer
121
- end
122
- end
146
+ ```ruby
147
+ class AddPrefectureCodeToPlaces < ActiveRecord::Migration
148
+ def change
149
+ add_column :places, :prefecture_code, :integer
150
+ end
151
+ end
152
+ ```
123
153
 
124
154
  ### 都道府県のマッピング情報を変更する
125
155
 
126
156
  デフォルトのマッピング情報以外のものを使用したい場合、以下のようにカスタマイズされた
127
157
  マッピングデータを指定することができます:
128
158
 
129
- custom_mapping_path = "..." # /path/to/mapping_data
159
+ ```ruby
160
+ custom_mapping_path = "..." # /path/to/mapping_data
130
161
 
131
- JpPrefecture.setup do |config|
132
- config.mapping_data = YAML.load_file custom_mapping_path
133
- end
162
+ JpPrefecture.setup do |config|
163
+ config.mapping_data = YAML.load_file custom_mapping_path
164
+ end
165
+ ```
134
166
 
135
167
  マッピングデータのフォーマットについては [prefecture.yml](https://github.com/chocoby/jp_prefecture/blob/master/data/prefecture.yml) を参考にしてください。
136
168
 
@@ -144,10 +176,8 @@ GitHub の [Issues](https://github.com/chocoby/jp_prefecture/issues) を参照
144
176
 
145
177
  ## 対象バージョン
146
178
 
147
- * Ruby: 1.9.3 / 2.0.0 以上
148
- * Rails: 3.2 系の最新
149
-
150
- Ruby 1.8.7 のサポートは [v0.1.1](https://github.com/chocoby/jp_prefecture/tree/v0.1.1) で終了しました。
179
+ * Ruby: 1.9.3 / 2.0.0
180
+ * Rails: 3.2.x / 4.0.x
151
181
 
152
182
  ## Contributing
153
183
 
data/Rakefile CHANGED
@@ -10,3 +10,61 @@ begin
10
10
  end
11
11
  rescue LoadError => e
12
12
  end
13
+
14
+ task :create_zips do
15
+ require 'csv'
16
+ require 'jp_prefecture'
17
+
18
+ file_name = 'ken_all_utf8.csv'
19
+
20
+ # download files
21
+ `wget http://www.post.japanpost.jp/zipcode/dl/kogaki/lzh/ken_all.lzh`
22
+ `lha x ken_all.lzh`
23
+ `iconv -f sjis -t utf-8 ken_all.csv > #{file_name}`
24
+ `rm ken_all.lzh`
25
+ `rm ken_all.csv`
26
+
27
+ zips = []
28
+
29
+ # read CSV
30
+ CSV.foreach(file_name, headers: false){|line| zips << [line[2],line[6]]}
31
+
32
+ # create sorted list of zips -> prefecture_code
33
+ zips = zips.collect{|zip, prefecture|[zip.to_i, JpPrefecture::Prefecture.find(name: prefecture).code]}.sort{|x,y| x[0]<=>y[0] }
34
+
35
+ # prepare calculation
36
+ ranged_zips = []
37
+ current = [ zips.first[0], zips.first[0], zips.first[1] ]
38
+
39
+ # calculate the zip ranges of each prefecture
40
+ zips.each do |zip, code|
41
+ if current[2] == code
42
+ current[1] = zip
43
+ else
44
+ ranged_zips << current
45
+ current = [zip, zip, code]
46
+ end
47
+ end
48
+
49
+ # add last prefecture
50
+ ranged_zips << current
51
+
52
+ # create prefecture hash
53
+ prefectures_to_zip = {}
54
+
55
+ ranged_zips.each do |r0, r1, code|
56
+ prefectures_to_zip[code] ||= []
57
+ prefectures_to_zip[code] << [r0,r1]
58
+ end
59
+
60
+ prefectures_to_zip = Hash[*prefectures_to_zip.sort.flatten(1)]
61
+
62
+ # save result
63
+ File.open('data/zip.yml', 'w') do |file|
64
+ file.write "# {:prefecture_code => [[from_zip_1, to_zip_1], [from_zip_2, to_zip_2], ...], ... }\n"
65
+ file.write prefectures_to_zip.to_yaml
66
+ end
67
+
68
+ # delete temporary file
69
+ `rm #{file_name}`
70
+ end
data/data/zip.yml ADDED
@@ -0,0 +1,253 @@
1
+ # {:prefecture_code => [[from_zip_1, to_zip_1], [from_zip_2, to_zip_2], ...], ... }
2
+ ---
3
+ 1:
4
+ - - 10000
5
+ - 70895
6
+ - - 400000
7
+ - 996509
8
+ 2:
9
+ - - 185501
10
+ - 185501
11
+ - - 300111
12
+ - 395346
13
+ 3:
14
+ - - 200000
15
+ - 295703
16
+ 4:
17
+ - - 9800000
18
+ - 9896941
19
+ 5:
20
+ - - 100000
21
+ - 185422
22
+ - - 185511
23
+ - 192742
24
+ 6:
25
+ - - 9900000
26
+ - 9998531
27
+ 7:
28
+ - - 9600000
29
+ - 9793204
30
+ 8:
31
+ - - 3000000
32
+ - 3114407
33
+ - - 3114501
34
+ - 3193705
35
+ 9:
36
+ - - 3114411
37
+ - 3114411
38
+ - - 3200001
39
+ - 3294425
40
+ - - 3491221
41
+ - 3491221
42
+ 10:
43
+ - - 3700000
44
+ - 3792314
45
+ - - 3840097
46
+ - 3840097
47
+ - - 3890121
48
+ - 3890121
49
+ 11:
50
+ - - 3300000
51
+ - 3491216
52
+ - - 3500001
53
+ - 3691912
54
+ 12:
55
+ - - 2600000
56
+ - 2995506
57
+ 13:
58
+ - - 1000000
59
+ - 2080035
60
+ 14:
61
+ - - 2100000
62
+ - 2591335
63
+ 15:
64
+ - - 3892261
65
+ - 3892261
66
+ - - 9400000
67
+ - 9498317
68
+ - - 9498401
69
+ - 9594636
70
+ 16:
71
+ - - 9300001
72
+ - 9390156
73
+ - - 9390231
74
+ - 9398282
75
+ 17:
76
+ - - 9200000
77
+ - 9220673
78
+ - - 9220801
79
+ - 9292379
80
+ - - 9390171
81
+ - 9390171
82
+ 18:
83
+ - - 9100001
84
+ - 9192387
85
+ - - 9220679
86
+ - 9220679
87
+ 19:
88
+ - - 4000000
89
+ - 4093867
90
+ 20:
91
+ - - 3800801
92
+ - 3840096
93
+ - - 3840301
94
+ - 3890115
95
+ - - 3890200
96
+ - 3892258
97
+ - - 3892300
98
+ - 3999601
99
+ - - 9498321
100
+ - 9498321
101
+ 21:
102
+ - - 5000000
103
+ - 5099232
104
+ 22:
105
+ - - 4100000
106
+ - 4314112
107
+ - - 4320000
108
+ - 4390037
109
+ 23:
110
+ - - 4314121
111
+ - 4314121
112
+ - - 4400001
113
+ - 4970058
114
+ - - 4980000
115
+ - 4980069
116
+ 24:
117
+ - - 4980000
118
+ - 4980000
119
+ - - 4980801
120
+ - 4980823
121
+ - - 5100000
122
+ - 5195835
123
+ - - 6471321
124
+ - 6471325
125
+ 25:
126
+ - - 5200000
127
+ - 5200363
128
+ - - 5200471
129
+ - 5291851
130
+ 26:
131
+ - - 5200461
132
+ - 5200465
133
+ - - 6000000
134
+ - 6170857
135
+ - - 6180000
136
+ - 6180000
137
+ - - 6180071
138
+ - 6293579
139
+ 27:
140
+ - - 5300000
141
+ - 5630373
142
+ - - 5640000
143
+ - 5998276
144
+ - - 6180000
145
+ - 6180000
146
+ - - 6180001
147
+ - 6180024
148
+ - - 6300271
149
+ - 6300272
150
+ 28:
151
+ - - 5630801
152
+ - 5630801
153
+ - - 6500000
154
+ - 6795654
155
+ 29:
156
+ - - 6300000
157
+ - 6300267
158
+ - - 6301101
159
+ - 6393809
160
+ - - 6471271
161
+ - 6471271
162
+ - - 6471581
163
+ - 6471584
164
+ - - 6480300
165
+ - 6480309
166
+ 30:
167
+ - - 6400000
168
+ - 6471235
169
+ - - 6471600
170
+ - 6480263
171
+ - - 6480401
172
+ - 6497216
173
+ 31:
174
+ - - 6800000
175
+ - 6840076
176
+ - - 6890101
177
+ - 6895673
178
+ 32:
179
+ - - 6840100
180
+ - 6850435
181
+ - - 6900000
182
+ - 6995637
183
+ 33:
184
+ - - 7000000
185
+ - 7193814
186
+ 34:
187
+ - - 7200001
188
+ - 7392732
189
+ 35:
190
+ - - 7400000
191
+ - 7596614
192
+ 36:
193
+ - - 7700000
194
+ - 7795453
195
+ 37:
196
+ - - 7600000
197
+ - 7692908
198
+ 38:
199
+ - - 7900001
200
+ - 7993772
201
+ 39:
202
+ - - 7800000
203
+ - 7891936
204
+ 40:
205
+ - - 8000000
206
+ - 8114343
207
+ - - 8120000
208
+ - 8160983
209
+ - - 8180000
210
+ - 8391415
211
+ - - 8710000
212
+ - 8710000
213
+ - - 8710226
214
+ - 8710226
215
+ - - 8710801
216
+ - 8710928
217
+ 41:
218
+ - - 8400001
219
+ - 8480146
220
+ - - 8490000
221
+ - 8495264
222
+ 42:
223
+ - - 8115100
224
+ - 8115757
225
+ - - 8170000
226
+ - 8172333
227
+ - - 8480401
228
+ - 8480408
229
+ - - 8500000
230
+ - 8596415
231
+ 43:
232
+ - - 8600001
233
+ - 8696405
234
+ 44:
235
+ - - 8391421
236
+ - 8391421
237
+ - - 8700001
238
+ - 8710000
239
+ - - 8710001
240
+ - 8710208
241
+ - - 8710311
242
+ - 8710716
243
+ - - 8720000
244
+ - 8797885
245
+ 45:
246
+ - - 8800000
247
+ - 8894602
248
+ 46:
249
+ - - 8900000
250
+ - 8998609
251
+ 47:
252
+ - - 9000000
253
+ - 9071801
@@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
16
16
  gem.version = JpPrefecture::VERSION
17
17
 
18
18
  gem.add_development_dependency "rake"
19
- gem.add_development_dependency "rspec", "~> 2.13.0"
19
+ gem.add_development_dependency "rspec", "~> 2.14.1"
20
20
  gem.add_development_dependency "activerecord", ">= 3.2.0"
21
21
  gem.add_development_dependency "sqlite3"
22
22
  end
@@ -3,6 +3,7 @@
3
3
  module JpPrefecture
4
4
  class Config
5
5
  attr_accessor :mapping_data
6
+ attr_accessor :zip_mapping_data
6
7
  end
7
8
  end
8
9
 
@@ -1,11 +1,12 @@
1
1
  # coding: utf-8
2
2
  require "jp_prefecture/mapping"
3
+ require "jp_prefecture/zip_mapping"
3
4
 
4
5
  module JpPrefecture
5
6
  # 都道府県のコードと名前を扱うクラス
6
7
  class Prefecture
7
8
 
8
- attr_accessor :code, :name, :name_e
9
+ attr_accessor :code, :name, :name_e, :zips
9
10
 
10
11
  # 配列から都道府県クラスを生成
11
12
  #
@@ -16,12 +17,14 @@ module JpPrefecture
16
17
  # @param pref [Integer] 都道府県コード
17
18
  # @param name [String] 都道府県名
18
19
  # @param name_e [String] 都道府県名(英語表記)
20
+ # @param zips [Array] 郵便番号の配列 (array of ranges, can be used in ARel, e.g. User.where(zip: prefecture.zips))
19
21
  def self.build(code, name, name_e)
20
22
  p = self.new
21
23
 
22
24
  p.code = code
23
25
  p.name = name
24
26
  p.name_e = name_e.capitalize
27
+ p.zips = ZipMapping.data[code]
25
28
 
26
29
  p
27
30
  end
@@ -44,6 +47,7 @@ module JpPrefecture
44
47
  # @param [Hash] args 検索条件
45
48
  # @option args [Integer] :code 都道府県コード
46
49
  # @option args [String] :name 都道府県名/英語表記の都道府県名
50
+ # @option args [Integer] :zip 郵便番号
47
51
  # @return [JpPrefecture::Prefecture] 都道府県が見つかった場合は都道府県クラス
48
52
  # @return [nil] 都道府県が見つからない場合は nil
49
53
  def self.find(args)
@@ -58,6 +62,8 @@ module JpPrefecture
58
62
  self.find_code_by_name(args[:name])
59
63
  when :code
60
64
  args[:code].to_i
65
+ when :zip
66
+ ZipMapping.code_for_zip(args[:zip].to_i)
61
67
  end
62
68
  end
63
69
 
@@ -68,7 +74,6 @@ module JpPrefecture
68
74
  self.build(code, names[:name], names[:name_e])
69
75
  end
70
76
 
71
-
72
77
  # すべての都道府県クラスを返す
73
78
  #
74
79
  # @example
@@ -1,3 +1,3 @@
1
1
  module JpPrefecture
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ require 'yaml'
3
+
4
+ module JpPrefecture
5
+ # コードと都道府県のマッピング
6
+ module ZipMapping
7
+
8
+ filepath = File.join(File.dirname(__FILE__), '../../data/zip.yml')
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)]
11
+
12
+ def self.data
13
+ JpPrefecture.config.zip_mapping_data || @data
14
+ end
15
+
16
+ def self.code_for_zip zip
17
+ self.data.select do |code, zip_ranges|
18
+ zip_ranges.find{|range|range.include? zip}
19
+ end.keys.first
20
+ end
21
+ end
22
+ end
@@ -7,6 +7,7 @@ describe JpPrefecture::Prefecture do
7
7
  it { pref.code.should eq 1 }
8
8
  it { pref.name.should eq '北海道' }
9
9
  it { pref.name_e.should eq 'Hokkaido' }
10
+ it { pref.zips.should eq [10000..70895, 400000..996509] }
10
11
  end
11
12
 
12
13
  describe '.find' do
@@ -16,6 +17,7 @@ describe JpPrefecture::Prefecture do
16
17
  it { pref.code.should eq 1 }
17
18
  it { pref.name.should eq '北海道' }
18
19
  it { pref.name_e.should eq 'Hokkaido' }
20
+ it { pref.zips.should eq [10000..70895, 400000..996509] }
19
21
  end
20
22
 
21
23
  shared_examples '都道府県が見つからない' do |arg|
@@ -79,6 +81,14 @@ describe JpPrefecture::Prefecture do
79
81
  name.should eq 'hokkaido'
80
82
  end
81
83
  end
84
+
85
+ context 'zip の場合' do
86
+ it '値が変更されないこと' do
87
+ zip = '9999999'
88
+ JpPrefecture::Prefecture.find(zip: zip)
89
+ zip.should eq '9999999'
90
+ end
91
+ end
82
92
  end
83
93
  end
84
94
 
@@ -0,0 +1,10 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe JpPrefecture::ZipMapping do
5
+ describe '.data' do
6
+ it '都道府県の数が47であること' do
7
+ described_class.data.count.should eq 47
8
+ end
9
+ end
10
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jp_prefecture
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - chocoby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-08 00:00:00.000000000 Z
11
+ date: 2013-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: 2.13.0
33
+ version: 2.14.1
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
- version: 2.13.0
40
+ version: 2.14.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: activerecord
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -83,6 +83,7 @@ files:
83
83
  - README.md
84
84
  - Rakefile
85
85
  - data/prefecture.yml
86
+ - data/zip.yml
86
87
  - jp_prefecture.gemspec
87
88
  - lib/jp_prefecture.rb
88
89
  - lib/jp_prefecture/base.rb
@@ -90,6 +91,7 @@ files:
90
91
  - lib/jp_prefecture/mapping.rb
91
92
  - lib/jp_prefecture/prefecture.rb
92
93
  - lib/jp_prefecture/version.rb
94
+ - lib/jp_prefecture/zip_mapping.rb
93
95
  - spec/base_spec.rb
94
96
  - spec/config_spec.rb
95
97
  - spec/fixtures/prefecture.yml
@@ -97,6 +99,7 @@ files:
97
99
  - spec/mapping_spec.rb
98
100
  - spec/prefecture_spec.rb
99
101
  - spec/spec_helper.rb
102
+ - spec/zip_mapping_spec.rb
100
103
  homepage: https://github.com/chocoby/jp_prefecture
101
104
  licenses: []
102
105
  metadata: {}
@@ -116,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
119
  version: '0'
117
120
  requirements: []
118
121
  rubyforge_project:
119
- rubygems_version: 2.0.3
122
+ rubygems_version: 2.0.14
120
123
  signing_key:
121
124
  specification_version: 4
122
125
  summary: Convert japan prefecture code into prefecture name
@@ -128,3 +131,4 @@ test_files:
128
131
  - spec/mapping_spec.rb
129
132
  - spec/prefecture_spec.rb
130
133
  - spec/spec_helper.rb
134
+ - spec/zip_mapping_spec.rb