jp_prefecture 0.2.0 → 0.3.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.
data/.travis.yml CHANGED
@@ -1,4 +1,3 @@
1
1
  rvm:
2
- - 1.8.7
3
2
  - 1.9.3
4
3
  - 2.0.0
data/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
+ ## 0.3.0 (June 11, 2013) ##
2
+
3
+ * 都道府県名(英語表記含む)から都道府県を検索できるようにした
4
+ * 都道府県名からの検索と同様の方法で、都道府県コードを検索できるようにした
5
+ * String 型の都道府県コードに対応
6
+
1
7
  ## 0.2.0 (June 9, 2013) ##
2
8
 
3
9
  * 英語表記を追加
10
+ * Ruby 1.8.7 のサポートを終了
4
11
 
5
12
  ## 0.1.1 (March 1, 2013) ##
6
13
 
data/README.md CHANGED
@@ -33,10 +33,14 @@ JIS X 0402 で定義されている都道府県コードをベースに、
33
33
 
34
34
  ## 使い方
35
35
 
36
- ### 都道府県コードから都道府県名を取得
36
+ ### ライブラリの読み込み
37
37
 
38
38
  require 'jp_prefecture'
39
39
 
40
+ ### 都道府県コードから都道府県を検索
41
+
42
+ 単純に都道府県コードを渡すと、都道府県コードから都道府県を検索します:
43
+
40
44
  pref = JpPrefecture::Prefecture.find 13
41
45
  # => #<JpPrefecture::Prefecture:0x007fd0a3d43fe8 @code=13, @name="東京都", @name_e="Tokyo">
42
46
  pref.code
@@ -46,6 +50,21 @@ JIS X 0402 で定義されている都道府県コードをベースに、
46
50
  pref.name_e
47
51
  # => "Tokyo"
48
52
 
53
+ 以下のように渡すことも可能です:
54
+
55
+ JpPrefecture::Prefecture.find code: 13
56
+
57
+ ### 都道府県名から都道府県を検索
58
+
59
+ JpPrefecture::Prefecture.find name: "東京都"
60
+ # => #<JpPrefecture::Prefecture:0x007ff672271800 @code=13, @name="東京都", @name_e="Tokyo">
61
+
62
+ JpPrefecture::Prefecture.find name: "Tokyo"
63
+ # => #<JpPrefecture::Prefecture:0x007fb3c2828b10 @code=13, @name="東京都", @name_e="Tokyo">
64
+
65
+ JpPrefecture::Prefecture.find name: "tokyo"
66
+ # => #<JpPrefecture::Prefecture:0x007f965c0c5a40 @code=13, @name="東京都", @name_e="Tokyo">
67
+
49
68
  ### 都道府県の一覧を取得
50
69
 
51
70
  JpPrefecture::Prefecture.all
@@ -74,7 +93,7 @@ app/models/place.rb:
74
93
  生成されるメソッド名は `method_name` というオプションで指定することができます:
75
94
 
76
95
  # model
77
- jp_prefecture :prefecture_code, :method_name => :pref
96
+ jp_prefecture :prefecture_code, method_name: :pref
78
97
 
79
98
  place = Place.new
80
99
  place.prefecture_code = 13
@@ -87,9 +106,21 @@ app/models/place.rb:
87
106
 
88
107
  f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name
89
108
 
90
- # 英語表記
109
+ # 英語表記で出力
91
110
  f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name_e
92
111
 
112
+ ### マイグレーション
113
+
114
+ カラムのタイプは `integer` か `string` で作成してください。
115
+
116
+ マイグレーションのサンプル:
117
+
118
+ class AddPrefectureCodeToPlaces < ActiveRecord::Migration
119
+ def change
120
+ add_column :places, :prefecture_code, :integer
121
+ end
122
+ end
123
+
93
124
  ## ドキュメント
94
125
 
