spawn 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +27 -4
- data/lib/spawn.rb +16 -4
- data/spawn.gemspec +2 -3
- data/test/active_record_test.rb +6 -7
- data/test/all_test.rb +21 -0
- data/test/ohm_test.rb +41 -0
- data/test/sequel_test.rb +2 -3
- metadata +4 -3
data/README.markdown
CHANGED
@@ -7,8 +7,8 @@ choice.
|
|
7
7
|
Description
|
8
8
|
-----------
|
9
9
|
|
10
|
-
Spawn is a very small library
|
11
|
-
|
10
|
+
Spawn is a very small library that can effectively replace fixtures or
|
11
|
+
any other huge library for the same task.
|
12
12
|
|
13
13
|
Usage
|
14
14
|
-----
|
@@ -16,7 +16,7 @@ Usage
|
|
16
16
|
In the examples below we are using [Faker](http://faker.rubyforge.org/)
|
17
17
|
to generate random data, but you can use any method.
|
18
18
|
|
19
|
-
With ActiveRecord:
|
19
|
+
With [ActiveRecord](http://api.rubyonrails.org/classes/ActiveRecord/Base.html):
|
20
20
|
|
21
21
|
class User < ActiveRecord::Base
|
22
22
|
spawner do |user|
|
@@ -25,7 +25,7 @@ With ActiveRecord:
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
With Sequel:
|
28
|
+
With [Sequel](http://sequel.rubyforge.org/):
|
29
29
|
|
30
30
|
class User < Sequel::Model
|
31
31
|
extend Spawn
|
@@ -36,6 +36,20 @@ With Sequel:
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
+
With [Ohm](http://ohm.keyvalue.org):
|
40
|
+
|
41
|
+
class User < Ohm::Model
|
42
|
+
extend Spawn
|
43
|
+
|
44
|
+
attribute :name
|
45
|
+
attribute :email
|
46
|
+
|
47
|
+
spawner do |user|
|
48
|
+
user.name = Faker::Name.name
|
49
|
+
user.email = Faker::Internet.email
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
39
53
|
If you don't want to pollute your class definition, you
|
40
54
|
can of course use it from outside:
|
41
55
|
|
@@ -52,6 +66,15 @@ Or, if you need something special:
|
|
52
66
|
|
53
67
|
@user = User.spawn :name => "Michel Martens"
|
54
68
|
|
69
|
+
This sends to User.new all the attributes defined in the spawner block, along with
|
70
|
+
the hash of attributes passed when spawning.
|
71
|
+
|
72
|
+
If you want a reference to the model before the validity is checked, you can pass
|
73
|
+
a block:
|
74
|
+
|
75
|
+
@user = User.spawn { |u| u.name = "Michel Martens" }
|
76
|
+
|
77
|
+
|
55
78
|
Conditional evaluation
|
56
79
|
----------------------
|
57
80
|
|
data/lib/spawn.rb
CHANGED
@@ -1,14 +1,26 @@
|
|
1
1
|
require "ostruct"
|
2
2
|
|
3
3
|
module Spawn
|
4
|
+
|
5
|
+
Invalid = Class.new(ArgumentError)
|
6
|
+
|
4
7
|
def spawner &default
|
5
8
|
@@spawn ||= Hash.new
|
6
9
|
@@spawn[self] = default
|
7
10
|
end
|
8
11
|
|
9
|
-
def spawn
|
10
|
-
|
11
|
-
|
12
|
-
|
12
|
+
def spawn params = {}
|
13
|
+
|
14
|
+
# Grab default parameters from spawner block.
|
15
|
+
@@spawn[self].call(attrs = OpenStruct.new(params))
|
16
|
+
|
17
|
+
# Initialize model
|
18
|
+
model = new(attrs.send(:table).merge(params))
|
19
|
+
|
20
|
+
# Yield model for changes to be made before saving.
|
21
|
+
yield(model) if block_given?
|
22
|
+
|
23
|
+
# Raise an error if the model is invalid or couldn't be saved.
|
24
|
+
model.valid? and model.save and model or raise(Invalid, model.errors.inspect)
|
13
25
|
end
|
14
26
|
end
|
data/spawn.gemspec
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'spawn'
|
3
|
-
s.version = '0.1.
|
3
|
+
s.version = '0.1.3'
|
4
4
|
s.summary = %{Simple fixtures replacement for Sequel, ActiveRecord, Ohm and probably many other ORMs}
|
5
5
|
s.description = %{Spawn is a very small library (just 14 lines of code) that can effectively replace fixtures or any other library for the same task.}
|
6
6
|
s.authors = ["Michel Martens", "Damian Janowski"]
|
7
7
|
s.email = ["michel@soveran.com", "djanowski@dimaion.com"]
|
8
8
|
s.homepage = "http://github.com/soveran/spawn"
|
9
|
-
s.files = ["lib/spawn.rb", "rails/init.rb", "README.markdown", "LICENSE", "Rakefile", "test/active_record_test.rb", "test/all_test.rb", "test/sequel_test.rb", "spawn.gemspec"]
|
10
|
-
s.require_paths = ['lib']
|
9
|
+
s.files = ["lib/spawn.rb", "rails/init.rb", "README.markdown", "LICENSE", "Rakefile", "test/active_record_test.rb", "test/all_test.rb", "test/ohm_test.rb", "test/sequel_test.rb", "spawn.gemspec"]
|
11
10
|
s.rubyforge_project = "spawner"
|
12
11
|
end
|
data/test/active_record_test.rb
CHANGED
@@ -18,25 +18,24 @@ class ActiveRecordUser < ActiveRecord::Base
|
|
18
18
|
validates_presence_of :name
|
19
19
|
|
20
20
|
spawner do |user|
|
21
|
-
user.name = Faker::Name.name
|
22
21
|
user.email = Faker::Internet.email
|
23
22
|
end
|
24
23
|
end
|
25
24
|
|
26
25
|
class TestSpawnWithActiveRecord < Test::Unit::TestCase
|
27
|
-
setup do
|
28
|
-
@user = ActiveRecordUser.spawn :name => "John"
|
29
|
-
end
|
30
|
-
|
31
26
|
context "spawned user" do
|
27
|
+
setup do
|
28
|
+
@user = ActiveRecordUser.spawn :name => "John"
|
29
|
+
end
|
30
|
+
|
32
31
|
should "have John as name" do
|
33
32
|
assert_equal "John", @user.name
|
34
33
|
end
|
35
34
|
|
36
35
|
context "with invalid attributes" do
|
37
36
|
should "raise an error" do
|
38
|
-
assert_raise
|
39
|
-
ActiveRecordUser.spawn
|
37
|
+
assert_raise Spawn::Invalid do
|
38
|
+
ActiveRecordUser.spawn
|
40
39
|
end
|
41
40
|
end
|
42
41
|
end
|
data/test/all_test.rb
CHANGED
@@ -16,6 +16,17 @@ class Base
|
|
16
16
|
def bar; attributes[:bar] end
|
17
17
|
def baz; attributes[:baz] end
|
18
18
|
|
19
|
+
def bar=(value); attributes[:bar] = value; end
|
20
|
+
def baz=(value); attributes[:baz] = value; end
|
21
|
+
|
22
|
+
def save
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def valid?
|
27
|
+
true
|
28
|
+
end
|
29
|
+
|
19
30
|
extend Spawn
|
20
31
|
end
|
21
32
|
|
@@ -114,6 +125,16 @@ class TestFoo < Test::Unit::TestCase
|
|
114
125
|
end
|
115
126
|
end
|
116
127
|
|
128
|
+
context "with a block supplied" do
|
129
|
+
should "override the default values with single assignments" do
|
130
|
+
foo = Foo.spawn(:bar => 1) do |f|
|
131
|
+
f.baz = 2
|
132
|
+
end
|
133
|
+
assert_equal 1, foo.bar
|
134
|
+
assert_equal 2, foo.baz
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
117
138
|
context "and a class Bar" do
|
118
139
|
context "that also implements Spawn" do
|
119
140
|
should "be kind of Spawn" do
|
data/test/ohm_test.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "ohm"
|
3
|
+
require "contest"
|
4
|
+
require File.dirname(__FILE__) + "/../lib/spawn"
|
5
|
+
|
6
|
+
Ohm.connect
|
7
|
+
|
8
|
+
class OhmUser < Ohm::Model
|
9
|
+
extend Spawn
|
10
|
+
|
11
|
+
attribute :name
|
12
|
+
attribute :email
|
13
|
+
|
14
|
+
def validate
|
15
|
+
assert_present :name
|
16
|
+
end
|
17
|
+
|
18
|
+
spawner do |user|
|
19
|
+
user.email = "albert@example.com"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class TestSpawnWithOhm < Test::Unit::TestCase
|
24
|
+
setup do
|
25
|
+
@user = OhmUser.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 Spawn::Invalid do
|
36
|
+
OhmUser.spawn
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/test/sequel_test.rb
CHANGED
@@ -15,7 +15,6 @@ class SequelUser < Sequel::Model
|
|
15
15
|
end
|
16
16
|
|
17
17
|
spawner do |user|
|
18
|
-
user.name = Faker::Name.name
|
19
18
|
user.email = Faker::Internet.email
|
20
19
|
end
|
21
20
|
end
|
@@ -32,8 +31,8 @@ class TestSpawnWithSequel < Test::Unit::TestCase
|
|
32
31
|
|
33
32
|
context "with invalid attributes" do
|
34
33
|
should "raise an error" do
|
35
|
-
assert_raise
|
36
|
-
SequelUser.spawn
|
34
|
+
assert_raise Spawn::Invalid do
|
35
|
+
SequelUser.spawn
|
37
36
|
end
|
38
37
|
end
|
39
38
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spawn
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michel Martens
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2009-
|
13
|
+
date: 2009-11-20 00:00:00 -03:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -32,6 +32,7 @@ files:
|
|
32
32
|
- Rakefile
|
33
33
|
- test/active_record_test.rb
|
34
34
|
- test/all_test.rb
|
35
|
+
- test/ohm_test.rb
|
35
36
|
- test/sequel_test.rb
|
36
37
|
- spawn.gemspec
|
37
38
|
has_rdoc: true
|
@@ -58,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
59
|
requirements: []
|
59
60
|
|
60
61
|
rubyforge_project: spawner
|
61
|
-
rubygems_version: 1.3.
|
62
|
+
rubygems_version: 1.3.5
|
62
63
|
signing_key:
|
63
64
|
specification_version: 3
|
64
65
|
summary: Simple fixtures replacement for Sequel, ActiveRecord, Ohm and probably many other ORMs
|