class_factory 0.1.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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Pat Shaughnessy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,61 @@
1
+ = Class Factory: Factory_girl syntax for dynamically creating Ruby classes
2
+
3
+ Class Factory will dynamically create classes using a factories model similar to {factory_girl}[http://github.com/thoughtbot/factory_girl]. But instead of passing a block with model attributes into the factory definition, you pass in a migration defining the attributes of the new model class you want to create:
4
+
5
+ ClassFactory.define :person do |p|
6
+ p.string :first_name
7
+ p.string :last_name
8
+ p.integer :age
9
+ end
10
+
11
+ Now when you need a "Person" model in your tests you create one like this:
12
+
13
+ ClassFactory :person
14
+ => Person(id: integer, first_name: string, last_name: string, age: integer)
15
+
16
+ This can be useful if you're writing tests for a gem or plugin and don't want to load the entire Rails environment, or have access to existing models in a target application. By default Class Factory creates ActiveRecord model classes, but using the :super option you can create any sort of Ruby class. Class Factory also makes it easy for each of your tests to use a different variation on a target class. For example, this will delete the Person model we created above, and create a new Person model that belongs to a group:
17
+
18
+ ClassFactory :person, :class_eval => 'belongs_to :group' do |p|
19
+ p.string :first_name
20
+ p.string :middle_name
21
+ p.string :last_name
22
+ p.string :group_id
23
+ end
24
+ => Person(id: integer, first_name: string, middle_name: string, last_name: string, group_id: string)
25
+
26
+ Creating different variations of the same class can be useful if you're writing tests for a generator, plugin or some other code which has different behavior depending on what classes you run it against.
27
+
28
+ == Install
29
+
30
+ gem install class_factory
31
+
32
+ == Options
33
+
34
+ Default: create a new ActiveRecord model, along with a corresponding table in your database:
35
+ ClassFactory :person
36
+
37
+ Execute a migration on the new table specified as a block, defining the attributes of the new model class:
38
+ ClassFactory :person do |p|
39
+ p.string :first_name
40
+ p.string :last_name
41
+ p.integer :age
42
+ end
43
+
44
+ Create a class with a specified superclass (default is ActiveRecord::Base):
45
+ ClassFactory :person_array, :super => Array
46
+ If SuperClass is not a subclass of ActiveRecord::Base then Class Factory won't create a table or run a migration. You can use this to create plain Ruby object classes.
47
+
48
+ Create a class called "DifferentClass" instead of "Person:"
49
+ ClassFactory :person, :class => 'DifferentClass'
50
+
51
+ Run the given code inside the new class using class_eval:
52
+ ClassFactory :person, :class_eval => 'has_many :shoes'
53
+
54
+ Create a table with the given name, instead of a table called "people:"
55
+ ClassFactory :person, :class_eval => 'set_table_name :table_name', :table => 'table_name'
56
+
57
+ If you provide options when the factory is defined they will be applied to each class created with the factory. You can also provide options when you create a class, in which case they will override the factory options.
58
+
59
+ == Detailed examples and more information
60
+
61
+ See: {http://patshaughnessy.net/class_factory}[http://patshaughnessy.net/class_factory]
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "class_factory"
8
+ gem.summary = %Q{Class Factory: Factory_girl-like syntax for dynamically creating Ruby classes}
9
+ gem.description = %Q{Use syntax similar to factory_girl to create new ActiveRecord or other test classes (vs. instances of an existing model).}
10
+ gem.email = "pat@patshaughnessy.net"
11
+ gem.homepage = "http://patshaughnessy.net/class_factory"
12
+ gem.authors = ["Pat Shaughnessy"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "class_factory #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{class_factory}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Pat Shaughnessy"]
12
+ s.date = %q{2010-03-12}
13
+ s.description = %q{Use syntax similar to factory_girl to create new ActiveRecord or other test classes (vs. instances of an existing model).}
14
+ s.email = %q{pat@patshaughnessy.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "class_factory.gemspec",
27
+ "lib/class_factory.rb",
28
+ "lib/class_factory/class_factory.rb",
29
+ "test/helper.rb",
30
+ "test/test_active_records.rb",
31
+ "test/test_plain_objects.rb"
32
+ ]
33
+ s.homepage = %q{http://patshaughnessy.net/class_factory}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.5}
37
+ s.summary = %q{Class Factory: Factory_girl-like syntax for dynamically creating Ruby classes}
38
+ s.test_files = [
39
+ "test/helper.rb",
40
+ "test/test_active_records.rb",
41
+ "test/test_plain_objects.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
55
+ end
56
+ end
57
+
@@ -0,0 +1,7 @@
1
+ require 'active_support'
2
+ require 'active_record'
3
+ require 'class_factory/class_factory'
4
+
5
+ def ClassFactory(name, options = {}, &block)
6
+ ClassFactory.create(name, options, block)
7
+ end
@@ -0,0 +1,63 @@
1
+ class ClassFactory
2
+
3
+ class << self
4
+
5
+ attr_accessor :factories
6
+
7
+ def define(name, options = {}, &block)
8
+ factories[name] = self.new({ :name => name, :migration => block }.merge(options))
9
+ end
10
+
11
+ def create(name, options, block)
12
+ raise "No such class factory defined" if !factories.has_key?(name)
13
+ options.merge!(:migration => block) if block
14
+ factories[name].create(options)
15
+ end
16
+ end
17
+
18
+ def create(override_options)
19
+ @options = @definition.merge(override_options)
20
+ @options[:super] = ActiveRecord::Base if @options[:super].nil?
21
+ create_table if is_active_record?(@options[:super])
22
+ klass = create_class
23
+ klass.class_eval @options[:class_eval] if @options[:class_eval]
24
+ klass
25
+ end
26
+
27
+ private
28
+
29
+ def initialize(options)
30
+ @definition = options
31
+ end
32
+
33
+ def create_table
34
+ ActiveRecord::Base.connection.create_table table_name, :force => true do |table|
35
+ @options[:migration].call(table) unless @options[:migration].nil?
36
+ end
37
+ end
38
+
39
+ def create_class
40
+ Object.send(:remove_const, class_name) rescue nil
41
+ Object.const_set class_name, Class.new(@options[:super])
42
+ end
43
+
44
+ def is_active_record?(klass)
45
+ klass == ActiveRecord::Base || klass.ancestors.include?(ActiveRecord::Base)
46
+ end
47
+
48
+ def class_name
49
+ (@options[:class] || @options[:name]).to_s.camelize
50
+ end
51
+
52
+ def table_name
53
+ if @options[:table]
54
+ @options[:table]
55
+ else
56
+ (@options[:class] || @options[:name]).to_s.underscore.pluralize.to_sym
57
+ end
58
+ end
59
+
60
+ self.factories = {}
61
+
62
+ end
63
+
data/test/helper.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'active_record'
5
+
6
+ ActiveRecord::Base.establish_connection({ :adapter => 'sqlite3', :database => ':memory:' })
7
+
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
10
+ require 'class_factory'
11
+
12
+ class Test::Unit::TestCase
13
+ end
@@ -0,0 +1,124 @@
1
+ require 'helper'
2
+
3
+ class TestClassFactory < Test::Unit::TestCase
4
+
5
+ context "A class definition for a simple active record model" do
6
+ setup do
7
+ ClassFactory.define :model
8
+ end
9
+
10
+ should "create an ActiveRecord model by default" do
11
+ klass = ClassFactory :model
12
+ assert_equal Model, klass
13
+ assert_equal ActiveRecord::Base, klass.superclass
14
+ end
15
+
16
+ should "create a table for each new model with the correct name" do
17
+ ClassFactory :model
18
+ assert_nothing_raised do
19
+ ActiveRecord::Base.connection.execute('select * from models')
20
+ end
21
+ end
22
+
23
+ should "create a table for each new model with the correct name if a different class name is specified" do
24
+ ClassFactory :model, :class => 'different_class'
25
+ assert_nothing_raised do
26
+ ActiveRecord::Base.connection.execute('select * from different_classes')
27
+ end
28
+ end
29
+ end
30
+
31
+ context "A class definition for an active record model with a migration specified" do
32
+ setup do
33
+ ClassFactory.define :person do |p|
34
+ p.string :first_name
35
+ p.string :last_name
36
+ p.integer :age
37
+ end
38
+ ClassFactory :person
39
+ end
40
+
41
+ should "run the given migration on the new table" do
42
+ assert_nothing_raised do
43
+ ActiveRecord::Base.connection.execute('select first_name from people')
44
+ end
45
+ end
46
+
47
+ should "create an ActiveRecord model class that can be used in the normal way to insert and select data" do
48
+ assert_equal 0, Person.count
49
+ Person.create :first_name => 'Joe', :last_name => 'Blow', :age => 50
50
+ assert_equal 1, Person.count
51
+ assert_equal 'Joe', Person.first.first_name
52
+ assert_equal 50, Person.find_by_age(50).age
53
+ end
54
+
55
+ should "delete the table and its contents if the model class is redefined" do
56
+ Person.create :first_name => 'Joe', :last_name => 'Blow', :age => 50
57
+ assert_equal 1, Person.count
58
+ ClassFactory :person
59
+ assert_equal 0, Person.count
60
+ end
61
+ end
62
+
63
+ context "A class definition for an active record model with a certain table name specified" do
64
+ setup do
65
+ ClassFactory.define :model, :table => 'model_table' do |m|
66
+ m.string :name
67
+ end
68
+ end
69
+
70
+ should "create a table with the specified name" do
71
+ ClassFactory :model
72
+ assert_nothing_raised do
73
+ ActiveRecord::Base.connection.execute('select name from model_table')
74
+ end
75
+ end
76
+
77
+ should "allow the table name be overriden" do
78
+ ClassFactory :model, :table => 'table_for_models'
79
+ assert_nothing_raised do
80
+ ActiveRecord::Base.connection.execute('select name from table_for_models')
81
+ end
82
+ end
83
+
84
+ should "not change the original definition after overriding the table setting once" do
85
+ ClassFactory :model
86
+ ActiveRecord::Base.connection.execute('drop table model_table')
87
+ ClassFactory :model, :table => 'table_for_models'
88
+ ActiveRecord::Base.connection.execute('drop table table_for_models')
89
+ ClassFactory :model
90
+ assert_nothing_raised do
91
+ ActiveRecord::Base.connection.execute('select name from model_table')
92
+ end
93
+ end
94
+ end
95
+
96
+ context "A class definition with a default migration specified" do
97
+ setup do
98
+ ClassFactory.define :person do |p|
99
+ p.string :first_name
100
+ p.string :last_name
101
+ p.integer :age
102
+ end
103
+ end
104
+
105
+ should "allow the migration to be overriden" do
106
+ ClassFactory :person do |p|
107
+ p.string :name
108
+ p.integer :age
109
+ p.integer :group_id
110
+ end
111
+ assert_equal ["id", "name", "age", "group_id"], Person.column_names
112
+ end
113
+
114
+ should "not change the original definition after overriding the migration" do
115
+ ClassFactory :person do |p|
116
+ p.string :name
117
+ p.integer :age
118
+ p.integer :group_id
119
+ end
120
+ ClassFactory :person
121
+ assert_equal ["id", "first_name", "last_name", "age"], Person.column_names
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,127 @@
1
+ require 'helper'
2
+
3
+ class TestClassFactory < Test::Unit::TestCase
4
+
5
+ should "raise an exception for an undefined class factory" do
6
+ assert_raise RuntimeError do
7
+ ClassFactory :unknown
8
+ end
9
+ end
10
+
11
+ should "create a Ruby class with the expected name and with the specified super class" do
12
+ ClassFactory.define :plain_object, :super => Object
13
+ klass = ClassFactory :plain_object
14
+ assert_equal PlainObject, klass
15
+ assert_equal Object, klass.superclass
16
+
17
+ ClassFactory.define :array_object, :super => Array
18
+ klass = ClassFactory :array_object
19
+ assert_equal ArrayObject, klass
20
+ assert_equal Array, klass.superclass
21
+ end
22
+
23
+ context "A class factory definition for simple Ruby class" do
24
+ setup do
25
+ ClassFactory.define :plain_object, :super => Object
26
+ ClassFactory :plain_object
27
+ PlainObject.class_eval do
28
+ def some_method
29
+ 'return value'
30
+ end
31
+ end
32
+ end
33
+
34
+ should "create a Ruby class that you can add methods to" do
35
+ assert_equal 'return value', PlainObject.new.some_method
36
+ end
37
+
38
+ should "allow you to redefine a class more than once" do
39
+ assert_equal 'return value', PlainObject.new.some_method
40
+ ClassFactory :plain_object
41
+ assert_raise NoMethodError do
42
+ PlainObject.new.some_method
43
+ end
44
+ end
45
+ end
46
+
47
+ context "A class factory definition for simple Ruby class with a class_eval option specified" do
48
+ setup do
49
+ ClassFactory.define :plain_object, :super => Object, :class_eval => <<END
50
+ def some_method
51
+ 'return value'
52
+ end
53
+ END
54
+ end
55
+
56
+ should "execute that class eval code when the class is created" do
57
+ ClassFactory :plain_object
58
+ assert_equal 'return value', PlainObject.new.some_method
59
+ end
60
+
61
+ should "allow the class eval code to be overriden with different code" do
62
+ ClassFactory :plain_object, :class_eval => <<END
63
+ def some_method
64
+ 'a different return value'
65
+ end
66
+ END
67
+ assert_equal 'a different return value', PlainObject.new.some_method
68
+ end
69
+
70
+ should "not change the original definition after overriding the class_eval setting" do
71
+ ClassFactory :plain_object, :class_eval => <<END
72
+ def some_method
73
+ 'return value'
74
+ end
75
+ END
76
+ ClassFactory :plain_object
77
+ assert_equal 'return value', PlainObject.new.some_method
78
+ end
79
+ end
80
+
81
+ context "A class factory definition for simple Ruby class" do
82
+ setup do
83
+ ClassFactory.define :plain_object, :super => Object
84
+ end
85
+
86
+ should "allow you to override the super class when the class is created" do
87
+ klass = ClassFactory :plain_object, :super => Array
88
+ assert_equal PlainObject, klass
89
+ assert_equal Array, klass.superclass
90
+ end
91
+
92
+ should "not change the original definition after overriding the super class setting" do
93
+ ClassFactory :plain_object, :super => Array
94
+ klass = ClassFactory :plain_object
95
+ assert_equal PlainObject, klass
96
+ assert_equal Object, klass.superclass
97
+ end
98
+ end
99
+
100
+ context "A class factory definition for simple Ruby class with a class setting" do
101
+ setup do
102
+ ClassFactory.define :plain_object, :super => Object, :class => 'CertainClass'
103
+ end
104
+
105
+ should "create a class with the specified name" do
106
+ klass = ClassFactory :plain_object
107
+ assert_equal CertainClass, klass
108
+ end
109
+
110
+ should "allow you to override the default class name" do
111
+ klass = ClassFactory :plain_object, :class => 'DifferentClass'
112
+ assert_equal DifferentClass, klass
113
+ end
114
+
115
+ should "not change the original definition after overriding the class name setting" do
116
+ ClassFactory :plain_object, :class => 'DifferentClass'
117
+ klass = ClassFactory :plain_object
118
+ assert_equal CertainClass, klass
119
+ end
120
+
121
+ should "allow you to use a class name not in camel case" do
122
+ klass = ClassFactory :plain_object, :class => 'some_otherType_OfClass_name'
123
+ assert_equal SomeOtherTypeOfClassName, klass
124
+ end
125
+ end
126
+
127
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: class_factory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pat Shaughnessy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-03-12 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Use syntax similar to factory_girl to create new ActiveRecord or other test classes (vs. instances of an existing model).
26
+ email: pat@patshaughnessy.net
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - class_factory.gemspec
42
+ - lib/class_factory.rb
43
+ - lib/class_factory/class_factory.rb
44
+ - test/helper.rb
45
+ - test/test_active_records.rb
46
+ - test/test_plain_objects.rb
47
+ has_rdoc: true
48
+ homepage: http://patshaughnessy.net/class_factory
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.5
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: "Class Factory: Factory_girl-like syntax for dynamically creating Ruby classes"
75
+ test_files:
76
+ - test/helper.rb
77
+ - test/test_active_records.rb
78
+ - test/test_plain_objects.rb