easy_model 1.0.5 → 2.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 21ecfa2162298fa25b4bbf17662ae0a128cddbc7
4
- data.tar.gz: 52241b8089b95c6d9c80b141c61a7c28b5023286
3
+ metadata.gz: 488a9ff8518bfe55261101a3cf8eb59db5d259af
4
+ data.tar.gz: a47ae035674368c4a3c356b2b37c7440b71fb938
5
5
  SHA512:
6
- metadata.gz: 1955d4b89055454fa57d6b1333999dbf8aa30e15cf923e9d0acf664d4ba34e59d166510924a72d0c056923abb3b95559c9dca70e9048224886d5cce459e6515a
7
- data.tar.gz: a752cf393cdd56cfd618eaf756492a6e4628c7a99667262af1223a07c94f8242705922758500540163cfe9f065dc0bca02c96aa2ce46bdf78eabe519d8b433ca
6
+ metadata.gz: e735726fc401878ff30c6d85f77d58e7a4d5346d322488aedb7ec0e03bc03a368912a9df3e0fab1ca65b748774f30847d353db10248c578cbe3f6be1dacdb853
7
+ data.tar.gz: b1b90d7f46091973c003cc1c39f8a06c5420cde4785449e4e9bfa74e721ba8f364cd17c7b39d80ddbe7e1aee9d1f82f6c4c9b47a9b90bdeee3fea28d62a117d3
data/Gemfile CHANGED
@@ -1,12 +1,16 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'activemodel', '~> 3.0'
4
- gem 'activerecord', '~> 3.0'
3
+ gem 'activemodel', '~> 4.0'
4
+ gem 'activerecord', '~> 4.0'
5
5
 
6
6
  group :development do
7
7
  gem 'rdoc', '>= 3.12'
8
8
  gem 'bundler', '>= 1.0.0'
9
9
  gem 'jeweler', '>= 1.8.4'
10
+ end
11
+
12
+ group :test do
10
13
  gem 'simplecov', '>= 0'
14
+ gem 'sqlite3'
11
15
  end
12
16
 
data/README.rdoc CHANGED
@@ -1,53 +1,23 @@
1
1
  = easy_model
2
2
 
3
- データベースに関連付かないモデルの基本クラスや,
4
- 代入時に ActiveRecord と同じ型変換が行われる属性を定義する機能を提供します.
3
+ EasyModel provides features of database independent attribute.
4
+ You can define the attribute like ActiveRecord that perform data conversion during assignment.
5
5
 
6
- == データベースに関連付かないモデル, 属性
6
+ == Define database independent attribute
7
7
 
8
- Rails3 ではネーミングルールやバリデーションを扱う機能がモジュール分けされているため,
9
- それらのモジュールを再利用することでデータベースに関連付かないモデルを作成することができます.
10
- コーディング自体は簡単ですが, いくつもあるモジュールを include するコードを何度も書くことは手間です.
8
+ First, include EasyModel to your model. Next define attribute by `column` method.
11
9
 
12
- EasyModel::Base はデータベースに関連付かないモデルの基本クラスです.
13
- 以下のように EasyModel::Base を継承すれば, そのような手間を省くことができます.
10
+ class LoginForm
14
11
 
15
- #
16
- # ログインフォーム.
17
- #
18
- class LoginForm < EasyModel::Base
19
-
20
- ...
21
-
22
- end
23
-
24
- 属性を定義する場合は, 以下のように attr_accessor ではなく column メソッドを使用します.
25
- column メソッドを使用すると, 属性の型とデフォルト値を指定することができます.
26
-
27
- #
28
- # ログインフォーム.
29
- #
30
- class LoginForm < EasyModel::Base
12
+ include EasyModel::Column
31
13
 
32
- # 会員番号.
33
14
  column :member_no, :integer
34
-
35
- validates :member_no,
36
- :presence => true,
37
- :numericality => {:only_integer => true}
38
-
39
- # パスワード.
40
15
  column :password, :string
41
-
42
- validates :password,
43
- :presence => true
44
-
45
- # ログイン状態を保持する (デフォルトは true).
46
- column :remember, :boolean, :default => true
16
+ column :remember, :boolan, default: false
47
17
 
48
18
  end
49
19
 
50
- column メソッドで定義した属性は, 代入時に ActiveRecord と同様の型変換が行われます.
20
+ When you assign a value to attribute defined by `column` method, a value is converted to attribute's data type (like ActiveRecord).
51
21
 
