jp_prefecture 0.1.0 → 0.1.1

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 ADDED
@@ -0,0 +1,4 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.3
4
+ - 2.0.0
data/.yardopts ADDED
@@ -0,0 +1,2 @@
1
+ -
2
+ CHANGELOG.md
data/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ ## 0.1.1 (March 1, 2013) ##
2
+
3
+ * モデルで使用する時に、生成するメソッド名を指定できるようにした
4
+ * Configuration モデル/Model モジュールを廃止
5
+ * extend による使用を廃止
6
+ * Travis CI によるテストを行うようにした
7
+
8
+ ## 0.1.0 (February 17, 2013) ##
9
+
10
+ * モデルで使用する時に、対象のカラム名を指定するようにした
11
+
12
+ ## 0.0.1 (February 14, 2013) ##
13
+
14
+ * 都道府県の変換
15
+ * 簡単な Rails サポート
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # JpPrefecture
2
2
 
3
+ [![Build Status](https://travis-ci.org/chocoby/jp_prefecture.png?branch=master)](https://travis-ci.org/chocoby/jp_prefecture)
4
+
3
5
  https://rubygems.org/gems/jp_prefecture
4
6
 
5
7
  ## jp_prefecture とは
@@ -16,15 +18,15 @@ JIS X 0402 で定義されている都道府県コードをベースに、
16
18
 
17
19
  また、Rails のプラグインとして使用することもできます。
18
20
 
19
- ## インストール方法
21
+ ## インストール
20
22
 
21
- Gemfile に記述してインストールするか:
23
+ 以下の行を `Gemfile` に記述してから:
22
24
 
23
25
  gem 'jp_prefecture'
24
26
 
25
- $ bundle
27
+ `bundle` を実行してください。
26
28
 
27
- 手動でインストールしてください:
29
+ または、手動でインストールしてください:
28
30
 
29
31
  $ gem install jp_prefecture
30
32
 
@@ -32,6 +34,8 @@ Gemfile に記述してインストールするか:
32
34
 
33
35
  ### 都道府県コードから都道府県名を取得
34
36
 
37
+ require 'jp_prefecture'
38
+
35
39
  pref = JpPrefecture::Prefecture.find 13
36
40
  # => #<JpPrefecture::Prefecture:0x007fd0a3d43fe8 @code=13, @name="東京都">
37
41
  pref.code
@@ -44,10 +48,9 @@ Gemfile に記述してインストールするか:
44
48
  JpPrefecture::Prefecture.all
45
49
  # => [#<JpPrefecture::Prefecture:0x007fd0a3d78d38 @code=1, @name="北海道">, ...]
46
50
 
47
- ### ActiveRecord で使用する
51
+ ### Rails(ActiveRecord) で使用する
48
52
 
49
- `ActiveRecord::Base` を継承した Model に include し、`jp_prefecture` を呼び出すことで、
50
- 都道府県コードを扱うことができます。
53
+ `ActiveRecord::Base` を継承した Model で、都道府県コードを扱うことができます。
51
54
 
52
55
  app/models/place.rb:
53
56
 
@@ -58,13 +61,23 @@ app/models/place.rb:
58
61
  jp_prefecture :prefecture_code
59
62
  end
60
63
 
61
- `prefecture` というメソッドが生成され、都道府県コード、都道府県名が参照できるようになります。:
64
+ `prefecture` というメソッドが生成され、都道府県コード、都道府県名が参照できるようになります:
62
65
 
63
66
  place = Place.new
64
67
  place.prefecture_code = 13
65
68
  place.prefecture.name
66
69
  # => "東京都"
67
70
 
71
+ 生成されるメソッド名は `method_name` というオプションで指定することができます:
72
+
73
+ # model
74
+ jp_prefecture :prefecture_code, :method_name => :pref
75
+
76
+ place = Place.new
77
+ place.prefecture_code = 13
78
+ place.pref.name
79
+ # => "東京都"
80
+
68
81
  ### テンプレートで使用する
69
82
 
70
83
  `collection_select` を使用して、都道府県のセレクトボックスを生成することができます。:
@@ -77,11 +90,9 @@ app/models/place.rb:
77
90
 
78
91
  ## TODO
79
92
 
80
- * 生成するメソッド名を指定できるようにする
81
- * 現在は `prefecture` 固定
82
93
  * バリデーター
83
94
  * i18n?(tokyo などの文字列に対応)
84
- * ドキュメントを書く
95
+ * 一つのモデルで複数のカラムをサポート?
85
96
  * JIS X 0402 で定義されている正規のコードをサポート?
86
97
 
87
98
  ## Contributing
data/Rakefile CHANGED
@@ -1,2 +1,12 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+
4
+ task :default => [:spec]
5
+ begin
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = 'spec/**/*_spec.rb'
9
+ spec.rspec_opts = ['-cfs']
10
+ end
11
+ rescue LoadError => e
12
+ end
@@ -1,31 +1,41 @@
1
1
  # coding: utf-8
2
- require "jp_prefecture/model"
3
-
4
2
  module JpPrefecture
5
3
  module Base
6
- # モデル内の設定を行う
4
+ # モデル内で使用するための設定を行う
7
5
  #
8
- # 第 1 引数には変換対象のカラムを指定する:
6
+ # 変換対象のカラムを指定する:
9
7
  #
10
8
  # class Place < ActiveRecord::Base
11
9
  # include JpPrefecture
12
10
  # jp_prefecture :prefecture_code
13
11
  # end
14
12
  #
13
+ # p = Place.new
14
+ # p.prefecture_code = 1
15
+ # p.prefecture
16
+ # # => #<JpPrefecture::Prefecture:0x007fd0a3d10f30 @code=1, @name="北海道">
17
+ #
18
+ # 生成するメソッド名は method_name で指定する:
19
+ #
20
+ # class Place < ActiveRecord::Base
21
+ # include JpPrefecture
22
+ # jp_prefecture :prefecture_code, method_name: :pref
23
+ # end
24
+ #
25
+ # p = Place.new
26
+ # p.prefecture_code = 1
27
+ # p.pref
28
+ # # => #<JpPrefecture::Prefecture:0x007fcb8444dcc8 @code=1, @name="北海道">
29
+ #
15
30
  # @param [Symbol] column_name 変換対象のカラム
31
+ # @option options [Symbol] :method_name 生成するメソッド名。デフォルト: +:prefecture+
16
32
  def jp_prefecture(column_name, options = {})
17
33
  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)
34
+ method_name = options[:method_name] || :prefecture
20
35
 
21
- include JpPrefecture::Model
22
- end
23
-
24
- # モデルクラスの設定を返す
25
- #
26
- # @return [JpPrefecture::Configuration]
27
- def jp_prefecture_config
28
- @jp_prefecture_config
36
+ define_method method_name do
37
+ JpPrefecture::Prefecture.find(self.send(column_name))
38
+ end
29
39
  end
30
40
  end
31
41
  end
@@ -1,3 +1,3 @@
1
1
  module JpPrefecture
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/jp_prefecture.rb CHANGED
@@ -1,18 +1,10 @@
1
1
  # coding: utf-8
2
2
  require "jp_prefecture/base"
3
- require "jp_prefecture/configuration"
4
3
  require "jp_prefecture/prefecture"
5
4
  require "jp_prefecture/version"
6
5
 
7
6
  module JpPrefecture
8
7
  def self.included(model_class)
9
- model_class.extend self
10
- end
11
-
12
- def self.extended(model_class)
13
- model_class.instance_eval do
14
- extend Base
15
- @jp_prefecture_config = Class.new(Configuration).new(self)
16
- end
8
+ model_class.extend Base
17
9
  end
18
10
  end
data/spec/base_spec.rb ADDED
@@ -0,0 +1,81 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe JpPrefecture::Base do
5
+ describe '#jp_prefecture' do
6
+ describe 'カラム名の指定について' do
7
+ context 'prefecture_code を指定した場合' do
8
+ let(:model_class) do
9
+ klass = Class.new(ActiveRecord::Base) do
10
+ self.table_name = :places
11
+ include JpPrefecture
12
+ jp_prefecture :prefecture_code
13
+ end
14
+
15
+ klass.new(:prefecture_code => 1)
16
+ end
17
+
18
+ it 'prefecture_code のコードが変換できること' do
19
+ model_class.prefecture.name.should eq '北海道'
20
+ end
21
+ end
22
+
23
+ context 'prefecture_id を指定した場合' do
24
+ let(:model_class) do
25
+ klass = Class.new(ActiveRecord::Base) do
26
+ self.table_name = :places
27
+ include JpPrefecture
28
+ jp_prefecture :prefecture_id
29
+ end
30
+
31
+ klass.new(:prefecture_id => 1)
32
+ end
33
+
34
+ it 'prefecture_id のコードが変換できること' do
35
+ model_class.prefecture.name.should eq '北海道'
36
+ end
37
+ end
38
+ end
39
+
40
+ describe '生成するメソッド名の指定について' do
41
+ context 'prefecture_method を指定した場合' do
42
+ let(:model_class) do
43
+ klass = Class.new(ActiveRecord::Base) do
44
+ self.table_name = :places
45
+ include JpPrefecture
46
+ jp_prefecture :prefecture_code, :method_name => :prefecture_method
47
+ end
48
+
49
+ klass.new(:prefecture_code => 1)
50
+ end
51
+
52
+ it 'prefecture_method で結果が参照できること' do
53
+ model_class.prefecture_method.name.should eq '北海道'
54
+ end
55
+ end
56
+ end
57
+
58
+ describe '都道府県の検索について' do
59
+ let(:klass) do
60
+ Class.new(ActiveRecord::Base) do
61
+ self.table_name = :places
62
+ include JpPrefecture
63
+ jp_prefecture :prefecture_code
64
+ end
65
+ end
66
+
67
+ context '都道府県が見つかった場合' do
68
+ let(:model_class) { klass.new(:prefecture_code => 1) }
69
+ it { model_class.should respond_to(:prefecture) }
70
+ it { model_class.prefecture.should be_an_instance_of(JpPrefecture::Prefecture) }
71
+ it { model_class.prefecture.name.should eq '北海道' }
72
+ end
73
+
74
+ context '都道府県が見つからなかった場合' do
75
+ let(:model_class) { klass.new(:prefecture_code => 99) }
76
+ it { model_class.should respond_to(:prefecture) }
77
+ it { model_class.prefecture.should be_nil }
78
+ end
79
+ end
80
+ end
81
+ end
@@ -12,16 +12,6 @@ describe JpPrefecture do
12
12
  it { should respond_to(:jp_prefecture) }
13
13
  end
14
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
15
  describe 'include されていない' do
26
16
  subject { Class.new(ActiveRecord::Base) }
27
17
  it { should_not respond_to(:jp_prefecture) }
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe JpPrefecture::Prefecture do
5
+ describe '.build' do
6
+ let(:pref) { JpPrefecture::Prefecture.build([1, '北海道']) }
7
+ it { pref.code.should eq 1 }
8
+ it { pref.name.should eq '北海道' }
9
+ end
10
+
11
+ describe '.find' do
12
+ context '都道府県が見つかった場合' do
13
+ let(:pref) { JpPrefecture::Prefecture.find(1) }
14
+ it { pref.code.should eq 1 }
15
+ it { pref.name.should eq '北海道' }
16
+ end
17
+
18
+ context '都道府県が見つからなかった場合' do
19
+ let(:pref) { JpPrefecture::Prefecture.find(99) }
20
+ it { pref.should be_nil }
21
+ end
22
+ end
23
+
24
+ describe '.all' do
25
+ let(:prefs) { JpPrefecture::Prefecture.all }
26
+ it { prefs.first.should be_an_instance_of(JpPrefecture::Prefecture) }
27
+ it '都道府県の数が 47 であること' do
28
+ prefs.count.should eq 47
29
+ end
30
+ end
31
+ 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.1.0
4
+ version: 0.1.1
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-17 00:00:00.000000000 Z
12
+ date: 2013-02-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -84,6 +84,9 @@ extra_rdoc_files: []
84
84
  files:
85
85
  - .gitignore
86
86
  - .rspec
87
+ - .travis.yml
88
+ - .yardopts
89
+ - CHANGELOG.md
87
90
  - Gemfile
88
91
  - LICENSE
89
92
  - README.md
@@ -91,16 +94,12 @@ files:
91
94
  - jp_prefecture.gemspec
92
95
  - lib/jp_prefecture.rb
93
96
  - lib/jp_prefecture/base.rb
94
- - lib/jp_prefecture/configuration.rb
95
97
  - lib/jp_prefecture/mapping.rb
96
- - lib/jp_prefecture/model.rb
97
98
  - lib/jp_prefecture/prefecture.rb
98
99
  - lib/jp_prefecture/version.rb
100
+ - spec/base_spec.rb
99
101
  - spec/jp_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
102
+ - spec/prefecture_spec.rb
104
103
  - spec/spec_helper.rb
105
104
  homepage: https://github.com/chocoby/jp_prefecture
106
105
  licenses: []
@@ -127,10 +126,8 @@ signing_key:
127
126
  specification_version: 3
128
127
  summary: Convert japan prefecture code into prefecture name
129
128
  test_files:
129
+ - spec/base_spec.rb
130
130
  - spec/jp_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
131
+ - spec/prefecture_spec.rb
135
132
  - spec/spec_helper.rb
136
133
  has_rdoc:
@@ -1,23 +0,0 @@
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
@@ -1,23 +0,0 @@
1
- # coding: utf-8
2
- module JpPrefecture
3
- # このモジュールが include されると、prefecture メソッドを呼び出せるようになります。
4
- # prefecture メソッドを呼び出すと都道府県コードと名前が参照できます。
5
- #
6
- # p = Place.new
7
- # p.prefecture_code = 1
8
- # p.prefecture
9
- # # => #<JpPrefecture::Prefecture:0x007fd0a3d10f30 @code=1, @name="北海道">
10
- module Model
11
- def prefecture
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
21
- end
22
- end
23
- end
@@ -1,16 +0,0 @@
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
@@ -1,17 +0,0 @@
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
@@ -1,62 +0,0 @@
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
@@ -1,28 +0,0 @@
1
- # coding: utf-8
2
- require 'spec_helper'
3
-
4
- describe JpPrefecture::Prefecture do
5
- describe '.build' do
6
- before { @pref = JpPrefecture::Prefecture.build([1, '北海道']) }
7
- it { @pref.code.should eq 1 }
8
- it { @pref.name.should eq '北海道' }
9
- end
10
-
11
- describe '.find' do
12
- context '都道府県が見つかった' do
13
- before { @pref = JpPrefecture::Prefecture.find(1) }
14
- it { @pref.code.should eq 1 }
15
- it { @pref.name.should eq '北海道' }
16
- end
17
-
18
- context '都道府県が見つからなかった' do
19
- it { JpPrefecture::Prefecture.find(999).should be_nil }
20
- end
21
- end
22
-
23
- describe '.all' do
24
- before { @prefs = JpPrefecture::Prefecture.all }
25
- it { @prefs.count.should eq 47 }
26
- it { @prefs.first.should be_an_instance_of(JpPrefecture::Prefecture) }
27
- end
28
- end