spawn 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.markdown +89 -0
- data/Rakefile +26 -0
- data/lib/clown.rb +14 -0
- data/test/active_record_test.rb +44 -0
- data/test/all_test.rb +117 -0
- data/test/sequel_test.rb +41 -0
- metadata +59 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Michel Martens for CitrusByte (http://www.citrusbyte.com)
|
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.markdown
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
Clown
|
2
|
+
=======
|
3
|
+
|
4
|
+
A ridiculously simple fixtures replacement for your web framework of
|
5
|
+
choice.
|
6
|
+
|
7
|
+
Description
|
8
|
+
-----------
|
9
|
+
|
10
|
+
Clown is a very small library (just 14 lines of code) that can
|
11
|
+
effectively replace fixtures or any other huge library for the same task.
|
12
|
+
|
13
|
+
Usage
|
14
|
+
-----
|
15
|
+
|
16
|
+
In the examples below we are using [Faker](http://faker.rubyforge.org/)
|
17
|
+
to generate random data, but you can use any method.
|
18
|
+
|
19
|
+
With ActiveRecord:
|
20
|
+
|
21
|
+
class User < ActiveRecord::Base
|
22
|
+
spawner do |user|
|
23
|
+
user.name = Faker::Name.name
|
24
|
+
user.email = Faker::Internet.email
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
With Sequel:
|
29
|
+
|
30
|
+
class User < Sequel::Model
|
31
|
+
extend Clown
|
32
|
+
|
33
|
+
spawner do |user|
|
34
|
+
user.name = Faker::Name.name
|
35
|
+
user.email = Faker::Internet.email
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
If you don't want to pollute your class definition, you
|
40
|
+
can of course use it from outside:
|
41
|
+
|
42
|
+
User.spawner do |user|
|
43
|
+
user.name = Faker::Name.name
|
44
|
+
user.email = Faker::Internet.email
|
45
|
+
end
|
46
|
+
|
47
|
+
Then, in your test or in any other place:
|
48
|
+
|
49
|
+
@user = User.spawn
|
50
|
+
|
51
|
+
Or, if you need something special:
|
52
|
+
|
53
|
+
@user = User.spawn :name => "Michel Martens"
|
54
|
+
|
55
|
+
Installation
|
56
|
+
------------
|
57
|
+
|
58
|
+
$ sudo gem install clown
|
59
|
+
|
60
|
+
### Thanks
|
61
|
+
|
62
|
+
Thanks to Foca (http://github.com/foca/) for his suggestions and Pedro
|
63
|
+
(http://github.com/peterpunk/) for the original gemspec.
|
64
|
+
|
65
|
+
License
|
66
|
+
-------
|
67
|
+
|
68
|
+
Copyright (c) 2009 Michel Martens for Citrusbyte
|
69
|
+
|
70
|
+
Permission is hereby granted, free of charge, to any person
|
71
|
+
obtaining a copy of this software and associated documentation
|
72
|
+
files (the "Software"), to deal in the Software without
|
73
|
+
restriction, including without limitation the rights to use,
|
74
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
75
|
+
copies of the Software, and to permit persons to whom the
|
76
|
+
Software is furnished to do so, subject to the following
|
77
|
+
conditions:
|
78
|
+
|
79
|
+
The above copyright notice and this permission notice shall be
|
80
|
+
included in all copies or substantial portions of the Software.
|
81
|
+
|
82
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
83
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
84
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
85
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
86
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
87
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
88
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
89
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rake/clean'
|
5
|
+
|
6
|
+
gem_spec_file = 'spawner.gemspec'
|
7
|
+
|
8
|
+
gem_spec = eval(File.read(gem_spec_file)) rescue nil
|
9
|
+
|
10
|
+
task :default => :test
|
11
|
+
|
12
|
+
Rake::TestTask.new(:test) do |t|
|
13
|
+
t.pattern = 'test/**/*_test.rb'
|
14
|
+
t.verbose = false
|
15
|
+
end
|
16
|
+
|
17
|
+
Rake::GemPackageTask.new(gem_spec) do |pkg|
|
18
|
+
pkg.need_zip = false
|
19
|
+
pkg.need_tar = false
|
20
|
+
rm_f FileList['pkg/**/*.*']
|
21
|
+
end if gem_spec
|
22
|
+
|
23
|
+
desc "Builds and installs the gem."
|
24
|
+
task :install => :repackage do
|
25
|
+
`sudo gem install pkg/#{gem_spec.name}-#{gem_spec.version}.gem`
|
26
|
+
end
|
data/lib/clown.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
|
3
|
+
module Spawner
|
4
|
+
def spawner &default
|
5
|
+
@@spawn ||= Hash.new
|
6
|
+
@@spawn[self.name] = default
|
7
|
+
end
|
8
|
+
|
9
|
+
def spawn attrs = {}
|
10
|
+
@@spawn[self.name].call(model = OpenStruct.new)
|
11
|
+
factory_method = respond_to?(:create!) ? :create! : :create
|
12
|
+
send(factory_method, model.send(:table).merge(attrs))
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "active_record"
|
3
|
+
require "contest"
|
4
|
+
require File.dirname(__FILE__) + "/../lib/clown"
|
5
|
+
require "faker"
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
8
|
+
ActiveRecord::Schema.define do
|
9
|
+
create_table :active_record_users do |table|
|
10
|
+
table.column :name, :string
|
11
|
+
table.column :email, :string
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class ActiveRecordUser < ActiveRecord::Base
|
16
|
+
extend Spawner
|
17
|
+
|
18
|
+
validates_presence_of :name
|
19
|
+
|
20
|
+
spawner do |user|
|
21
|
+
user.name = Faker::Name.name
|
22
|
+
user.email = Faker::Internet.email
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class TestSpawnerWithActiveRecord < Test::Unit::TestCase
|
27
|
+
setup do
|
28
|
+
@user = ActiveRecordUser.spawn :name => "John"
|
29
|
+
end
|
30
|
+
|
31
|
+
context "spawned user" do
|
32
|
+
should "have John as name" do
|
33
|
+
assert_equal "John", @user.name
|
34
|
+
end
|
35
|
+
|
36
|
+
context "with invalid attributes" do
|
37
|
+
should "raise an error" do
|
38
|
+
assert_raise ActiveRecord::RecordInvalid do
|
39
|
+
ActiveRecordUser.spawn :name => nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/test/all_test.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'contest'
|
3
|
+
require File.dirname(__FILE__) + "/../lib/clown"
|
4
|
+
|
5
|
+
class Base
|
6
|
+
attr_accessor :attributes
|
7
|
+
|
8
|
+
def initialize(attrs = {})
|
9
|
+
@attributes = attrs
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.create(attrs = {})
|
13
|
+
new(attrs)
|
14
|
+
end
|
15
|
+
|
16
|
+
def bar; attributes[:bar] end
|
17
|
+
def baz; attributes[:baz] end
|
18
|
+
def self.name; "Foo" end
|
19
|
+
|
20
|
+
extend Spawner
|
21
|
+
|
22
|
+
spawner do |object|
|
23
|
+
object.bar = 7
|
24
|
+
object.baz = 8
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Foo < Base
|
29
|
+
class << self
|
30
|
+
alias_method :create!, :create
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Bar < Foo
|
35
|
+
def self.name; "Bar" end
|
36
|
+
|
37
|
+
spawner do |bar|
|
38
|
+
bar.bar = 9
|
39
|
+
bar.baz = 10
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class Baz < Base
|
44
|
+
end
|
45
|
+
|
46
|
+
class TestFoo < Test::Unit::TestCase
|
47
|
+
should "have a name class method" do
|
48
|
+
assert Foo.respond_to?(:name)
|
49
|
+
assert_equal "Foo", Foo.name
|
50
|
+
end
|
51
|
+
|
52
|
+
context "with attributes :bar and :baz" do
|
53
|
+
context "when sent a hash on initialization" do
|
54
|
+
should "set the attributes to the passed values" do
|
55
|
+
foo = Foo.new :bar => 1, :baz => 2
|
56
|
+
assert_equal 1, foo.bar
|
57
|
+
assert_equal 2, foo.baz
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "that implements Spawner" do
|
63
|
+
should "be kind of Spawner" do
|
64
|
+
assert Foo.kind_of?(Spawner)
|
65
|
+
end
|
66
|
+
|
67
|
+
context "when instantiated with spawn" do
|
68
|
+
context "without parameters" do
|
69
|
+
should "initialize the instance with the defined block" do
|
70
|
+
foo = Foo.spawn
|
71
|
+
assert_equal 7, foo.bar
|
72
|
+
assert_equal 8, foo.baz
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context "with a hash supplied" do
|
77
|
+
should "override the default values" do
|
78
|
+
foo = Foo.spawn :bar => 1
|
79
|
+
assert_equal 1, foo.bar
|
80
|
+
assert_equal 8, foo.baz
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context "and a class Bar" do
|
85
|
+
context "that also implements Spawner" do
|
86
|
+
should "be kind of Spawner" do
|
87
|
+
assert Bar.kind_of?(Spawner)
|
88
|
+
end
|
89
|
+
|
90
|
+
context "when sent :name" do
|
91
|
+
should "return 'Bar'" do
|
92
|
+
assert_equal "Bar", Bar.name
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "when sent :spawn" do
|
97
|
+
should "return an instance of Bar" do
|
98
|
+
assert Bar.spawn.kind_of?(Bar)
|
99
|
+
end
|
100
|
+
|
101
|
+
should "not interfere with Foo.spawn" do
|
102
|
+
assert_equal 7, Foo.spawn.bar
|
103
|
+
assert_equal 9, Bar.spawn.bar
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context "and it doesn't understand #create! (only #create)" do
|
111
|
+
should "work anyway" do
|
112
|
+
baz = Baz.spawn :bar => 1
|
113
|
+
assert_equal 1, baz.bar
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
data/test/sequel_test.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "sequel"
|
3
|
+
require "contest"
|
4
|
+
require File.dirname(__FILE__) + "/../lib/clown"
|
5
|
+
require "faker"
|
6
|
+
|
7
|
+
DB = Sequel.sqlite
|
8
|
+
DB << "CREATE TABLE sequel_users (name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL)"
|
9
|
+
|
10
|
+
class SequelUser < Sequel::Model
|
11
|
+
extend Spawner
|
12
|
+
|
13
|
+
validates do
|
14
|
+
presence_of :name
|
15
|
+
end
|
16
|
+
|
17
|
+
spawner do |user|
|
18
|
+
user.name = Faker::Name.name
|
19
|
+
user.email = Faker::Internet.email
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class TestSpawnerWithSequel < Test::Unit::TestCase
|
24
|
+
setup do
|
25
|
+
@user = SequelUser.spawn :name => "John"
|
26
|
+
end
|
27
|
+
|
28
|
+
context "spawned user" do
|
29
|
+
should "have John as name" do
|
30
|
+
assert_equal "John", @user.name
|
31
|
+
end
|
32
|
+
|
33
|
+
context "with invalid attributes" do
|
34
|
+
should "raise an error" do
|
35
|
+
assert_raise Sequel::Error::InvalidValue do
|
36
|
+
SequelUser.spawn :name => nil
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spawn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.8
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michel Martens
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-12-18 00:00:00 -02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: michel@soveran.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/clown.rb
|
26
|
+
- README.markdown
|
27
|
+
- LICENSE
|
28
|
+
- Rakefile
|
29
|
+
- test/active_record_test.rb
|
30
|
+
- test/all_test.rb
|
31
|
+
- test/sequel_test.rb
|
32
|
+
has_rdoc: false
|
33
|
+
homepage: http://github.com/soveran/spawner
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.3.1
|
55
|
+
signing_key:
|
56
|
+
specification_version: 2
|
57
|
+
summary: Simple fixtures replacement for Sequel, ActiveRecord and probably many other ORMs
|
58
|
+
test_files: []
|
59
|
+
|