52
22
  login_form = LoginForm.new(:member_no => '1234567',
53
23
  :password => 'PASSWORD',
@@ -60,35 +30,27 @@ column メソッドで定義した属性は, 代入時に ActiveRecord と同様
60
30
  login_form.remember # => false
61
31
  login_form.remember_before_type_cast # => 'false'
62
32
 
63
- EasyModel::Column モジュールを include することで,
64
- EasyModel::Base を継承しないモデルでも column メソッドを使用することができます.
65
-
66
- class User < ActiveRecord::Base
67
-
68
- include EasyModel::Column
69
-
70
- column ...
33
+ == Internationalization
71
34
 
72
- end
73
-
74
- == 国際化
75
-
76
- 国際化は I18n で行われます.
77
- デフォルトでは '<locale>.easy_model' 以下がルックアップされます.
35
+ EasyModel uses I18n to internationalization.
36
+ By default, the look up key is `<locale>.easy_model`.
78
37
 
79
- ja:
38
+ en:
80
39
  easy_model:
81
40
  models:
82
- user_search_form: 会員検索フォーム
41
+ login_form: Login form
83
42
  attributes:
84
- user_search_form:
85
- name: 会員名
86
- status: ステータス
43
+ login_form:
44
+ member_no: Member number
45
+ password: Password
46
+ remember: Keep the login state.
87
47
 
88
- EasyModel::Base.i18n_scope を上書きすることで, ルックアップされるキーを変更することができます.
89
- 例えば, ActiveRecord と同じキーを参照させたい場合は, 以下のように上書きします.
48
+ By define the `i18n_scope` method, you can change the look up key.
49
+ For example, to use the look up key same as ActiveRecord:
90
50
 
91
- class UserSearchForm < EasyModel::Base
51
+ class LoginForm
52
+
53
+ include EasyModel::Column
92
54
 
93
55
  def self.i18n_scope
94
56
  :activerecord
@@ -96,45 +58,37 @@ EasyModel::Base.i18n_scope を上書きすることで, ルックアップされ
96
58
 
97
59
  end
98
60
 
99
- == 検索フォーム
61
+ == Search form
100
62
 
101
- 検索フォームを表すモデルを作成する場合は, EasyModel::SearchForm を利用すると便利です.
102
- EasyModel::SearchForm EasyModel::Base に検索用に便利な機能を追加したクラスです.
103
- EasyModel::SearchForm を継承したクラスは ActiveRecord::Relation を返す scoped メソッドを定義する必要があります.
63
+ EasyModel::SearchForm is useful to create search form model.
64
+ EasyModel::SearchForm includes EasyModel::Column, and provides useful features to searching.
65
+ Only define the `scoped` method, you can use query method same as ActiveRecord.
104
66
 
105
- 以下に実装例を示します.
67
+ For example:
106
68
 
107
- #
108
- # 会員検索フォーム.
109
- #
110
69
  class UserSearchForm < EasyModel::SearchForm
111
70
 
112
- # 会員名.
113
71
  column :name, :string
114
72
 
115
- # ステータス.
116
73
  column :status, :integer
117
74
 
118
75
  validates :status, :inclusion => [1, 2, 3]
119
76
 
120
- #
121
- # 検索条件を保持する ActiveRecord::Relation を返す.
122
- #
123
77
  def scoped
124
- scoped = User.scoped
78
+ scoped = User.all
125
79
 
126
- # 値が設定されている場合のみ検索条件に加える.
80
+ # Add to conditions if value is present.
127
81
  scoped = scoped.where(:name => name) if self.name.present?
128
82
  scoped = scoped.where(:status => status) if self.status.present?
129
83
 
130
- # 検索条件を含む scope を返す.
84
+ # Return scope including search conditions.
131
85
  scoped
132
86
  end
133
87
 
134
88
  end
135
89
 
136
- EasyModel::SearchForm find all などのメソッドを scoped メソッドに移譲します.
137
- そのため, 以下のように ActiveRecord::Relation と同じインタフェースで利用することができます.
90
+ Query methods (i.e. find, all, ...) is delegated to `scoped` method.
91
+ So, you can use the interface same as ActiveRecord::Relation.
138
92
 
139
93
  user_search_form = UserSearchForm.new(params[:user_search_form])
140
94
 
data/README_ja.rdoc ADDED
@@ -0,0 +1,104 @@
1
+ = easy_model
2
+
3
+ データベースに関連付かない属性を定義する機能を提供します.
4
+ 代入時に ActiveRecord と同様の型変換が行われる属性を定義することができます.
5
+
6
+ == データベースに関連付かない属性の定義
7
+
8
+ EasyModel::Column モジュールを include し, column メソッドで属性を定義します.
9
+
10
+ class LoginForm
11
+
12
+ include EasyModel::Column
13
+
14
+ column :member_no, :integer
15
+ column :password, :string
16
+ column :remember, :boolan, default: false
17
+
18
+ end
19
+
20
+ column メソッドで定義した属性は ActiveRecord と同様のデータ型変換が行われます.
21
+
22
+ login_form = LoginForm.new(:member_no => '1234567',
23
+ :password => 'PASSWORD',
24
+ :remember => 'false')
25
+
26
+ login_form.member_no # => 1234567
27
+ login_form.member_no_before_type_cast # => '1234567'
28
+ login_form.password # => 'PASSWORD'
29
+ login_form.password_before_type_cast # => 'PASSWORD'
30
+ login_form.remember # => false
31
+ login_form.remember_before_type_cast # => 'false'
32
+
33
+ == 国際化
34
+
35
+ 国際化は I18n で行われます.
36
+ デフォルトでは '<locale>.easy_model' 以下がルックアップされます.
37
+
38
+ ja:
39
+ easy_model:
40
+ models:
41
+ login_form: ログインフォーム
42
+ attributes:
43
+ login_form:
44
+ member_no: 会員番号
45
+ password: パスワード
46
+ remember: ログイン状態を記憶する
47
+
48
+ i18n_scope メソッドを上書きすることで, ルックアップされるキーを変更することができます.
49
+ 例えば, ActiveRecord と同じキーを参照させたい場合は, 以下のように上書きします.
50
+
51
+ class LoginForm
52
+
53
+ include EasyModel::Column
54
+
55
+ def self.i18n_scope
56
+ :activerecord
57
+ end
58
+
59
+ end
60
+
61
+ == 検索フォーム
62
+
63
+ 検索フォームを表すモデルを作成する場合は EasyModel::SearchForm を利用すると便利です.
64
+ EasyModel::SearchForm は EasyModel::Column を include した上で検索に便利な機能を追加したクラスです.
65
+ scoped メソッドを定義するだけで ActiveRecord が持つ検索形のメソッドなどを利用できるようになります.
66
+
67
+ 以下に実装例を示します.
68
+
69
+ class UserSearchForm < EasyModel::SearchForm
70
+
71
+ column :name, :string
72
+
73
+ column :status, :integer
74
+
75
+ validates :status, :inclusion => [1, 2, 3]
76
+
77
+ def scoped
78
+ scoped = User.scoped
79
+
80
+ # 値が設定されている場合のみ検索条件に加える.
81
+ scoped = scoped.where(:name => name) if self.name.present?
82
+ scoped = scoped.where(:status => status) if self.status.present?
83
+
84
+ # 検索条件を含む scope を返す.
85
+ scoped
86
+ end
87
+
88
+ end
89
+
90
+ EasyModel::SearchForm は find や all などのメソッドを scoped メソッドに移譲します.
91
+ そのため, 次のように ActiveRecord::Relation と同じインタフェースで利用することができます.
92
+
93
+ user_search_form = UserSearchForm.new(params[:user_search_form])
94
+
95
+ if user_search_form.valid?
96
+ users = user_search_form.all
97
+ else
98
+ ...
99
+ end
100
+
101
+ == Copyright
102
+
103
+ Copyright (c) 2012 Synergy Marketing, Inc. See LICENSE.txt for further details.
104
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.5
1
+ 2.0.0
data/easy_model.gemspec CHANGED
@@ -5,31 +5,35 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "easy_model"
8
- s.version = "1.0.5"
8
+ s.version = "2.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["SUZUKI Kei"]
12
- s.date = "2013-10-12"
12
+ s.date = "2013-10-14"
13
13
  s.description = "\u{30c7}\u{30fc}\u{30bf}\u{30d9}\u{30fc}\u{30b9}\u{306b}\u{4f9d}\u{5b58}\u{3057}\u{306a}\u{3044}\u{30e2}\u{30c7}\u{30eb}\u{306e}\u{57fa}\u{672c}\u{30af}\u{30e9}\u{30b9}\u{3084}, ActiveRecord \u{3068}\u{540c}\u{3058}\u{578b}\u{5909}\u{63db}\u{3092}\u{884c}\u{3046}\u{5c5e}\u{6027}\u{5b9a}\u{7fa9}\u{30e1}\u{30bd}\u{30c3}\u{30c9}\u{3092}\u{63d0}\u{4f9b}\u{3057}\u{307e}\u{3059}."
14
14
  s.email = "info-techscore@synergy101.jp"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
- "README.rdoc"
17
+ "README.rdoc",
18
+ "README_ja.rdoc"
18
19
  ]
