easy_model 1.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.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'activemodel', '>= 3.0.0'
4
+ gem 'activerecord', '>= 3.0.0'
5
+
6
+ group :development do
7
+ gem 'rdoc', '~> 3.12'
8
+ gem 'bundler', '~> 1.0.0'
9
+ gem 'jeweler', '~> 1.8.4'
10
+ gem 'simplecov', '>= 0'
11
+ end
12
+
data/LICENSE.txt ADDED
@@ -0,0 +1,31 @@
1
+ Copyright (c) 2012, Synergy Marketing, Inc.
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions
6
+ are met:
7
+
8
+ Redistributions of source code must retain the above copyright notice,
9
+ this list of conditions and the following disclaimer.
10
+
11
+ Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+
15
+ Neither the name of the Synergy Marketing, Inc. nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
+ COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27
+ AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29
+ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30
+ DAMAGE.
31
+
data/README.rdoc ADDED
@@ -0,0 +1,79 @@
1
+ = easy_model
2
+
3
+ データベースに依存しないモデルの基本クラスや, ActiveRecord と同じ型変換を行う属性定義メソッドを提供します.
4
+
5
+ == データベースに依存しないモデル
6
+
7
+ EasyModel::Base クラスを継承することでデータベースに依存しないモデルを手軽に作成できます.
8
+ column メソッドを利用することで, ActiveRecord と同じ型変換が行われる属性を定義することができます.
9
+ 例として, 会員のログイン情報を保持するモデルは以下のように定義することができます.
10
+
11
+ #
12
+ # 会員ログインフォーム.
13
+ #
14
+ class UserLoginForm < EasyModel::Base
15
+
16
+ # 会員番号.
17
+ column :member_no, :integer
18
+ validates :member_no, :presence => true
19
+
20
+ # パスワード.
21
+ column :password, :string
22
+ validates :password, :presence => true
23
+
24
+ # ログイン状態を保持する (デフォルトは true).
25
+ column :remember, :boolean, :default => true
26
+
27
+ end
28
+
29
+ == 検索フォーム
30
+
31
+ 検索フォームを表すモデルを作成する場合は, EasyModel::SearchForm を継承すると便利です.
32
+ EasyModel::SearchForm は EasyModel::Base に検索用に便利な機能を追加したクラスです.
33
+ EasyModel::SearchForm を継承したクラスは ActiveRecord::Relation を返す scoped メソッドを定義する必要があります.
34
+
35
+ 以下に実装例を示します.
36
+
37
+ #
38
+ # 会員検索フォーム.
39
+ #
40
+ class UserSearchForm < EasyModel::Base
41
+
42
+ # 会員名.
43
+ column :name, :string
44
+
45
+ # 会員ステータス.
46
+ column :status, :integer
47
+ validates :status, :inclusion => [1, 2, 3]
48
+
49
+ #
50
+ # 検索条件を保持する ActiveRecord::Relation を返す.
51
+ #
52
+ def scoped
53
+ scoped = User.scoped
54
+
55
+ # 値が設定されている場合のみ検索条件に加える.
56
+ scoped = scoped.where(:name => name) if self.name.present?
57
+ scoped = scoped.where(:status => status) if self.status.present?
58
+
59
+ # 検索条件を含む scope
60
+ scoped
61
+ end
62
+
63
+ end
64
+
65
+ EasyModel::SearchForm は find や all などのメソッドを scoped メソッドに移譲します.
66
+ そのため, 以下のように ActiveRecord::Relation と同じインタフェースで利用することができます.
67
+
68
+ user_search_form = UserSearchForm.new(params[:user_search_form])
69
+
70
+ if user_search_form.valid?
71
+ users = user_search_form.all
72
+ else
73
+ ...
74
+ end
75
+
76
+ == Copyright
77
+
78
+ Copyright (c) 2012 Synergy Marketing, Inc. See LICENSE.txt for further details.
79
+
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ gem.name = 'easy_model'
17
+ gem.homepage = 'http://github.com/techscore/easy_model'
18
+ gem.license = 'BSD'
19
+ gem.summary = 'データベースに依存しないモデルの基本クラスや, ActiveRecord と同じ型変換を行う属性定義メソッドを提供します.'
20
+ gem.description = 'データベースに依存しないモデルの基本クラスや, ActiveRecord と同じ型変換を行う属性定義メソッドを提供します.'
21
+ gem.email = 'info-techscore@synergy101.jp'
22
+ gem.authors = ['SUZUKI Kei']
23
+ end
24
+ Jeweler::RubygemsDotOrgTasks.new
25
+
26
+ require 'rake/testtask'
27
+ Rake::TestTask.new(:test) do |test|
28
+ test.libs << 'lib' << 'test'
29
+ test.pattern = 'test/**/test_*.rb'
30
+ test.verbose = true
31
+ end
32
+
33
+ desc 'Run tests with simplecov'
34
+ task :simplecov do
35
+ ENV['COVERAGE'] = 'true'
36
+ Rake::Task['test'].execute
37
+ end
38
+
39
+ task :default => :test
40
+
41
+ # RDoc::Parser.binary? はマルチバイト文字を含むファイルを誤判定する場合があるので NKF.guess で判定するように置き換える.
42
+ require 'nkf'
43
+ require 'rdoc/task'
44
+ class << RDoc::Parser
45
+ def binary?(file)
46
+ NKF.guess(File.read(file)) == NKF::BINARY
47
+ end
48
+ end
49
+
50
+ require 'rdoc/task'
51
+ Rake::RDocTask.new do |rdoc|
52
+ version = File.exist?('VERSION') ? File.read('VERSION') : ''
53
+
54
+ rdoc.rdoc_dir = 'rdoc'
55
+ rdoc.title = "easy_model #{version}"
56
+ rdoc.rdoc_files.include('README*')
57
+ rdoc.rdoc_files.include('lib/**/*.rb')
58
+ end
59
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,68 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "easy_model"
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["SUZUKI Kei"]
12
+ s.date = "2012-07-27"
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
+ s.email = "info-techscore@synergy101.jp"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ "Gemfile",
21
+ "LICENSE.txt",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "easy_model.gemspec",
26
+ "lib/easy_model.rb",
27
+ "lib/easy_model/base.rb",
28
+ "lib/easy_model/column.rb",
29
+ "lib/easy_model/search_form.rb",
30
+ "test/easy_model/test_base.rb",
31
+ "test/easy_model/test_column.rb",
32
+ "test/easy_model/test_search_form.rb",
33
+ "test/helper.rb"
34
+ ]
35
+ s.homepage = "http://github.com/techscore/easy_model"
36
+ s.licenses = ["BSD"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = "1.8.10"
39
+ s.summary = "\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}."
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<activemodel>, [">= 3.0.0"])
46
+ s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
47
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
48
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
50
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<activemodel>, [">= 3.0.0"])
53
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
54
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
55
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
56
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
57
+ s.add_dependency(%q<simplecov>, [">= 0"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<activemodel>, [">= 3.0.0"])
61
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
62
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
63
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
64
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
65
+ s.add_dependency(%q<simplecov>, [">= 0"])
66
+ end
67
+ end
68
+
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+
3
+ require 'active_model'
4
+
5
+ #
6
+ # テーブルに依存しないモデルの基本クラス.
7
+ #
8
+ class EasyModel::Base
9
+
10
+ extend ActiveModel::Naming
11
+ include ActiveModel::Conversion
12
+ include ActiveModel::Validations
13
+ include EasyModel::Column
14
+
15
+ #
16
+ # 指定された属性値で初期化する.
17
+ #
18
+ # ==== 引数
19
+ # attributes::
20
+ # 属性値を保持する Hash.
21
+ #
22
+ def initialize(attributes=nil)
23
+ (attributes || {}).each do |name, value|
24
+ send("#{name}=", value)
25
+ end
26
+ end
27
+
28
+ #
29
+ # オブジェクトが永続化されているか判定する.
30
+ #
31
+ # ==== 戻り値
32
+ # DB に関連付かないモデルのため常に false.
33
+ #
34
+ def persisted?
35
+ false
36
+ end
37
+
38
+ end
39
+
@@ -0,0 +1,53 @@
1
+ # coding: utf-8
2
+
3
+ require 'active_record'
4
+
5
+ #
6
+ # テーブルに存在しないカラムを定義する機能を提供する.
7
+ #
8
+ # ==== 詳細
9
+ # attr_accessor による属性定義とは異なり, データ型及びデフォルト値を指定することが可能.
10
+ #
11
+ module EasyModel::Column
12
+
13
+ def self.included(base)
14
+ base.extend(EasyModel::Column::ClassMethods)
15
+ end
16
+
17
+ end
18
+
19
+ module EasyModel::Column::ClassMethods
20
+
21
+ protected
22
+
23
+ #
24
+ # テーブルに存在しないカラムを定義する.
25
+ #
26
+ # ==== 引数
27
+ # name::
28
+ # カラム名.
29
+ #
30
+ # type::
31
+ # データ型.
32
+ #
33
+ # options [:default]::
34
+ # デフォルト値.
35
+ # 省略した場合は nil.
36
+ #
37
+ # ==== 戻り値
38
+ # 定義したカラムを表す ActiveRecord::ConnectionAdapters::Column オブジェクト.
39
+ #
40
+ def column(name, type, options={})
41
+ ActiveRecord::ConnectionAdapters::Column.new(name, options[:default], type, true).tap do |column|
42
+ define_method("#{name}=") do |value|
43
+ instance_variable_set("@#{name}", column.type_cast(value))
44
+ end
45
+ define_method(name) do
46
+ instance_variable_set("@#{name}", column.default) unless instance_variable_defined?("@#{name}")
47
+ instance_variable_get("@#{name}")
48
+ end
49
+ end
50
+ end
51
+
52
+ end
53
+
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+
3
+ #
4
+ # 検索フォームの基本クラス.
5
+ #
6
+ # ==== 詳細
7
+ # ActiveRecord::Relation と同じインタフェースのメソッドをインスタンスメソッドとして持つ.
8
+ # 派生クラスは ActiveRecord::Relation を返す scoped メソッドを定義しなければならない.
9
+ # scoped メソッドの戻り値には all や exists? などの処理が delegate される.
10
+ #
11
+ class EasyModel::SearchForm < EasyModel::Base
12
+
13
+ # from active_record/querying.rb
14
+ delegate :find, :first, :first!, :last, :last!, :all, :exists?, :any?, :many?, :to => :scoped
15
+ delegate :first_or_create, :first_or_create!, :first_or_initialize, :to => :scoped
16
+ delegate :destroy, :destroy_all, :delete, :delete_all, :update, :update_all, :to => :scoped
17
+ delegate :find_each, :find_in_batches, :to => :scoped
18
+ delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins,
19
+ :where, :preload, :eager_load, :includes, :from, :lock, :readonly,
20
+ :having, :create_with, :uniq, :to => :scoped
21
+ delegate :count, :average, :minimum, :maximum, :sum, :calculate, :pluck, :to => :scoped
22
+
23
+ # from active_record/relation/delegation.rb
24
+ delegate :to_xml, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to_ary, :to => :scoped
25
+
26
+ #
27
+ # ActiveRecord::Relation を返す.
28
+ #
29
+ # ==== 詳細
30
+ # このメソッドは派生クラスによって上書きされることを前提としている.
31
+ #
32
+ # ==== 戻り値
33
+ # ActiveRecord::Relation.
34
+ #
35
+ def scoped
36
+ raise NotImplementedError, 'Must define `scoped` method.'
37
+ end
38
+
39
+ end
40
+
data/lib/easy_model.rb ADDED
@@ -0,0 +1,13 @@
1
+ # coding: utf-8
2
+
3
+ #
4
+ # データベースに依存しないモデルの基本クラスや, ActiveRecord と同じ型変換を行う属性定義メソッドを提供する.
5
+ #
6
+ module EasyModel
7
+
8
+ %w(column base search_form).each do |name|
9
+ require "easy_model/#{name}"
10
+ end
11
+
12
+ end
13
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'helper')
4
+
5
+ class EasyModel::TestBase < Test::Unit::TestCase
6
+
7
+ def test_initialize
8
+ model = Class.new(EasyModel::Base)
9
+ model.send(:column, :name, :string)
10
+ model.send(:column, :email, :string)
11
+ model.send(:column, :age, :integer)
12
+
13
+ object = model.new(:name => 'taro', :email => 'taro@example.com', :age => 20)
14
+ assert_equal 'taro', object.name
15
+ assert_equal 'taro@example.com', object.email
16
+ assert_equal 20, object.age
17
+ end
18
+
19
+ def test_persisted?
20
+ model = Class.new(EasyModel::Base)
21
+ object = model.new
22
+ assert_equal false, object.persisted?
23
+ end
24
+
25
+ end
26
+
@@ -0,0 +1,232 @@
1
+ # coding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'helper')
4
+
5
+ class EasyModel::TestColumn < Test::Unit::TestCase
6
+
7
+ def test_has_column_method
8
+ model = Class.new
9
+ model.send(:include, EasyModel::Column)
10
+ assert model.protected_methods.include?(:column)
11
+ end
12
+
13
+ def test_string_column
14
+ model = Class.new
15
+ model.send(:include, EasyModel::Column)
16
+ model.send(:column, :column_with_default, :string, :default => 'default value')
17
+ model.send(:column, :column_without_default, :string)
18
+ object = model.new
19
+
20
+ # 未代入の場合にデフォルト値が取得できること.
21
+ assert_equal 'default value', object.column_with_default
22
+ assert_nil object.column_without_default
23
+
24
+ # 代入した値が取得できること.
25
+ object.column_with_default = 'other value'
26
+ assert_equal 'other value', object.column_with_default
27
+ end
28
+
29
+ def test_text_column
30
+ model = Class.new
31
+ model.send(:include, EasyModel::Column)
32
+ model.send(:column, :column_with_default, :text, :default => 'default value')
33
+ model.send(:column, :column_without_default, :text)
34
+ object = model.new
35
+
36
+ # 未代入の場合にデフォルト値が取得できること.
37
+ assert_equal 'default value', object.column_with_default
38
+ assert_nil object.column_without_default
39
+
40
+ # 代入した値が取得できること.
41
+ object.column_with_default = 'other value'
42
+ assert_equal 'other value', object.column_with_default
43
+ end
44
+
45
+ def test_integer_column
46
+ model = Class.new
47
+ model.send(:include, EasyModel::Column)
48
+ model.send(:column, :column_with_default, :integer, :default => 777)
49
+ model.send(:column, :column_without_default, :integer)
50
+ object = model.new
51
+
52
+ # 未代入の場合にデフォルト値が取得できること.
53
+ assert_nil object.column_without_default
54
+ assert_equal 777, object.column_with_default
55
+
56
+ # 代入した値が取得できること.
57
+ object.column_with_default = 123
58
+ assert_equal 123, object.column_with_default
59
+
60
+ # 値の変換が行われること.
61
+ object.column_with_default = '789'
62
+ assert_equal 789, object.column_with_default
63
+ end
64
+
65
+ def test_float_column
66
+ model = Class.new
67
+ model.send(:include, EasyModel::Column)
68
+ model.send(:column, :column_with_default, :float, :default => 1.5)
69
+ model.send(:column, :column_without_default, :float)
70
+ object = model.new
71
+
72
+ # 未代入の場合にデフォルト値が取得できること.
73
+ assert_nil object.column_without_default
74
+ assert_equal 1.5, object.column_with_default
75
+
76
+ # 代入した値が取得できること.
77
+ object.column_with_default = 2.5
78
+ assert_equal 2.5, object.column_with_default
79
+
80
+ # 値の変換が行われること.
81
+ object.column_with_default = '3.5'
82
+ assert_equal 3.5, object.column_with_default
83
+ object.column_with_default = 3
84
+ assert_equal 3.0, object.column_with_default
85
+ end
86
+
87
+ def test_decimal_column
88
+ model = Class.new
89
+ model.send(:include, EasyModel::Column)
90
+ model.send(:column, :column_with_default, :decimal, :default => 1.5)
91
+ model.send(:column, :column_without_default, :decimal)
92
+ object = model.new
93
+
94
+ # 未代入の場合にデフォルト値が取得できること.
95
+ assert_nil object.column_without_default
96
+ assert_equal 1.5, object.column_with_default
97
+
98
+ # 代入した値が取得できること.
99
+ object.column_with_default = 2.5
100
+ assert_equal 2.5, object.column_with_default
101
+
102
+ # 値の変換が行われること.
103
+ object.column_with_default = '3.5'
104
+ assert_equal 3.5, object.column_with_default
105
+ object.column_with_default = 3
106
+ assert_equal 3.0, object.column_with_default
107
+ end
108
+
109
+ def test_datetime_column
110
+ model = Class.new
111
+ model.send(:include, EasyModel::Column)
112
+ model.send(:column, :column_with_default, :datetime, :default => Time.parse('2000-01-01 10:20:30'))
113
+ model.send(:column, :column_without_default, :datetime)
114
+ object = model.new
115
+
116
+ # 未代入の場合にデフォルト値が取得できること.
117
+ assert_nil object.column_without_default
118
+ assert_equal Time.parse('2000-01-01 10:20:30'), object.column_with_default
119
+
120
+ # 代入した値が取得できること.
121
+ object.column_with_default = Time.parse('2000-02-01 10:20:30')
122
+ assert_equal Time.parse('2000-02-01 10:20:30'), object.column_with_default
123
+
124
+ # 値の変換が行われること.
125
+ object.column_with_default = '2000-03-01 10:20:30'
126
+ assert_equal Time.parse('2000-03-01 10:20:30'), object.column_with_default
127
+ end
128
+
129
+ def test_timestamp_column
130
+ model = Class.new
131
+ model.send(:include, EasyModel::Column)
132
+ model.send(:column, :column_with_default, :timestamp, :default => Time.parse('2000-01-01 10:20:30'))
133
+ model.send(:column, :column_without_default, :timestamp)
134
+ object = model.new
135
+
136
+ # 未代入の場合にデフォルト値が取得できること.
137
+ assert_nil object.column_without_default
138
+ assert_equal Time.parse('2000-01-01 10:20:30'), object.column_with_default
139
+
140
+ # 代入した値が取得できること.
141
+ object.column_with_default = Time.parse('2000-02-01 10:20:30')
142
+ assert_equal Time.parse('2000-02-01 10:20:30'), object.column_with_default
143
+
144
+ # 値の変換が行われること.
145
+ object.column_with_default = '2000-03-01 10:20:30'
146
+ assert_equal Time.parse('2000-03-01 10:20:30'), object.column_with_default
147
+ end
148
+
149
+ def test_time_column
150
+ model = Class.new
151
+ model.send(:include, EasyModel::Column)
152
+ model.send(:column, :column_with_default, :time, :default => Time.parse('2000-01-01 10:20:30'))
153
+ model.send(:column, :column_without_default, :time)
154
+ object = model.new
155
+
156
+ # 未代入の場合にデフォルト値が取得できること.
157
+ assert_nil object.column_without_default
158
+ assert_equal 10, object.column_with_default.hour
159
+ assert_equal 20, object.column_with_default.min
160
+ assert_equal 30, object.column_with_default.sec
161
+
162
+ # 代入した値が取得できること.
163
+ object.column_with_default = Time.parse('2000-02-01 20:30:40')
164
+ assert_equal 20, object.column_with_default.hour
165
+ assert_equal 30, object.column_with_default.min
166
+ assert_equal 40, object.column_with_default.sec
167
+ end
168
+
169
+ def test_date_column
170
+ model = Class.new
171
+ model.send(:include, EasyModel::Column)
172
+ model.send(:column, :column_with_default, :date, :default => Date.parse('2000-01-01'))
173
+ model.send(:column, :column_without_default, :date)
174
+ object = model.new
175
+
176
+ # 未代入の場合にデフォルト値が取得できること.
177
+ assert_nil object.column_without_default
178
+ assert_equal Date.parse('2000-01-01'), object.column_with_default
179
+
180
+ # 代入した値が取得できること.
181
+ object.column_with_default = Date.parse('2000-02-01')
182
+ assert_equal Date.parse('2000-02-01'), object.column_with_default
183
+
184
+ # 値の変換が行われること.
185
+ object.column_with_default = '2000-03-01'
186
+ assert_equal Date.parse('2000-03-01'), object.column_with_default
187
+ end
188
+
189
+ def test_binary_column
190
+ model = Class.new
191
+ model.send(:include, EasyModel::Column)
192
+ model.send(:column, :column_with_default, :binary, :default => 'default value')
193
+ model.send(:column, :column_without_default, :binary)
194
+ object = model.new
195
+
196
+ # 未代入の場合にデフォルト値が取得できること.
197
+ assert_equal 'default value', object.column_with_default
198
+ assert_nil object.column_without_default
199
+
200
+ # 代入した値が取得できること.
201
+ object.column_with_default = 'other value'
202
+ assert_equal 'other value', object.column_with_default
203
+ end
204
+
205
+ def test_boolean_column
206
+ model = Class.new
207
+ model.send(:include, EasyModel::Column)
208
+ model.send(:column, :column_with_default, :boolean, :default => true)
209
+ model.send(:column, :column_without_default, :boolean)
210
+ object = model.new
211
+
212
+ # 未代入の場合にデフォルト値が取得できること.
213
+ assert_nil object.column_without_default
214
+ assert_equal true, object.column_with_default
215
+
216
+ # 代入した値が取得できること.
217
+ object.column_with_default = false
218
+ assert_equal false, object.column_with_default
219
+
220
+ # 値の変換が行われること.
221
+ object.column_with_default = 'true'
222
+ assert_equal true, object.column_with_default
223
+ object.column_with_default = 'false'
224
+ assert_equal false, object.column_with_default
225
+ object.column_with_default = '1'
226
+ assert_equal true, object.column_with_default
227
+ object.column_with_default = '0'
228
+ assert_equal false, object.column_with_default
229
+ end
230
+
231
+ end
232
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'helper')
4
+
5
+ class EasyModel::TestSearchForm < Test::Unit::TestCase
6
+
7
+ def test_persisted?
8
+ model = Class.new(EasyModel::SearchForm) do
9
+ def scoped
10
+ delegatee = Class.new do
11
+ def method_missing(method_name, *arguments)
12
+ "#{method_name} called"
13
+ end
14
+ end
15
+ delegatee.new
16
+ end
17
+ end
18
+ object = model.new
19
+
20
+ assert_equal 'all called', object.all
21
+ assert_equal 'order called', object.order
22
+ assert_equal 'count called', object.count
23
+ assert_equal 'to_xml called', object.to_xml
24
+ end
25
+
26
+ end
27
+
data/test/helper.rb ADDED
@@ -0,0 +1,25 @@
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ coverage_dir File.join('coverage')
5
+ end
6
+ end
7
+
8
+ require 'rubygems'
9
+ require 'bundler'
10
+ begin
11
+ Bundler.setup(:default, :development)
12
+ rescue Bundler::BundlerError => e
13
+ $stderr.puts e.message
14
+ $stderr.puts "Run `bundle install` to install missing gems"
15
+ exit e.status_code
16
+ end
17
+ require 'test/unit'
18
+
19
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
20
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
21
+ require 'easy_model'
22
+
23
+ class Test::Unit::TestCase
24
+ end
25
+
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - SUZUKI Kei
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: &84138540 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *84138540
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &84138060 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *84138060
36
+ - !ruby/object:Gem::Dependency
37
+ name: rdoc
38
+ requirement: &84136730 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.12'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *84136730
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: &84135870 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *84135870
58
+ - !ruby/object:Gem::Dependency
59
+ name: jeweler
60
+ requirement: &84738990 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.8.4
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *84738990
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: &84738160 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *84738160
80
+ description: データベースに依存しないモデルの基本クラスや, ActiveRecord と同じ型変換を行う属性定義メソッドを提供します.
81
+ email: info-techscore@synergy101.jp
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files:
85
+ - LICENSE.txt
86
+ - README.rdoc
87
+ files:
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.rdoc
91
+ - Rakefile
92
+ - VERSION
93
+ - easy_model.gemspec
94
+ - lib/easy_model.rb
95
+ - lib/easy_model/base.rb
96
+ - lib/easy_model/column.rb
97
+ - lib/easy_model/search_form.rb
98
+ - test/easy_model/test_base.rb
99
+ - test/easy_model/test_column.rb
100
+ - test/easy_model/test_search_form.rb
101
+ - test/helper.rb
102
+ homepage: http://github.com/techscore/easy_model
103
+ licenses:
104
+ - BSD
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ segments:
116
+ - 0
117
+ hash: -1049948049
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 1.8.10
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: データベースに依存しないモデルの基本クラスや, ActiveRecord と同じ型変換を行う属性定義メソッドを提供します.
130
+ test_files: []