jp_prefecture 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +4 -0
- data/README.md +9 -6
- data/jp_prefecture.gemspec +1 -1
- data/lib/jp_prefecture/base.rb +2 -2
- data/lib/jp_prefecture/mapping.rb +47 -47
- data/lib/jp_prefecture/prefecture.rb +18 -10
- data/lib/jp_prefecture/version.rb +1 -1
- data/spec/prefecture_spec.rb +3 -1
- metadata +4 -4
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# JpPrefecture
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/chocoby/jp_prefecture.png?branch=master)](https://travis-ci.org/chocoby/jp_prefecture)
|
4
|
+
[![Dependency Status](https://gemnasium.com/chocoby/jp_prefecture.png)](https://gemnasium.com/chocoby/jp_prefecture)
|
4
5
|
|
5
6
|
https://rubygems.org/gems/jp_prefecture
|
6
7
|
|
@@ -37,16 +38,18 @@ JIS X 0402 で定義されている都道府県コードをベースに、
|
|
37
38
|
require 'jp_prefecture'
|
38
39
|
|
39
40
|
pref = JpPrefecture::Prefecture.find 13
|
40
|
-
# => #<JpPrefecture::Prefecture:0x007fd0a3d43fe8 @code=13, @name="東京都">
|
41
|
+
# => #<JpPrefecture::Prefecture:0x007fd0a3d43fe8 @code=13, @name="東京都", @name_e="Tokyo">
|
41
42
|
pref.code
|
42
43
|
# => 13
|
43
44
|
pref.name
|
44
45
|
# => "東京都"
|
46
|
+
pref.name_e
|
47
|
+
# => "Tokyo"
|
45
48
|
|
46
49
|
### 都道府県の一覧を取得
|
47
50
|
|
48
51
|
JpPrefecture::Prefecture.all
|
49
|
-
# => [#<JpPrefecture::Prefecture:0x007fd0a3d78d38 @code=1, @name="北海道">, ...]
|
52
|
+
# => [#<JpPrefecture::Prefecture:0x007fd0a3d78d38 @code=1, @name="北海道", @name_e="Hokkaido">, ...]
|
50
53
|
|
51
54
|
### Rails(ActiveRecord) で使用する
|
52
55
|
|
@@ -84,16 +87,16 @@ app/models/place.rb:
|
|
84
87
|
|
85
88
|
f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name
|
86
89
|
|
90
|
+
# 英語表記
|
91
|
+
f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name_e
|
92
|
+
|
87
93
|
## ドキュメント
|
88
94
|
|
89
95
|
[http://rdoc.info/github/chocoby/jp_prefecture/master/frames/index](http://rdoc.info/github/chocoby/jp_prefecture/master/frames/index)
|
90
96
|
|
91
97
|
## TODO
|
92
98
|
|
93
|
-
|
94
|
-
* i18n?(tokyo などの文字列に対応)
|
95
|
-
* 一つのモデルで複数のカラムをサポート?
|
96
|
-
* JIS X 0402 で定義されている正規のコードをサポート?
|
99
|
+
GitHub の [Issues](https://github.com/chocoby/jp_prefecture/issues) を参照してください。
|
97
100
|
|
98
101
|
## Contributing
|
99
102
|
|
data/jp_prefecture.gemspec
CHANGED
@@ -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.
|
19
|
+
gem.add_development_dependency "rspec", "~> 2.13.0"
|
20
20
|
gem.add_development_dependency "activerecord", ">= 3.2.0"
|
21
21
|
gem.add_development_dependency "sqlite3"
|
22
22
|
end
|
data/lib/jp_prefecture/base.rb
CHANGED
@@ -13,7 +13,7 @@ module JpPrefecture
|
|
13
13
|
# p = Place.new
|
14
14
|
# p.prefecture_code = 1
|
15
15
|
# p.prefecture
|
16
|
-
# # => #<JpPrefecture::Prefecture:0x007fd0a3d10f30 @code=1, @name="北海道">
|
16
|
+
# # => #<JpPrefecture::Prefecture:0x007fd0a3d10f30 @code=1, @name="北海道", @name_e="Hokkaido">
|
17
17
|
#
|
18
18
|
# 生成するメソッド名は method_name で指定する:
|
19
19
|
#
|
@@ -25,7 +25,7 @@ module JpPrefecture
|
|
25
25
|
# p = Place.new
|
26
26
|
# p.prefecture_code = 1
|
27
27
|
# p.pref
|
28
|
-
# # => #<JpPrefecture::Prefecture:0x007fcb8444dcc8 @code=1, @name="北海道">
|
28
|
+
# # => #<JpPrefecture::Prefecture:0x007fcb8444dcc8 @code=1, @name="北海道", @name_e="Hokkaido">
|
29
29
|
#
|
30
30
|
# @param [Symbol] column_name 変換対象のカラム
|
31
31
|
# @option options [Symbol] :method_name 生成するメソッド名。デフォルト: +:prefecture+
|
@@ -5,53 +5,53 @@ module JpPrefecture
|
|
5
5
|
# JIS X 0402 で定義されている都道府県コードと都道府県名をベースに
|
6
6
|
# コードがゼロから始まるもの(09 など)を 1 ケタにしたもの
|
7
7
|
PREFECTURE_CODE_NAME = {
|
8
|
-
1 => '北海道',
|
9
|
-
2 => '青森県',
|
10
|
-
3 => '岩手県',
|
11
|
-
4 => '宮城県',
|
12
|
-
5 => '秋田県',
|
13
|
-
6 => '山形県',
|
14
|
-
7 => '福島県',
|
15
|
-
8 => '茨城県',
|
16
|
-
9 => '栃木県',
|
17
|
-
10 => '群馬県',
|
18
|
-
11 => '埼玉県',
|
19
|
-
12 => '千葉県',
|
20
|
-
13 => '東京都',
|
21
|
-
14 => '神奈川県',
|
22
|
-
15 => '新潟県',
|
23
|
-
16 => '富山県',
|
24
|
-
17 => '石川県',
|
25
|
-
18 => '福井県',
|
26
|
-
19 => '山梨県',
|
27
|
-
20 => '長野県',
|
28
|
-
21 => '岐阜県',
|
29
|
-
22 => '静岡県',
|
30
|
-
23 => '愛知県',
|
31
|
-
24 => '三重県',
|
32
|
-
25 => '滋賀県',
|
33
|
-
26 => '京都府',
|
34
|
-
27 => '大阪府',
|
35
|
-
28 => '兵庫県',
|
36
|
-
29 => '奈良県',
|
37
|
-
30 => '和歌山県',
|
38
|
-
31 => '鳥取県',
|
39
|
-
32 => '島根県',
|
40
|
-
33 => '岡山県',
|
41
|
-
34 => '広島県',
|
42
|
-
35 => '山口県',
|
43
|
-
36 => '徳島県',
|
44
|
-
37 => '香川県',
|
45
|
-
38 => '愛媛県',
|
46
|
-
39 => '高知県',
|
47
|
-
40 => '福岡県',
|
48
|
-
41 => '佐賀県',
|
49
|
-
42 => '長崎県',
|
50
|
-
43 => '熊本県',
|
51
|
-
44 => '大分県',
|
52
|
-
45 => '宮崎県',
|
53
|
-
46 => '鹿児島県',
|
54
|
-
47 => '沖縄県'
|
8
|
+
1 => { name: '北海道', name_e: 'Hokkaido' },
|
9
|
+
2 => { name: '青森県', name_e: 'Aomori' },
|
10
|
+
3 => { name: '岩手県', name_e: 'Iwate' },
|
11
|
+
4 => { name: '宮城県', name_e: 'Miyagi' },
|
12
|
+
5 => { name: '秋田県', name_e: 'Akita' },
|
13
|
+
6 => { name: '山形県', name_e: 'Yamagata' },
|
14
|
+
7 => { name: '福島県', name_e: 'Fukushima' },
|
15
|
+
8 => { name: '茨城県', name_e: 'Ibaraki' },
|
16
|
+
9 => { name: '栃木県', name_e: 'Tochigi' },
|
17
|
+
10 => { name: '群馬県', name_e: 'Gunma' },
|
18
|
+
11 => { name: '埼玉県', name_e: 'Saitama' },
|
19
|
+
12 => { name: '千葉県', name_e: 'Chiba' },
|
20
|
+
13 => { name: '東京都', name_e: 'Tokyo' },
|
21
|
+
14 => { name: '神奈川県', name_e: 'Kanagawa' },
|
22
|
+
15 => { name: '新潟県', name_e: 'Niigata' },
|
23
|
+
16 => { name: '富山県', name_e: 'Toyama' },
|
24
|
+
17 => { name: '石川県', name_e: 'Ishikawa' },
|
25
|
+
18 => { name: '福井県', name_e: 'Fukui' },
|
26
|
+
19 => { name: '山梨県', name_e: 'Yamanashi' },
|
27
|
+
20 => { name: '長野県', name_e: 'Nagano' },
|
28
|
+
21 => { name: '岐阜県', name_e: 'Gifu' },
|
29
|
+
22 => { name: '静岡県', name_e: 'Shizuoka' },
|
30
|
+
23 => { name: '愛知県', name_e: 'Aichi' },
|
31
|
+
24 => { name: '三重県', name_e: 'Mie' },
|
32
|
+
25 => { name: '滋賀県', name_e: 'Shiga' },
|
33
|
+
26 => { name: '京都府', name_e: 'Kyoto' },
|
34
|
+
27 => { name: '大阪府', name_e: 'Osaka' },
|
35
|
+
28 => { name: '兵庫県', name_e: 'Hyogo' },
|
36
|
+
29 => { name: '奈良県', name_e: 'Nara' },
|
37
|
+
30 => { name: '和歌山県', name_e: 'Wakayama' },
|
38
|
+
31 => { name: '鳥取県', name_e: 'Tottori' },
|
39
|
+
32 => { name: '島根県', name_e: 'Shimane' },
|
40
|
+
33 => { name: '岡山県', name_e: 'Okayama' },
|
41
|
+
34 => { name: '広島県', name_e: 'Hiroshima' },
|
42
|
+
35 => { name: '山口県', name_e: 'Yamaguchi' },
|
43
|
+
36 => { name: '徳島県', name_e: 'Tokushima' },
|
44
|
+
37 => { name: '香川県', name_e: 'Kagawa' },
|
45
|
+
38 => { name: '愛媛県', name_e: 'Ehime' },
|
46
|
+
39 => { name: '高知県', name_e: 'Kochi' },
|
47
|
+
40 => { name: '福岡県', name_e: 'Fukuoka' },
|
48
|
+
41 => { name: '佐賀県', name_e: 'Saga' },
|
49
|
+
42 => { name: '長崎県', name_e: 'Nagasaki' },
|
50
|
+
43 => { name: '熊本県', name_e: 'Kumamoto' },
|
51
|
+
44 => { name: '大分県', name_e: 'Oita' },
|
52
|
+
45 => { name: '宮崎県', name_e: 'Miyazaki' },
|
53
|
+
46 => { name: '鹿児島県', name_e: 'Kagoshima' },
|
54
|
+
47 => { name: '沖縄県', name_e: 'Okinawa' },
|
55
55
|
}
|
56
56
|
end
|
57
57
|
end
|
@@ -6,19 +6,23 @@ module JpPrefecture
|
|
6
6
|
class Prefecture
|
7
7
|
include JpPrefecture::Mapping
|
8
8
|
|
9
|
-
attr_accessor :code, :name
|
9
|
+
attr_accessor :code, :name, :name_e
|
10
10
|
|
11
11
|
# 配列から都道府県クラスを生成
|
12
12
|
#
|
13
13
|
# @example
|
14
14
|
# # コード/名前から都道府県クラスを生成
|
15
|
-
# JpPrefecture::Prefecture.build
|
15
|
+
# JpPrefecture::Prefecture.build 1, '北海道', 'Hokkaido'
|
16
16
|
#
|
17
|
-
# @param pref [
|
18
|
-
|
17
|
+
# @param pref [Integer] 都道府県コード
|
18
|
+
# @param name [String] 都道府県名
|
19
|
+
# @param name_e [String] 都道府県名(英語表記)
|
20
|
+
def self.build(code, name, name_e)
|
19
21
|
p = self.new
|
20
|
-
|
21
|
-
p.
|
22
|
+
|
23
|
+
p.code = code
|
24
|
+
p.name = name
|
25
|
+
p.name_e = name_e
|
22
26
|
|
23
27
|
p
|
24
28
|
end
|
@@ -32,10 +36,10 @@ module JpPrefecture
|
|
32
36
|
# @return [JpPrefecture::Prefecture] 都道府県が見つかった場合は都道府県クラス
|
33
37
|
# @return [nil] 都道府県が見つからない場合は nil
|
34
38
|
def self.find(code)
|
35
|
-
|
36
|
-
return nil unless
|
39
|
+
names = PREFECTURE_CODE_NAME[code]
|
40
|
+
return nil unless names
|
37
41
|
|
38
|
-
self.build(
|
42
|
+
self.build(code, names[:name], names[:name_e])
|
39
43
|
end
|
40
44
|
|
41
45
|
|
@@ -48,10 +52,14 @@ module JpPrefecture
|
|
48
52
|
# # collection_select で選択肢を生成
|
49
53
|
# f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name
|
50
54
|
#
|
55
|
+
# # collection_select で選択肢を生成(英語表記)
|
56
|
+
# f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name_e
|
57
|
+
#
|
51
58
|
# @return [Array] 都道府県クラスの配列
|
52
59
|
def self.all
|
53
60
|
PREFECTURE_CODE_NAME.map do |pref|
|
54
|
-
|
61
|
+
names = pref[1]
|
62
|
+
self.build(pref[0], names[:name], names[:name_e])
|
55
63
|
end
|
56
64
|
end
|
57
65
|
end
|
data/spec/prefecture_spec.rb
CHANGED
@@ -3,9 +3,10 @@ require 'spec_helper'
|
|
3
3
|
|
4
4
|
describe JpPrefecture::Prefecture do
|
5
5
|
describe '.build' do
|
6
|
-
let(:pref) { JpPrefecture::Prefecture.build(
|
6
|
+
let(:pref) { JpPrefecture::Prefecture.build(1, '北海道', 'Hokkaido') }
|
7
7
|
it { pref.code.should eq 1 }
|
8
8
|
it { pref.name.should eq '北海道' }
|
9
|
+
it { pref.name_e.should eq 'Hokkaido' }
|
9
10
|
end
|
10
11
|
|
11
12
|
describe '.find' do
|
@@ -13,6 +14,7 @@ describe JpPrefecture::Prefecture do
|
|
13
14
|
let(:pref) { JpPrefecture::Prefecture.find(1) }
|
14
15
|
it { pref.code.should eq 1 }
|
15
16
|
it { pref.name.should eq '北海道' }
|
17
|
+
it { pref.name_e.should eq 'Hokkaido' }
|
16
18
|
end
|
17
19
|
|
18
20
|
context '都道府県が見つからなかった場合' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jp_prefecture
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-06-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -34,7 +34,7 @@ dependencies:
|
|
34
34
|
requirements:
|
35
35
|
- - ~>
|
36
36
|
- !ruby/object:Gem::Version
|
37
|
-
version: 2.
|
37
|
+
version: 2.13.0
|
38
38
|
type: :development
|
39
39
|
prerelease: false
|
40
40
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -42,7 +42,7 @@ dependencies:
|
|
42
42
|
requirements:
|
43
43
|
- - ~>
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
version: 2.
|
45
|
+
version: 2.13.0
|
46
46
|
- !ruby/object:Gem::Dependency
|
47
47
|
name: activerecord
|
48
48
|
requirement: !ruby/object:Gem::Requirement
|