jp_prefecture 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ Guardfile
data/Gemfile CHANGED
@@ -1,4 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in jp_prefecture.gemspec
4
3
  gemspec
data/README.md CHANGED
@@ -1,9 +1,20 @@
1
1
  # JpPrefecture
2
2
 
3
+ https://rubygems.org/gems/jp_prefecture
4
+
3
5
  ## jp_prefecture とは
4
6
 
5
7
  都道府県コードと都道府県名を変換するライブラリです。
6
- Ruby on Rails で使用することもできます。
8
+
9
+ JIS X 0402 で定義されている都道府県コードをベースに、
10
+ ゼロから始まるものはゼロを削除して使用しています。
11
+
12
+ 北海道: 01 -> 1
13
+ 東京都: 13 -> 13
14
+
15
+ 参考: [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)
16
+
17
+ また、Rails のプラグインとして使用することもできます。
7
18
 
8
19
  ## インストール方法
9
20
 
@@ -31,22 +42,20 @@ Gemfile に記述してインストールするか:
31
42
  ### 都道府県の一覧を取得
32
43
 
33
44
  JpPrefecture::Prefecture.all
34
- # => [#<JpPrefecture::Prefecture:0x007fd0a3d78d38 @code=1, @name="北海道">, ...
45
+ # => [#<JpPrefecture::Prefecture:0x007fd0a3d78d38 @code=1, @name="北海道">, ...]
35
46
 
36
47
  ### ActiveRecord で使用する
37
48
 
38
49
  `ActiveRecord::Base` を継承した Model に include し、`jp_prefecture` を呼び出すことで、
39
50
  都道府県コードを扱うことができます。
40
51
 
41
- **NOTE:** 現在、カラム名は `prefecture_code` 固定です。(変更できるようにします)
42
-
43
52
  app/models/place.rb:
44
53
 
45
54
  class Place < ActiveRecord::Base
46
- # prefecture_code:string
55
+ # prefecture_code:integer
47
56
 
48
57
  include JpPrefecture
49
- jp_prefecture
58
+ jp_prefecture :prefecture_code
50
59
  end
51
60
 
52
61
  `prefecture` というメソッドが生成され、都道府県コード、都道府県名が参照できるようになります。:
@@ -58,35 +67,22 @@ app/models/place.rb:
58
67
 
59
68
  ### テンプレートで使用する
60
69
 
61
- `collection_select` を使用して、都道府県のセレクトボックスを生成することができます。
70
+ `collection_select` を使用して、都道府県のセレクトボックスを生成することができます。:
62
71
 
63
72
  f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name
64
73
 
65
- ## 都道府県コードと都道府県名のマッピングについて
66
-
67
- JIS X 0402 で定義されている都道府県コードをベースに、
68
- ゼロから始まるものはゼロを削除して使用しています。
69
-
70
- 北海道 01 -> 1
71
- 東京都 13 -> 13
72
-
73
- 要望があれば JIS X 0402 で定義されている正規のコードもサポートするようにします。
74
-
75
- 参考: [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)
76
-
77
74
  ## ドキュメント
78
75
 