19
20
  s.files = [
20
21
  "Gemfile",
21
22
  "LICENSE.txt",
22
23
  "README.rdoc",
24
+ "README_ja.rdoc",
23
25
  "Rakefile",
24
26
  "VERSION",
25
27
  "easy_model.gemspec",
26
28
  "lib/easy_model.rb",
27
29
  "lib/easy_model/base.rb",
28
30
  "lib/easy_model/column.rb",
31
+ "lib/easy_model/column_for_active_model.rb",
32
+ "lib/easy_model/column_for_active_record.rb",
29
33
  "lib/easy_model/search_form.rb",
30
34
  "test/easy_model/ja.yml",
31
- "test/easy_model/test_base.rb",
32
- "test/easy_model/test_column.rb",
35
+ "test/easy_model/test_column_for_active_model.rb",
36
+ "test/easy_model/test_column_for_active_record.rb",
33
37
  "test/easy_model/test_search_form.rb",
34
38
  "test/helper.rb"
35
39
  ]
@@ -43,27 +47,24 @@ Gem::Specification.new do |s|
43
47
  s.specification_version = 4
44
48
 
45
49
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
- s.add_runtime_dependency(%q<activemodel>, ["~> 3.0"])
47
- s.add_runtime_dependency(%q<activerecord>, ["~> 3.0"])
50
+ s.add_runtime_dependency(%q<activemodel>, ["~> 4.0"])
51
+ s.add_runtime_dependency(%q<activerecord>, ["~> 4.0"])
48
52
  s.add_development_dependency(%q<rdoc>, [">= 3.12"])
