modelizer 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ === 1.0.0 (2009-02-24)
2
+
3
+ * Birthday!
@@ -0,0 +1,8 @@
1
+ CHANGELOG.rdoc
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/modelizer.rb
6
+ rails/init.rb
7
+ test/helper.rb
8
+ test/modelizer_test.rb
@@ -0,0 +1,84 @@
1
+ = Modelizer
2
+
3
+ Need a simple, consistent way to create model instances in your Rails
4
+ tests? Use the Modelizer.
5
+
6
+ == Examples
7
+
8
+ First, define a model template. I generally do this in the unit test
9
+ for the model until I need to use the template somewhere else. As soon
10
+ as it's used in multiple places, I move it to a reopened
11
+ <tt>Test::Unit::TestCase</tt> in <tt>test/test_helper.rb</tt>.
12
+
13
+ class UserTest < ActiveSupport::TestCase
14
+
15
+ # a simple list of attributes
16
+ model_template_for User, :name => "Bob"
17
+
18
+ # or a block for lazy evaluation
19
+ model_template_for User do
20
+ { :name => "Bob", :address => addresses(:default) }
21
+ end
22
+
23
+ end
24
+
25
+ This declaration generates a bunch of instance methods for you:
26
+
27
+ * def valid_user_attributes(extras = {})
28
+ * def valid_user_attributes_without(*excluded)
29
+ * def new_user(extras = {})
30
+ * def new_user_without(*excluded)
31
+ * def create_user(extras = {})
32
+ * def create_user!(extras = {})
33
+ * def create_user_without(*excluded)
34
+ * def create_user_without!(*excluded)
35
+
36
+ It also generates a test to make sure your model template is valid.
37
+
38
+ === Using in Tests
39
+
40
+ These methods are just simple helpers for model creation:
41
+
42
+ class UserTest < ActiveSupport::TestCase
43
+ model_template_for User, :name => "Bob"
44
+
45
+ def test_pointless_stuff
46
+ assert new_user.valid?
47
+ assert !new_user_without(:name).valid?
48
+
49
+ assert !create_user.new_record?
50
+
51
+ assert_raise do
52
+ create_user_without! :name
53
+ end
54
+
55
+ assert_equal "Fred", new_user(:name => "Fred").name
56
+ end
57
+ end
58
+
59
+ == Installation
60
+
61
+ $ sudo gem install modelizer
62
+
63
+ == License
64
+
65
+ Copyright 2009 John Barnette (jbarnette@rubyforge.org)
66
+
67
+ Permission is hereby granted, free of charge, to any person obtaining
68
+ a copy of this software and associated documentation files (the
69
+ 'Software'), to deal in the Software without restriction, including
70
+ without limitation the rights to use, copy, modify, merge, publish,
71
+ distribute, sublicense, and/or sell copies of the Software, and to
72
+ permit persons to whom the Software is furnished to do so, subject to
73
+ the following conditions:
74
+
75
+ The above copyright notice and this permission notice shall be
76
+ included in all copies or substantial portions of the Software.
77
+
78
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
79
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
80
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
81
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
82
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
83
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
84
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,14 @@
1
+ require "rubygems"
2
+ require "hoe"
3
+
4
+ Hoe.new "modelizer", "1.0.0" do |p|
5
+ p.developer "John Barnette", "jbarnette@rubyforge.org"
6
+
7
+ p.url = "http://github.com/jbarnette/modelizer"
8
+ p.history_file = "CHANGELOG.rdoc"
9
+ p.readme_file = "README.rdoc"
10
+ p.extra_rdoc_files = [p.readme_file]
11
+ p.need_tar = false
12
+ p.test_globs = %w(test/**/*_test.rb)
13
+ p.testlib = :minitest
14
+ end
@@ -0,0 +1,100 @@
1
+ module Modelizer
2
+ @@cache = {}
3
+ def self.cache; @@cache end
4
+
5
+ def self.included target
6
+ target.extend ClassMethods
7
+ end
8
+
9
+ def assign_model_template_attributes model, attributes
10
+ model.send :attributes=, attributes, false
11
+ model
12
+ end
13
+
14
+ def valid_model_template_attributes klass, extras = {}
15
+ defaults, block = ::Modelizer.cache[klass]
16
+ lazy = block && instance_eval(&block)
17
+ [defaults, lazy, extras].compact.inject { |t, s| t.merge s }
18
+ end
19
+
20
+ def valid_model_template_attributes_without klass, excluded
21
+ valid_model_template_attributes(klass).delete_if do |k, v|
22
+ excluded.include? k
23
+ end
24
+ end
25
+
26
+ module ClassMethods
27
+ def model_template_for klass, defaults = {}, &block
28
+ if defaults.empty? && !block
29
+ raise "default attributes or lazy block required"
30
+ end
31
+
32
+ ::Modelizer.cache[klass] = [defaults, block]
33
+
34
+ klass = klass.name
35
+ model = klass.underscore.tr "/", "_"
36
+
37
+ module_eval <<-END, __FILE__, __LINE__ + 1
38
+ def valid_#{model}_attributes extras = {}
39
+ valid_model_template_attributes #{klass}, extras
40
+ end
41
+
42
+ def valid_#{model}_attributes_without *excluded
43
+ valid_model_template_attributes_without #{klass}, excluded
44
+ end
45
+
46
+ def new_#{model} extras = {}
47
+ assign_model_template_attributes #{klass}.new,
48
+ valid_model_template_attributes(#{klass}, extras)
49
+ end
50
+
51
+ def new_#{model}_without *excluded
52
+ assign_model_template_attributes #{klass}.new,
53
+ valid_model_template_attributes_without(#{klass}, excluded)
54
+ end
55
+
56
+ def create_#{model} extras = {}
57
+ (m = new_#{model}(extras)).save; m
58
+ end
59
+
60
+ def create_#{model}! extras = {}
61
+ (m = new_#{model}(extras)).save!; m
62
+ end
63
+
64
+ def create_#{model}_without *excluded
65
+ (m = new_#{model}_without(*excluded)).save; m
66
+ end
67
+
68
+ def create_#{model}_without! *excluded
69
+ (m = new_#{model}_without(*excluded)).save!; m
70
+ end
71
+ END
72
+
73
+ # Install a test that ensures the model template is valid. If
74
+ # the template is defined in one of the abstract test
75
+ # superclasses, generate a whole new testcase. If it's in a
76
+ # concrete test, just generate a method.
77
+
78
+ file, line = caller.first.split ":"
79
+ line = line.to_i
80
+
81
+ test =<<-END
82
+ def test_model_template_for_#{model}
83
+ assert (m = new_#{model}).valid?,
84
+ "#{klass} template is invalid: " +
85
+ m.errors.full_messages.to_sentence
86
+ end
87
+ END
88
+
89
+ if self == Test::Unit::TestCase
90
+ eval <<-END, nil, file, line - 2
91
+ class ::ModelTemplateFor#{klass}Test < ActiveSupport::TestCase
92
+ #{test}
93
+ end
94
+ END
95
+ else
96
+ module_eval test, file, line - 1
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,3 @@
1
+ class ::Test::Unit::TestCase
2
+ include Modelizer
3
+ end
@@ -0,0 +1,2 @@
1
+ require "minitest/autorun"
2
+ require "modelizer"
@@ -0,0 +1,15 @@
1
+ require "minitest/unit"
2
+ require "activesupport"
3
+ require "modelizer"
4
+
5
+ class TestModelizer < MiniTest::Unit::TestCase
6
+ def setup
7
+ @klass = Class.new
8
+ @klass.send :include, Modelizer
9
+ end
10
+
11
+ def test_adds_model_template_for_class_method
12
+ assert_includes @klass.singleton_methods.collect { |m| m.to_s },
13
+ "model_template_for"
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: modelizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - John Barnette
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-26 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.9.0
24
+ version:
25
+ description: ""
26
+ email:
27
+ - jbarnette@rubyforge.org
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - Manifest.txt
34
+ - README.rdoc
35
+ files:
36
+ - CHANGELOG.rdoc
37
+ - Manifest.txt
38
+ - README.rdoc
39
+ - Rakefile
40
+ - lib/modelizer.rb
41
+ - rails/init.rb
42
+ - test/helper.rb
43
+ - test/modelizer_test.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/jbarnette/modelizer
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --main
49
+ - README.rdoc
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project: modelizer
67
+ rubygems_version: 1.3.1
68
+ signing_key:
69
+ specification_version: 2
70
+ summary: ""
71
+ test_files:
72
+ - test/modelizer_test.rb