soveran-spawner 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +2 -8
- data/lib/spawner.rb +2 -3
- data/spawner.gemspec +1 -1
- data/test/all_test.rb +42 -50
- metadata +1 -1
data/README.markdown
CHANGED
@@ -7,7 +7,7 @@ choice.
|
|
7
7
|
Description
|
8
8
|
-----------
|
9
9
|
|
10
|
-
Spawner is a very small library (just
|
10
|
+
Spawner is a very small library (just 14 lines of code) that can
|
11
11
|
effectively replace fixtures or any other huge library for the same task.
|
12
12
|
|
13
13
|
Usage
|
@@ -50,12 +50,6 @@ Then, in your test or in any other place:
|
|
50
50
|
|
51
51
|
Or, if you need something special:
|
52
52
|
|
53
|
-
@user = User.spawn do |user|
|
54
|
-
user.name = "Michel Martens"
|
55
|
-
end
|
56
|
-
|
57
|
-
Or even this:
|
58
|
-
|
59
53
|
@user = User.spawn :name => "Michel Martens"
|
60
54
|
|
61
55
|
Installation
|
@@ -67,7 +61,7 @@ Installation
|
|
67
61
|
### Thanks
|
68
62
|
|
69
63
|
Thanks to Foca (http://github.com/foca/) for his suggestions and Pedro
|
70
|
-
(http://github.com/peterpunk/) for the gemspec.
|
64
|
+
(http://github.com/peterpunk/) for the original gemspec.
|
71
65
|
|
72
66
|
License
|
73
67
|
-------
|
data/lib/spawner.rb
CHANGED
@@ -2,13 +2,12 @@ require 'ostruct'
|
|
2
2
|
|
3
3
|
module Spawner
|
4
4
|
def spawner &default
|
5
|
-
@@spawn ||= Hash.new
|
5
|
+
@@spawn ||= Hash.new
|
6
6
|
@@spawn[self.name] = default
|
7
7
|
end
|
8
8
|
|
9
9
|
def spawn attrs = {}
|
10
|
-
model = OpenStruct.new
|
11
|
-
@@spawn[self.name].call(model)
|
10
|
+
@@spawn[self.name].call(model = OpenStruct.new)
|
12
11
|
create(model.send(:table).merge(attrs))
|
13
12
|
end
|
14
13
|
end
|
data/spawner.gemspec
CHANGED
data/test/all_test.rb
CHANGED
@@ -5,44 +5,17 @@ require File.dirname(__FILE__) + "/../lib/spawner"
|
|
5
5
|
class Foo
|
6
6
|
attr_accessor :attributes
|
7
7
|
|
8
|
-
def initialize(
|
9
|
-
@attributes =
|
10
|
-
|
11
|
-
yield self if block_given?
|
12
|
-
|
13
|
-
if args.first
|
14
|
-
args.first.each do |key, value|
|
15
|
-
@attributes[key] = value
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def save!
|
8
|
+
def initialize(attrs = {})
|
9
|
+
@attributes = attrs
|
21
10
|
end
|
22
11
|
|
23
12
|
def self.create(attrs = {})
|
24
13
|
new(attrs)
|
25
14
|
end
|
26
15
|
|
27
|
-
def bar
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
def baz
|
32
|
-
attributes[:baz]
|
33
|
-
end
|
34
|
-
|
35
|
-
def bar=(value)
|
36
|
-
attributes[:bar] = value
|
37
|
-
end
|
38
|
-
|
39
|
-
def baz=(value)
|
40
|
-
attributes[:baz] = value
|
41
|
-
end
|
42
|
-
|
43
|
-
def name
|
44
|
-
"Foo"
|
45
|
-
end
|
16
|
+
def bar; attributes[:bar] end
|
17
|
+
def baz; attributes[:baz] end
|
18
|
+
def self.name; "Foo" end
|
46
19
|
|
47
20
|
extend Spawner
|
48
21
|
|
@@ -52,9 +25,19 @@ class Foo
|
|
52
25
|
end
|
53
26
|
end
|
54
27
|
|
28
|
+
class Bar < Foo
|
29
|
+
def self.name; "Bar" end
|
30
|
+
|
31
|
+
spawner do |bar|
|
32
|
+
bar.bar = 9
|
33
|
+
bar.baz = 10
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
55
37
|
class TestFoo < Test::Unit::TestCase
|
56
|
-
should "have a name
|
57
|
-
assert Foo.
|
38
|
+
should "have a name class method" do
|
39
|
+
assert Foo.respond_to?(:name)
|
40
|
+
assert_equal "Foo", Foo.name
|
58
41
|
end
|
59
42
|
|
60
43
|
context "with attributes :bar and :baz" do
|
@@ -64,22 +47,6 @@ class TestFoo < Test::Unit::TestCase
|
|
64
47
|
assert_equal 1, foo.bar
|
65
48
|
assert_equal 2, foo.baz
|
66
49
|
end
|
67
|
-
|
68
|
-
context "and a block" do
|
69
|
-
should "give priority to the values in the hash" do
|
70
|
-
foo = Foo.new(:bar => 5, :baz => 6) { |f| f.baz = 7 }
|
71
|
-
assert_equal 5, foo.bar
|
72
|
-
assert_equal 6, foo.baz
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
context "when sent a block" do
|
78
|
-
should "set the attributes from the passed block" do
|
79
|
-
foo = Foo.new { |f| f.bar = 3; f.baz = 4 }
|
80
|
-
assert_equal 3, foo.bar
|
81
|
-
assert_equal 4, foo.baz
|
82
|
-
end
|
83
50
|
end
|
84
51
|
end
|
85
52
|
|
@@ -104,6 +71,31 @@ class TestFoo < Test::Unit::TestCase
|
|
104
71
|
assert_equal 8, foo.baz
|
105
72
|
end
|
106
73
|
end
|
74
|
+
|
75
|
+
context "and a class Bar" do
|
76
|
+
context "that also implements Spawner" do
|
77
|
+
should "be kind of Spawner" do
|
78
|
+
assert Bar.kind_of?(Spawner)
|
79
|
+
end
|
80
|
+
|
81
|
+
context "when sent :name" do
|
82
|
+
should "return 'Bar'" do
|
83
|
+
assert_equal "Bar", Bar.name
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "when sent :spawn" do
|
88
|
+
should "return an instance of Bar" do
|
89
|
+
assert Bar.spawn.kind_of?(Bar)
|
90
|
+
end
|
91
|
+
|
92
|
+
should "not interfere with Foo.spawn" do
|
93
|
+
assert_equal 7, Foo.spawn.bar
|
94
|
+
assert_equal 9, Bar.spawn.bar
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
107
99
|
end
|
108
100
|
end
|
109
101
|
end
|