jp_prefecture 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 151ed52704cd41c8b9fb1a240bd441306ef38133
4
- data.tar.gz: 661515a372f6075e08dd954c43eb5ead8cb14ebb
3
+ metadata.gz: 39053050aa747aa80a7e3375ae5de13f46430390
4
+ data.tar.gz: 9b833787a8c06c93430a03eedc4705c9736620b4
5
5
  SHA512:
6
- metadata.gz: 8a9bef0061e91d081adbe2a557e1cd768555ec21970e595ddfe443420aba9ec5877f43ad5c2406800cada9fef99094e1e5d3aa02a0e1a79db2a652e810e8f05d
7
- data.tar.gz: 38747b5b15cb93374e0c2508639fa6d43b34b5139d07ce63721a05a6f2c665f7d88f4efe60eef73bf268394fd25c8130ae82ad53c67d3477ff5f024180713782
6
+ metadata.gz: 689fb7be7b194e1706f7448324c3b3f4d3c9633c659475eee51a8b45e7c8ce5e1e74ef3aa9687eb791de059bc78f18261805b34f63e4afba11bae42a5d921290
7
+ data.tar.gz: c7e225554165eca1fccc483a6f881fccc40e4f969ca9b576f6f2a776b2b3b6c23e53c17aa12647bc44d17ff702b43ce243855651b363088bd9596c0e2dc2dc04
data/.travis.yml CHANGED
@@ -1,3 +1,4 @@
1
1
  rvm:
2
2
  - 1.9.3
3
3
  - 2.0.0