95
126
  [http://rdoc.info/github/chocoby/jp_prefecture/master/frames/index](http://rdoc.info/github/chocoby/jp_prefecture/master/frames/index)
@@ -98,6 +129,13 @@ app/models/place.rb:
98
129
 
99
130
  GitHub の [Issues](https://github.com/chocoby/jp_prefecture/issues) を参照してください。
100
131
 
132
+ ## 対象バージョン
133
+
134
+ * Ruby: 1.9.3 / 2.0.0 以上
135
+ * Rails: 3.2 以上
136
+
137
+ Ruby 1.8.7 のサポートは [v0.1.1](https://github.com/chocoby/jp_prefecture/tree/v0.1.1) で終了しました。
138
+
101
139
  ## Contributing
102
140
 
103
141
  1. Fork it
@@ -12,7 +12,7 @@ module JpPrefecture
12
12
  #
13
13
  # @example
14
14
  # # コード/名前から都道府県クラスを生成
15
- # JpPrefecture::Prefecture.build 1, '北海道', 'Hokkaido'
15
+ # JpPrefecture::Prefecture.build(1, '北海道', 'Hokkaido')
16
16
  #
17
17
  # @param pref [Integer] 都道府県コード
18
18
  # @param name [String] 都道府県名
@@ -27,17 +27,44 @@ module JpPrefecture
27
27
  p
28
28
  end
29
29
 
30
- # 都道府県コードから都道府県を検索
30
+ # 都道府県を検索
31
31
  #
32
32
  # @example
33
- # JpPrefecture::Prefecture.find 1
33
+ # # 都道府県コードから検索
34
+ # JpPrefecture::Prefecture.find(1)
35
+ # JpPrefecture::Prefecture.find(code: 1)
34
36
  #
35
- # @param code [Integer] 都道府県コード
37
+ # # 都道府県名から検索
38
+ # JpPrefecture::Prefecture.find(name: '北海道')
39
+ #
40
+ # # 英語表記の都道府県名から検索
41
+ # JpPrefecture::Prefecture.find(name: 'Hokkaido')
42
+ # JpPrefecture::Prefecture.find(name: 'hokkaido')
43
+ #
44
+ # @param args [Integer] 都道府県コード
45
+ # @param [Hash] args 検索条件
46
+ # @option args [Integer] :code 都道府県コード
47
+ # @option args [String] :name 都道府県名/英語表記の都道府県名
36
48
  # @return [JpPrefecture::Prefecture] 都道府県が見つかった場合は都道府県クラス
37
49
  # @return [nil] 都道府県が見つからない場合は nil
38
- def self.find(code)
50
+ def self.find(args)
51
+ return if args.nil?
52
+
53
+ if args.is_a?(Integer) || args.is_a?(String)
54
+ code = args.to_i
55
+ else
56
+ code =
57
+ case args.keys[0]
58
+ when :name
59
+ self.find_code_by_name(args[:name])
60
+ when :code
61
+ args[:code].to_i
62
+ end
63
+ end
64
+
39
65
  names = PREFECTURE_CODE_NAME[code]
40
- return nil unless names
66
+
67
+ return unless names
41
68
 
42
69
  self.build(code, names[:name], names[:name_e])
43
70
  end
@@ -62,5 +89,18 @@ module JpPrefecture
62
89
  self.build(pref[0], names[:name], names[:name_e])
63
90
  end
64
91
  end
92
+
93
+ protected
94
+
95
+ # 名前から都道府県コードを検索
96
+ def self.find_code_by_name(name)
97
+ name.capitalize!
98
+
99
+ result = PREFECTURE_CODE_NAME.select { |_, v| v.has_value?(name) }.first
100
+ return if result.nil?
101
+
102
+ result[0]
103
+ end
104
+
65
105
  end
66
106
  end
@@ -1,3 +1,3 @@
1
1
  module JpPrefecture
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -10,17 +10,48 @@ describe JpPrefecture::Prefecture do
10
10
  end
11
11
 
12
12
  describe '.find' do
13
- context '都道府県が見つかった場合' do
14
- let(:pref) { JpPrefecture::Prefecture.find(1) }
13
+ shared_examples "都道府県が見つかる" do |arg|
14
+ let(:pref) { JpPrefecture::Prefecture.find(arg) }
15
15
  it { pref.code.should eq 1 }
16
16
  it { pref.name.should eq '北海道' }
17
17
  it { pref.name_e.should eq 'Hokkaido' }
18
18
  end
19
19
 
20
- context '都道府県が見つからなかった場合' do
21
- let(:pref) { JpPrefecture::Prefecture.find(99) }
20
+ shared_examples '都道府県が見つからない' do |arg|
21
+ let(:pref) { JpPrefecture::Prefecture.find(arg) }
22
22
  it { pref.should be_nil }
23
23
  end
24
+
25
+ describe '都道府県コード' do
26
+ it_behaves_like "都道府県が見つかる", 1
27
+ it_behaves_like "都道府県が見つからない", 99
28
+ it_behaves_like "都道府県が見つかる", "1"
29
+ it_behaves_like "都道府県が見つかる", "01"
30
+ it_behaves_like "都道府県が見つからない", "99"
31
+ end
32
+
33
+ describe '都道府県コード(キーワード引数)' do
34
+ it_behaves_like "都道府県が見つかる", code: 1
35
+ it_behaves_like "都道府県が見つからない", code: 99
36
+ it_behaves_like "都道府県が見つかる", code: "1"
37
+ it_behaves_like "都道府県が見つかる", code: "01"
38
+ it_behaves_like "都道府県が見つからない", code: "99"
39
+ end
40
+
41
+ describe '都道府県名' do
42
+ it_behaves_like "都道府県が見つかる", name: "北海道"
43
+ it_behaves_like "都道府県が見つからない", name: "うどん県"
44
+ end
45
+
46
+ describe '都道府県名(英語表記)' do
47
+ it_behaves_like "都道府県が見つかる", name: "Hokkaido"
48
+ it_behaves_like "都道府県が見つからない", name: "Udon"
49
+ end
50
+
51
+ describe '都道府県名(英語表記-小文字)' do
52
+ it_behaves_like "都道府県が見つかる", name: "hokkaido"
53
+ it_behaves_like "都道府県が見つからない", name: "udon"
54
+ end
24
55
  end
25
56
 
26
57
  describe '.all' 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.2.0
4
+ version: 0.3.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-06-09 00:00:00.000000000 Z
12
+ date: 2013-06-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -130,4 +130,3 @@ test_files:
130
130
  - spec/jp_prefecture_spec.rb
131
131
  - spec/prefecture_spec.rb
132
132
  - spec/spec_helper.rb
133
- has_rdoc: