spawn 0.1.1 → 0.1.2
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/LICENSE +1 -1
- data/README.markdown +33 -3
- data/lib/spawn.rb +2 -2
- data/spawn.gemspec +3 -3
- data/test/all_test.rb +46 -8
- data/test/sequel_test.rb +2 -2
- metadata +6 -3
data/LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2008 Michel Martens
|
1
|
+
Copyright (c) 2008 Michel Martens and Damian Janowski
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
a copy of this software and associated documentation files (the
|
data/README.markdown
CHANGED
@@ -52,6 +52,36 @@ Or, if you need something special:
|
|
52
52
|
|
53
53
|
@user = User.spawn :name => "Michel Martens"
|
54
54
|
|
55
|
+
Conditional evaluation
|
56
|
+
----------------------
|
57
|
+
|
58
|
+
Spawn will execute the right hand side of your assignment even if you
|
59
|
+
provide a value for some particular key. Consider the following example:
|
60
|
+
|
61
|
+
User.spawner do |user|
|
62
|
+
user.profile = Profile.spawn # Profile.spawn is executed even if you provide a value for :profile.
|
63
|
+
end
|
64
|
+
|
65
|
+
User.spawn(:profile => Profile.first)
|
66
|
+
|
67
|
+
Here, you will be creating an extra instance of `Profile`, because when
|
68
|
+
the block is evaluated it calls `Profile.spawn`. If the right hand side
|
69
|
+
of your assignment is costly or has side effects, you may want to avoid
|
70
|
+
this behavior by using `||=`:
|
71
|
+
|
72
|
+
User.spawner do |user|
|
73
|
+
user.profile ||= Profile.spawn
|
74
|
+
end
|
75
|
+
|
76
|
+
Then, if you pass a `:profile`:
|
77
|
+
|
78
|
+
User.spawn(:profile => Profile.first)
|
79
|
+
|
80
|
+
You can verify that `Profile.spawn` is never called. Although this
|
81
|
+
may sound evident, it can bite you if you rely on the RHS not executing
|
82
|
+
(e.g. if you're using Spawn to populate fake data into a database and
|
83
|
+
you want to control how many instances you create).
|
84
|
+
|
55
85
|
Installation
|
56
86
|
------------
|
57
87
|
|
@@ -59,13 +89,13 @@ Installation
|
|
59
89
|
|
60
90
|
### Thanks
|
61
91
|
|
62
|
-
Thanks to Foca
|
63
|
-
(http://github.com/peterpunk
|
92
|
+
Thanks to [Foca](http://github.com/foca) for his suggestions and
|
93
|
+
[Pedro](http://github.com/peterpunk) for the original gemspec.
|
64
94
|
|
65
95
|
License
|
66
96
|
-------
|
67
97
|
|
68
|
-
Copyright (c) 2009 Michel Martens
|
98
|
+
Copyright (c) 2009 Michel Martens and Damian Janowski
|
69
99
|
|
70
100
|
Permission is hereby granted, free of charge, to any person
|
71
101
|
obtaining a copy of this software and associated documentation
|
data/lib/spawn.rb
CHANGED
@@ -3,11 +3,11 @@ require "ostruct"
|
|
3
3
|
module Spawn
|
4
4
|
def spawner &default
|
5
5
|
@@spawn ||= Hash.new
|
6
|
-
@@spawn[self
|
6
|
+
@@spawn[self] = default
|
7
7
|
end
|
8
8
|
|
9
9
|
def spawn attrs = {}
|
10
|
-
@@spawn[self
|
10
|
+
@@spawn[self].call(model = OpenStruct.new(attrs))
|
11
11
|
factory_method = respond_to?(:create!) ? :create! : :create
|
12
12
|
send(factory_method, model.send(:table).merge(attrs))
|
13
13
|
end
|
data/spawn.gemspec
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'spawn'
|
3
|
-
s.version = '0.1.
|
3
|
+
s.version = '0.1.2'
|
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
|
-
s.
|
7
|
-
s.
|
6
|
+
s.authors = ["Michel Martens", "Damian Janowski"]
|
7
|
+
s.email = ["michel@soveran.com", "djanowski@dimaion.com"]
|
8
8
|
s.homepage = "http://github.com/soveran/spawn"
|
9
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
10
|
s.require_paths = ['lib']
|
data/test/all_test.rb
CHANGED
@@ -15,25 +15,22 @@ class Base
|
|
15
15
|
|
16
16
|
def bar; attributes[:bar] end
|
17
17
|
def baz; attributes[:baz] end
|
18
|
-
def self.name; "Foo" end
|
19
18
|
|
20
19
|
extend Spawn
|
21
|
-
|
22
|
-
spawner do |object|
|
23
|
-
object.bar = 7
|
24
|
-
object.baz = 8
|
25
|
-
end
|
26
20
|
end
|
27
21
|
|
28
22
|
class Foo < Base
|
29
23
|
class << self
|
30
24
|
alias_method :create!, :create
|
31
25
|
end
|
26
|
+
|
27
|
+
spawner do |object|
|
28
|
+
object.bar = 7
|
29
|
+
object.baz = 8
|
30
|
+
end
|
32
31
|
end
|
33
32
|
|
34
33
|
class Bar < Foo
|
35
|
-
def self.name; "Bar" end
|
36
|
-
|
37
34
|
spawner do |bar|
|
38
35
|
bar.bar = 9
|
39
36
|
bar.baz = 10
|
@@ -41,6 +38,34 @@ class Bar < Foo
|
|
41
38
|
end
|
42
39
|
|
43
40
|
class Baz < Base
|
41
|
+
spawner do |object|
|
42
|
+
object.bar = 7
|
43
|
+
object.baz = 8
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
module FooBar
|
48
|
+
class Baz < ::Base
|
49
|
+
spawner do |baz|
|
50
|
+
baz.bar = 1
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
module FooBaz
|
56
|
+
class Baz < ::Base
|
57
|
+
spawner do |baz|
|
58
|
+
baz.bar = 2
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class Qux < Base
|
64
|
+
def self.name; "Qux"; end
|
65
|
+
|
66
|
+
spawner do |barz|
|
67
|
+
barz.bar ||= raise "Should have received :bar"
|
68
|
+
end
|
44
69
|
end
|
45
70
|
|
46
71
|
class TestFoo < Test::Unit::TestCase
|
@@ -56,6 +81,14 @@ class TestFoo < Test::Unit::TestCase
|
|
56
81
|
assert_equal 1, foo.bar
|
57
82
|
assert_equal 2, foo.baz
|
58
83
|
end
|
84
|
+
|
85
|
+
should "pass the values to the block" do
|
86
|
+
assert_raise(RuntimeError) do
|
87
|
+
Qux.spawn
|
88
|
+
end
|
89
|
+
|
90
|
+
assert_equal "Qux", Qux.spawn(:bar => "Qux").bar
|
91
|
+
end
|
59
92
|
end
|
60
93
|
end
|
61
94
|
|
@@ -114,4 +147,9 @@ class TestFoo < Test::Unit::TestCase
|
|
114
147
|
end
|
115
148
|
end
|
116
149
|
end
|
150
|
+
|
151
|
+
should "respect namespaces" do
|
152
|
+
assert_equal 1, FooBar::Baz.spawn.bar
|
153
|
+
assert_equal 2, FooBaz::Baz.spawn.bar
|
154
|
+
end
|
117
155
|
end
|
data/test/sequel_test.rb
CHANGED
@@ -10,8 +10,8 @@ DB << "CREATE TABLE sequel_users (name VARCHAR(255) NOT NULL, email VARCHAR(255)
|
|
10
10
|
class SequelUser < Sequel::Model
|
11
11
|
extend Spawn
|
12
12
|
|
13
|
-
|
14
|
-
|
13
|
+
def validate
|
14
|
+
errors.add(:name, "Not present") if name.nil? or name.empty?
|
15
15
|
end
|
16
16
|
|
17
17
|
spawner do |user|
|
metadata
CHANGED
@@ -1,20 +1,23 @@
|
|
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michel Martens
|
8
|
+
- Damian Janowski
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
12
|
|
12
|
-
date: 2009-06-
|
13
|
+
date: 2009-06-16 00:00:00 -03:00
|
13
14
|
default_executable:
|
14
15
|
dependencies: []
|
15
16
|
|
16
17
|
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.
|
17
|
-
email:
|
18
|
+
email:
|
19
|
+
- michel@soveran.com
|
20
|
+
- djanowski@dimaion.com
|
18
21
|
executables: []
|
19
22
|
|
20
23
|
extensions: []
|