4
+ - 2.1.1
data/CHANGELOG.md CHANGED
@@ -1,10 +1,15 @@
1
+ ## 0.6.0 (Mar 10, 2014)
2
+
3
+ * 都道府県名を前方一致で検索できるようにした (PR [#11](https://github.com/chocoby/jp_prefecture/pull/11)/[@yuuna](https://github.com/yuuna))
4
+ * 都道府県名の検索ロジックを高速化
5
+
1
6
  ## 0.5.0 (Nov 30, 2013)
2
7
 
3
- * 郵便番号から都道府県を検索する機能の追加 (PR #9/@fruwe)
8
+ * 郵便番号から都道府県を検索する機能の追加 (PR [#9](https://github.com/chocoby/jp_prefecture/pull/9)/[@fruwe](https://github.com/fruwe))
4
9
 
5
10
  ## 0.4.0 (Jul 08, 2013)
6
11
 
7
- * 都道府県のマッピングを変更する機能の追加 (PR #8/@mizoR)
12
+ * 都道府県のマッピングを変更する機能の追加 (PR [#8](https://github.com/chocoby/jp_prefecture/pull/8)/[@mizoR](https://github.com/mizoR))
8
13
 
9
14
  ## 0.3.2 (Jun 12, 2013)
10
15
 
data/README.md CHANGED
@@ -81,6 +81,9 @@ JpPrefecture::Prefecture.find name: "Tokyo"
81
81
 
82
82
  JpPrefecture::Prefecture.find name: "tokyo"
83
83
  # => #<JpPrefecture::Prefecture:0x007f965c0c5a40 @code=13, @name="東京都", @name_e="Tokyo">
84
+
85
+ JpPrefecture::Prefecture.find name: "東京"
86
+ # => #<JpPrefecture::Prefecture:0x007fe8b2b6f678 @code=13, @name="東京都", @name_e="Tokyo">
84
87
  ```
85
88
 
86
89
  ### 都道府県の一覧を取得
@@ -176,7 +179,7 @@ GitHub の [Issues](https://github.com/chocoby/jp_prefecture/issues) を参照
176
179
 
177
180
  ## 対象バージョン
178
181
 
179
- * Ruby: 1.9.3 / 2.0.0
182
+ * Ruby: 1.9.3 / 2.0 / 2.1
180
183
  * Rails: 3.2.x / 4.0.x
181
184
 
182
185
  ## Contributing
data/Rakefile CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env rake
2
+ # coding: utf-8
2
3
  require "bundler/gem_tasks"
3
4
 
4
5
  task :default => [:spec]
@@ -11,6 +12,7 @@ begin
11
12
  rescue LoadError => e
12
13
  end
13
14
 
15
+ desc 'zip.yml を作成します'
14
16
  task :create_zips do
15
17
  require 'csv'
16
18
  require 'jp_prefecture'
@@ -27,10 +29,12 @@ task :create_zips do
27
29
  zips = []
28
30
 
29
31
  # read CSV
30
- CSV.foreach(file_name, headers: false){|line| zips << [line[2],line[6]]}
32
+ CSV.foreach(file_name, headers: false) { |line| zips << [line[2], line[6]] }
31
33
 
32
34
  # 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] }
35
+ zips = zips
36
+ .collect { |zip, prefecture| [zip.to_i, JpPrefecture::Prefecture.find(name: prefecture).code] }
37
+ .sort{ |x, y| x[0] <=> y[0] }
34
38
 
35
39
  # prepare calculation
36
40
  ranged_zips = []
@@ -54,7 +58,7 @@ task :create_zips do
54
58
 
55
59
  ranged_zips.each do |r0, r1, code|
56
60
  prefectures_to_zip[code] ||= []
57
- prefectures_to_zip[code] << [r0,r1]
61
+ prefectures_to_zip[code] << [r0, r1]
58
62
  end
59
63
 
60
64
  prefectures_to_zip = Hash[*prefectures_to_zip.sort.flatten(1)]
@@ -67,4 +71,4 @@ task :create_zips do
67
71
 
68
72
  # delete temporary file
69
73
  `rm #{file_name}`
70
- end
74
+ end
data/data/zip.yml CHANGED
@@ -131,8 +131,6 @@
131
131
  - - 5200461
132
132
  - 5200465
133
133
  - - 6000000
134
- - 6170857
135
- - - 6180000
136
134
  - 6180000
137
135
  - - 6180071
138
136
  - 6293579
@@ -142,8 +140,6 @@
142
140
  - - 5640000
143
141
  - 5998276
144
142
  - - 6180000
145
- - 6180000
146
- - - 6180001
147
143
  - 6180024
148
144
  - - 6300271
149
145
  - 6300272
@@ -19,14 +19,14 @@ module JpPrefecture
19
19
  # @param name_e [String] 都道府県名(英語表記)
20
20
  # @param zips [Array] 郵便番号の配列 (array of ranges, can be used in ARel, e.g. User.where(zip: prefecture.zips))
21
21
  def self.build(code, name, name_e)
22
- p = self.new
22
+ pref = self.new
23
23
 
24
- p.code = code
25
- p.name = name
26
- p.name_e = name_e.capitalize
27
- p.zips = ZipMapping.data[code]
24
+ pref.code = code
25
+ pref.name = name
26
+ pref.name_e = name_e.capitalize
27
+ pref.zips = ZipMapping.data[code]
28
28
 
29
- p
29
+ pref
30
30
  end
31
31
 
32
32
  # 都道府県を検索
@@ -39,6 +39,9 @@ module JpPrefecture
39
39
  # # 都道府県名から検索
40
40
  # JpPrefecture::Prefecture.find(name: '北海道')
41
41
  #
42
+ # # 都道府県名から検索(先頭一致)
43
+ # JpPrefecture::Prefecture.find(name: '東京')
44
+ #
42
45
  # # 英語表記の都道府県名から検索
43
46
  # JpPrefecture::Prefecture.find(name: 'Hokkaido')
44
47
  # JpPrefecture::Prefecture.find(name: 'hokkaido')
@@ -96,15 +99,23 @@ module JpPrefecture
96
99
 
97
100
  protected
98
101
 
99
- # 名前から都道府県コードを検索
102
+ # 名前から都道府県コードを先頭一致で検索
103
+ #
104
+ # @param name [String] 検索する都道府県名
105
+ # @return [Integer] 見つかった場合は都道府県コード
106
+ # @return [nil] 見つからない場合は nil
100
107
  def self.find_code_by_name(name)
101
- result = Mapping.data.select { |_, v|
102
- v.has_value?(name.downcase)
103
- }.first
108
+ name = name.downcase
104
109
 
105
- return if result.nil?
110
+ Mapping.data.each do |m|
111
+ m[1].values.each do |v|
112
+ if v.start_with?(name)
113
+ return m[0]
114
+ end
115
+ end
116
+ end
106
117
 
107
- result[0]
118
+ nil
108
119
  end
109
120
 
110
121
  end
@@ -1,3 +1,3 @@
1
1
  module JpPrefecture
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -2,7 +2,7 @@
2
2
  require 'yaml'
3
3
 
4
4
  module JpPrefecture
5
- # コードと都道府県のマッピング
5
+ # コードと郵便番号のマッピング
6
6
  module ZipMapping
7
7
 
8
8
  filepath = File.join(File.dirname(__FILE__), '../../data/zip.yml')
@@ -55,6 +55,27 @@ describe JpPrefecture::Prefecture do
55
55
  it_behaves_like "都道府県が見つかる", name: "hokkaido"
56
56
  it_behaves_like "都道府県が見つからない", name: "udon"
57
57
  end
58
+
59
+ describe '都道府県名(先頭一致)' do
60
+ let(:pref) { JpPrefecture::Prefecture.find(name: '東京') }
61
+ it { expect(pref.name).to eq '東京都' }
62
+
63
+ let(:pref2) { JpPrefecture::Prefecture.find(name: '京都') }
64
+ it { expect(pref2.name).to eq '京都府' }
65
+
66
+ context 'マッチする都道府県が複数あった場合' do
67
+ let(:pref) { JpPrefecture::Prefecture.find(name: '宮') }
68
+ it { expect(pref.name).to eq '宮城県' }
69
+ end
70
+
71
+ context 'マッチする都道府県が複数あった場合(英語表記)' do
72
+ let(:pref) { JpPrefecture::Prefecture.find(name: 'miya') }
73
+ it { expect(pref.name_e).to eq 'Miyagi' }
74
+
75
+ let(:pref2) { JpPrefecture::Prefecture.find(name: 'Miya') }
76
+ it { expect(pref2.name_e).to eq 'Miyagi' }
77
+ end
78
+ end
58
79
  end
59
80
 
60
81
  describe '渡した変数について' do
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jp_prefecture
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.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-11-30 00:00:00.000000000 Z
11
+ date: 2014-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
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
40
  version: 2.14.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: activerecord
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: 3.2.0
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: 3.2.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: sqlite3
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: Convert japan prefecture code(JIS X 0402 based) into prefecture name.
@@ -73,10 +73,10 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
- - .gitignore
77
- - .rspec
78
- - .travis.yml
79
- - .yardopts
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - ".yardopts"
80
80
  - CHANGELOG.md
81
81
  - Gemfile
82
82
  - LICENSE
@@ -109,17 +109,17 @@ require_paths:
109
109
  - lib
110
110
  required_ruby_version: !ruby/object:Gem::Requirement
111
111
  requirements:
112
- - - '>='
112
+ - - ">="
113
113
  - !ruby/object:Gem::Version
114
114
  version: '0'
115
115
  required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  requirements:
117
- - - '>='
117
+ - - ">="
118
118
  - !ruby/object:Gem::Version
119
119
  version: '0'
120
120
  requirements: []
121
121
  rubyforge_project:
122
- rubygems_version: 2.0.14
122
+ rubygems_version: 2.2.2
123
123
  signing_key:
124
124
  specification_version: 4
125
125
  summary: Convert japan prefecture code into prefecture name