49
53
  s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
50
54
  s.add_development_dependency(%q<jeweler>, [">= 1.8.4"])
51
- s.add_development_dependency(%q<simplecov>, [">= 0"])
52
55
  else
53
- s.add_dependency(%q<activemodel>, ["~> 3.0"])
54
- s.add_dependency(%q<activerecord>, ["~> 3.0"])
56
+ s.add_dependency(%q<activemodel>, ["~> 4.0"])
57
+ s.add_dependency(%q<activerecord>, ["~> 4.0"])
55
58
  s.add_dependency(%q<rdoc>, [">= 3.12"])
56
59
  s.add_dependency(%q<bundler>, [">= 1.0.0"])
57
60
  s.add_dependency(%q<jeweler>, [">= 1.8.4"])
58
- s.add_dependency(%q<simplecov>, [">= 0"])
59
61
  end
60
62
  else
61
- s.add_dependency(%q<activemodel>, ["~> 3.0"])
62
- s.add_dependency(%q<activerecord>, ["~> 3.0"])
63
+ s.add_dependency(%q<activemodel>, ["~> 4.0"])
64
+ s.add_dependency(%q<activerecord>, ["~> 4.0"])
63
65
  s.add_dependency(%q<rdoc>, [">= 3.12"])
64
66
  s.add_dependency(%q<bundler>, [">= 1.0.0"])
65
67
  s.add_dependency(%q<jeweler>, [">= 1.8.4"])
66
- s.add_dependency(%q<simplecov>, [">= 0"])
67
68
  end
68
69
  end
69
70
 
@@ -1,67 +1,11 @@
1
1
  # coding: utf-8
2
2
 
3
- require 'active_model'
4
-
5
- #
6
- # テーブルに依存しないモデルの基本クラス.
7
- #
8
3
  class EasyModel::Base
9
4
 
10
- extend ActiveModel::Naming
11
- include ActiveModel::Conversion
12
- include ActiveModel::Validations
13
5
  include EasyModel::Column
14
6
 
15
- #
16
- # ロケールファイルからルックアップするときのキー.
17
- #
18
- def self.i18n_scope
19
- :easy_model
20
- end
21
-
22
- #
23
- # 指定された属性値で初期化する.
24
- #
25
- # ==== 引数
26
- # attributes::
27
- # 属性名と属性値を保持する Hash.
28
- #
29
- def initialize(attributes=nil)
30
- assign_attributes(attributes)
31
- end
32
-
33
- #
34
- # 属性を一括代入する.
35
- #
36
- # ==== 引数
37
- # new_attributes::
38
- # 属性名と属性値を保持する Hash.
39
- #
40
- def attributes=(new_attributes)
41
- assign_attributes(new_attributes)
42
- end
43
-
44
- #
45
- # 属性を一括代入する.
46
- #
47
- # ==== 引数
48
- # new_attributes::
49
- # 属性名と属性値を保持する Hash.
50
- #
51
- def assign_attributes(new_attributes)
52
- (new_attributes || {}).each do |name, value|
53
- send("#{name}=", value)
54
- end
55
- end
56
-
57
- #
58
- # オブジェクトが永続化されているか判定する.
59
- #
60
- # ==== 戻り値
61
- # DB に関連付かないモデルのため常に false.
62
- #
63
- def persisted?
64
- false
7
+ def self.inherited(base)
8
+ $stderr.puts 'DEPRECATED: EasyModel::Base is deprecated. Instead to include EasyModel::Column.'
65
9
  end
66
10
 
67
11
  end