79
- [http://rdoc.info/github/chocoby/jp_prefecture/master/index](http://rdoc.info/github/chocoby/jp_prefecture/master/index)
76
+ [http://rdoc.info/github/chocoby/jp_prefecture/master/frames/index](http://rdoc.info/github/chocoby/jp_prefecture/master/frames/index)
80
77
 
81
78
  ## TODO
82
79
 
83
- * 対象カラムを指定できるようにする
84
- * 現在は `prefecture_code` 固定
85
80
  * 生成するメソッド名を指定できるようにする
86
81
  * 現在は `prefecture` 固定
87
82
  * バリデーター
88
83
  * i18n?(tokyo などの文字列に対応)
89
- * ドキュメントをちゃんと書く
84
+ * ドキュメントを書く
85
+ * JIS X 0402 で定義されている正規のコードをサポート?
90
86
 
91
87
  ## Contributing
92
88
 
@@ -16,15 +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 "yard"
20
- gem.add_development_dependency "redcarpet"
21
-
22
- gem.add_development_dependency "awesome_print"
23
-
19
+ gem.add_development_dependency "rspec", "~> 2.12.0"
24
20
  gem.add_development_dependency "activerecord", ">= 3.2.0"
25
21
  gem.add_development_dependency "sqlite3"
26
-
27
- gem.add_development_dependency "rspec", "~> 2.12.0"
28
- gem.add_development_dependency "guard-rspec", "~> 2.4.0"
29
- gem.add_development_dependency "growl-rspec", "~> 0.0.1"
30
22
  end
data/lib/jp_prefecture.rb CHANGED
@@ -1,14 +1,18 @@
1
1
  # coding: utf-8
2
- require "jp_prefecture/version"
3
- require "jp_prefecture/mapping"
4
- require "jp_prefecture/prefecture"
5
2
  require "jp_prefecture/base"
6
- require "jp_prefecture/model"
3
+ require "jp_prefecture/configuration"
4
+ require "jp_prefecture/prefecture"
5
+ require "jp_prefecture/version"
7
6
 
8
7
  module JpPrefecture
9
8
  def self.included(model_class)
9
+ model_class.extend self
10
+ end
11
+
12
+ def self.extended(model_class)
10
13
  model_class.instance_eval do
11
14
  extend Base
15
+ @jp_prefecture_config = Class.new(Configuration).new(self)
12
16
  end
13
17
  end
14
18
  end
@@ -1,8 +1,31 @@
1
1
  # coding: utf-8
2
+ require "jp_prefecture/model"
3
+
2
4
  module JpPrefecture
3
5
  module Base
4
- def jp_prefecture
6
+ # モデル内の設定を行う
7
+ #
8
+ # 第 1 引数には変換対象のカラムを指定する:
9
+ #
10
+ # class Place < ActiveRecord::Base
11
+ # include JpPrefecture
12
+ # jp_prefecture :prefecture_code
13
+ # end
14
+ #
15
+ # @param [Symbol] column_name 変換対象のカラム
16
+ def jp_prefecture(column_name, options = {})
17
+ column_name = column_name.to_sym if column_name.is_a?(String)
18
+ options.merge!(:column_name => column_name)
19
+ jp_prefecture_config.send(:set, options)
20
+
5
21
  include JpPrefecture::Model
6
22
  end
23
+
24
+ # モデルクラスの設定を返す
25
+ #
26
+ # @return [JpPrefecture::Configuration]
27
+ def jp_prefecture_config
28
+ @jp_prefecture_config
29
+ end
7
30
  end
8
31
  end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ module JpPrefecture
3
+ class Configuration
4
+ # モデルクラス
5
+ # @return ActiveRecord::Base
6
+ attr_accessor :model_class
7
+
8
+ # 変換対象のカラム
9
+ # @return [Symbol]
10
+ attr_accessor :column_name
11
+
12
+ def initialize(model_class)
13
+ @model_class = model_class
14
+ end
15
+
16
+ private
17
+
18
+ # オプションをセットする
19
+ def set(values)
20
+ values && values.each { |name, value| self.send :"#{name}=", value }
21
+ end
22
+ end
23
+ end
@@ -6,10 +6,18 @@ module JpPrefecture
6
6
  # p = Place.new
7
7
  # p.prefecture_code = 1
8
8
  # p.prefecture
9
- # # => #<JpPrefecture::Prefecture:0x007fd0a3d10f30 @code=1, @name="北海道"> >
9
+ # # => #<JpPrefecture::Prefecture:0x007fd0a3d10f30 @code=1, @name="北海道">
10
10
  module Model
11
11
  def prefecture
12
- JpPrefecture::Prefecture.find(self.prefecture_code)
12
+ column_name = jp_prefecture_config.column_name
13
+ JpPrefecture::Prefecture.find(self.send(column_name))
14
+ end
15
+
16
+ # モデルクラスの設定を返す
17
+ #
18
+ # @return [JpPrefecture::Configuration]
19
+ def jp_prefecture_config
20
+ self.class.jp_prefecture_config
13
21
  end
14
22
  end
15
23
  end
@@ -1,4 +1,5 @@
1
1
  # coding: utf-8
2
+ require "jp_prefecture/mapping"
2
3
 
3
4
  module JpPrefecture
4
5
  # 都道府県のコードと名前を扱うクラス
@@ -1,3 +1,3 @@
1
1
  module JpPrefecture
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -2,7 +2,7 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe JpPrefecture do
5
- describe '.include' do
5
+ describe '.included' do
6
6
  subject do
7
7
  Class.new(ActiveRecord::Base) do
8
8
  include JpPrefecture
@@ -11,4 +11,19 @@ describe JpPrefecture do
11
11
 
12
12
  it { should respond_to(:jp_prefecture) }
13
13
  end
14
+
15
+ describe '.extended' do
16
+ subject do
17
+ Class.new(ActiveRecord::Base) do
18
+ extend JpPrefecture
19
+ end
20
+ end
21
+
22
+ it { should respond_to(:jp_prefecture) }
23
+ end
24
+
25
+ describe 'include されていない' do
26
+ subject { Class.new(ActiveRecord::Base) }
27
+ it { should_not respond_to(:jp_prefecture) }
28
+ end
14
29
  end
@@ -0,0 +1,16 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe JpPrefecture::Base do
5
+ describe '#jp_prefecture' do
6
+ before do
7
+ @model_class = Class.new(ActiveRecord::Base) do
8
+ self.abstract_class = true
9
+ include JpPrefecture
10
+ jp_prefecture 'aiueo'
11
+ end
12
+ end
13
+
14
+ it { @model_class.jp_prefecture_config.column_name.should eq :aiueo }
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe JpPrefecture::Configuration do
5
+ before do
6
+ @model_class = Class.new(ActiveRecord::Base) do
7
+ self.abstract_class = true
8
+ end
9
+ end
10
+
11
+ describe '#initialize' do
12
+ describe 'モデルクラスについて' do
13
+ before { @config = JpPrefecture::Configuration.new(@model_class) }
14
+ it { @config.model_class.should eq @model_class }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,62 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe JpPrefecture::Model do
5
+ describe '#prefecture' do
6
+ describe 'カラム名の指定について' do
7
+ context 'prefecture_code' do
8
+ before do
9
+ class Place < ActiveRecord::Base
10
+ include JpPrefecture
11
+ jp_prefecture :prefecture_code
12
+ end
13
+
14
+ @model_class = Place.new(:prefecture_code => 1)
15
+ end
16
+
17
+ it { @model_class.prefecture.name.should eq '北海道' }
18
+ end
19
+
20
+ context 'prefecture_id' do
21
+ before do
22
+ class Place < ActiveRecord::Base
23
+ include JpPrefecture
24
+ jp_prefecture :prefecture_id
25
+ end
26
+
27
+ @model_class = Place.new(:prefecture_id => 1)
28
+ end
29
+
30
+ it { @model_class.prefecture.name.should eq '北海道' }
31
+ end
32
+ end
33
+
34
+ describe '検索について' do
35
+ before do
36
+ class Place < ActiveRecord::Base
37
+ include JpPrefecture
38
+ jp_prefecture :prefecture_code
39
+ end
40
+ end
41
+
42
+ context '都道府県が見つかった' do
43
+ before do
44
+ @model_class = Place.new(:prefecture_code => 1)
45
+ end
46
+
47
+ it { @model_class.should respond_to(:prefecture) }
48
+ it { @model_class.prefecture.should be_an_instance_of(JpPrefecture::Prefecture) }
49
+ it { @model_class.prefecture.name.should eq '北海道' }
50
+ end
51
+
52
+ context '都道府県が見つからなかった' do
53
+ before do
54
+ @model_class = Place.new(:prefecture_code => 999)
55
+ end
56
+
57
+ it { @model_class.should respond_to(:prefecture) }
58
+ it { @model_class.prefecture.should be_nil }
59
+ end
60
+ end
61
+ end
62
+ end
File without changes
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,7 @@
1
1
  # coding: utf-8
2
- require 'rubygems'
3
2
  require 'jp_prefecture'
4
-
5
3
  require "active_record"
6
4
 
7
- require 'awesome_print'
8
-
9
5
  RSpec.configure do |config|
10
6
  config.before(:suite) do
11
7
  setup_db
@@ -22,6 +18,7 @@ def setup_db
22
18
  ActiveRecord::Schema.define(:version => 1) do
23
19
  create_table :places do |t|
24
20
  t.integer :prefecture_code
21
+ t.integer :prefecture_id
25
22
  end
26
23
  end
27
24
  end
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.0.1
4
+ version: 0.1.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-02-14 00:00:00.000000000 Z
12
+ date: 2013-02-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -28,53 +28,21 @@ dependencies:
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
- name: yard
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: '0'
38
- type: :development
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
46
- - !ruby/object:Gem::Dependency
47
- name: redcarpet
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: '0'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- - !ruby/object:Gem::Dependency
63
- name: awesome_print
31
+ name: rspec
64
32
  requirement: !ruby/object:Gem::Requirement
65
33
  none: false
66
34
  requirements:
67
- - - ! '>='
35
+ - - ~>
68
36
  - !ruby/object:Gem::Version
69
- version: '0'
37
+ version: 2.12.0
70
38
  type: :development
71
39
  prerelease: false
72
40
  version_requirements: !ruby/object:Gem::Requirement
73
41
  none: false
74
42
  requirements:
75
- - - ! '>='
43
+ - - ~>
76
44
  - !ruby/object:Gem::Version
77
- version: '0'
45
+ version: 2.12.0
78
46
  - !ruby/object:Gem::Dependency
79
47
  name: activerecord
80
48
  requirement: !ruby/object:Gem::Requirement
@@ -107,54 +75,6 @@ dependencies:
107
75
  - - ! '>='
108
76
  - !ruby/object:Gem::Version
109
77
  version: '0'
110
- - !ruby/object:Gem::Dependency
111
- name: rspec
112
- requirement: !ruby/object:Gem::Requirement
113
- none: false
114
- requirements:
115
- - - ~>
116
- - !ruby/object:Gem::Version
117
- version: 2.12.0
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
- requirements:
123
- - - ~>
124
- - !ruby/object:Gem::Version
125
- version: 2.12.0
126
- - !ruby/object:Gem::Dependency
127
- name: guard-rspec
128
- requirement: !ruby/object:Gem::Requirement
129
- none: false
130
- requirements:
131
- - - ~>
132
- - !ruby/object:Gem::Version
133
- version: 2.4.0
134
- type: :development
135
- prerelease: false
136
- version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
- requirements:
139
- - - ~>
140
- - !ruby/object:Gem::Version
141
- version: 2.4.0
142
- - !ruby/object:Gem::Dependency
143
- name: growl-rspec
144
- requirement: !ruby/object:Gem::Requirement
145
- none: false
146
- requirements:
147
- - - ~>
148
- - !ruby/object:Gem::Version
149
- version: 0.0.1
150
- type: :development
151
- prerelease: false
152
- version_requirements: !ruby/object:Gem::Requirement
153
- none: false
154
- requirements:
155
- - - ~>
156
- - !ruby/object:Gem::Version
157
- version: 0.0.1
158
78
  description: Convert japan prefecture code(JIS X 0402 based) into prefecture name.
159
79
  email:
160
80
  - chocoby@gmail.com
@@ -165,20 +85,22 @@ files:
165
85
  - .gitignore
166
86
  - .rspec
167
87
  - Gemfile
168
- - Guardfile
169
88
  - LICENSE
170
89
  - README.md
171
90
  - Rakefile
172
91
  - jp_prefecture.gemspec
173
92
  - lib/jp_prefecture.rb
174
93
  - lib/jp_prefecture/base.rb
94
+ - lib/jp_prefecture/configuration.rb
175
95
  - lib/jp_prefecture/mapping.rb
176
96
  - lib/jp_prefecture/model.rb
177
97
  - lib/jp_prefecture/prefecture.rb
178
98
  - lib/jp_prefecture/version.rb
179
- - spec/active_record_spec.rb
180
99
  - spec/jp_prefecture_spec.rb
181
- - spec/prefecture_spec.rb
100
+ - spec/lib/base_spec.rb
101
+ - spec/lib/configuration_spec.rb
102
+ - spec/lib/model_spec.rb
103
+ - spec/lib/prefecture_spec.rb
182
104
  - spec/spec_helper.rb
183
105
  homepage: https://github.com/chocoby/jp_prefecture
184
106
  licenses: []
@@ -205,8 +127,10 @@ signing_key:
205
127
  specification_version: 3
206
128
  summary: Convert japan prefecture code into prefecture name
207
129
  test_files:
208
- - spec/active_record_spec.rb
209
130
  - spec/jp_prefecture_spec.rb
210
- - spec/prefecture_spec.rb
131
+ - spec/lib/base_spec.rb
132
+ - spec/lib/configuration_spec.rb
133
+ - spec/lib/model_spec.rb
134
+ - spec/lib/prefecture_spec.rb
211
135
  - spec/spec_helper.rb
212
136
  has_rdoc:
data/Guardfile DELETED
@@ -1,5 +0,0 @@
1
- guard 'rspec' do
2
- watch(%r{^spec/.+_spec\.rb$})
3
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
- watch('spec/spec_helper.rb') { "spec" }
5
- end
@@ -1,35 +0,0 @@
1
- # coding: utf-8
2
- require 'spec_helper'
3
-
4
- class Place < ActiveRecord::Base
5
- include JpPrefecture
6
- jp_prefecture
7
- end
8
-
9
- describe ActiveRecord do
10
- describe 'include されていない' do
11
- before { @klass = Class.new(ActiveRecord::Base) }
12
- it { @klass.should_not respond_to(:jp_prefecture) }
13
- end
14
-
15
- describe '普通のモデル' do
16
- context '都道府県が見つかった場合' do
17
- before do
18
- @model_class = Place.new(:prefecture_code => 1)
19
- end
20
-
21
- it { @model_class.should respond_to(:prefecture) }
22
- it { @model_class.prefecture.should be_an_instance_of(JpPrefecture::Prefecture) }
23
- it { @model_class.prefecture.name.should eq '北海道' }
24
- end
25
-
26
- context '都道府県が見つからなかった場合' do
27
- before do
28
- @model_class = Place.new(:prefecture_code => 999)
29
- end
30
-
31
- it { @model_class.should respond_to(:prefecture) }
32
- it { @model_class.prefecture.should be_nil }
33
- end
34
- end
35
- end