soveran-spawner 0.0.4 → 0.0.5
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/{MIT-LICENSE → LICENSE} +0 -0
- data/README.markdown +96 -0
- data/lib/spawner.rb +7 -9
- data/spawner.gemspec +6 -7
- data/test/all_test.rb +109 -0
- metadata +9 -9
- data/README.rdoc +0 -61
data/{MIT-LICENSE → LICENSE}
RENAMED
File without changes
|
data/README.markdown
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
Spawner
|
2
|
+
=======
|
3
|
+
|
4
|
+
A ridiculously simple fixtures replacement for your web framework of
|
5
|
+
choice.
|
6
|
+
|
7
|
+
Description
|
8
|
+
-----------
|
9
|
+
|
10
|
+
Spawner is a very small library (just 15 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 Spawner
|
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 do |user|
|
54
|
+
user.name = "Michel Martens"
|
55
|
+
end
|
56
|
+
|
57
|
+
Or even this:
|
58
|
+
|
59
|
+
@user = User.spawn :name => "Michel Martens"
|
60
|
+
|
61
|
+
Installation
|
62
|
+
------------
|
63
|
+
|
64
|
+
$ gem sources -a http://gems.github.com (you only have to do this once)
|
65
|
+
$ sudo gem install soveran-spawner
|
66
|
+
|
67
|
+
### Thanks
|
68
|
+
|
69
|
+
Thanks to Foca (http://github.com/foca/) for his suggestions and Pedro
|
70
|
+
(http://github.com/peterpunk/) for the gemspec.
|
71
|
+
|
72
|
+
License
|
73
|
+
-------
|
74
|
+
|
75
|
+
Copyright (c) 2009 Michel Martens for Citrusbyte
|
76
|
+
|
77
|
+
Permission is hereby granted, free of charge, to any person
|
78
|
+
obtaining a copy of this software and associated documentation
|
79
|
+
files (the "Software"), to deal in the Software without
|
80
|
+
restriction, including without limitation the rights to use,
|
81
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
82
|
+
copies of the Software, and to permit persons to whom the
|
83
|
+
Software is furnished to do so, subject to the following
|
84
|
+
conditions:
|
85
|
+
|
86
|
+
The above copyright notice and this permission notice shall be
|
87
|
+
included in all copies or substantial portions of the Software.
|
88
|
+
|
89
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
90
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
91
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
92
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
93
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
94
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
95
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
96
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/spawner.rb
CHANGED
@@ -1,16 +1,14 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
1
3
|
module Spawner
|
2
4
|
def spawner &default
|
3
|
-
@@spawn ||= Hash.new
|
4
|
-
hash[key] = lambda { |model| model }
|
5
|
-
end
|
5
|
+
@@spawn ||= Hash.new { |hash, key| hash[key] = lambda { |model| model } }
|
6
6
|
@@spawn[self.name] = default
|
7
7
|
end
|
8
8
|
|
9
|
-
def spawn attrs =
|
10
|
-
model = new
|
11
|
-
|
12
|
-
|
13
|
-
model.save!
|
14
|
-
model
|
9
|
+
def spawn attrs = {}
|
10
|
+
model = OpenStruct.new
|
11
|
+
@@spawn[self.name].call(model)
|
12
|
+
create(model.send(:table).merge(attrs))
|
15
13
|
end
|
16
14
|
end
|
data/spawner.gemspec
CHANGED
@@ -1,20 +1,19 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "spawner"
|
3
|
-
s.version = "0.0.
|
3
|
+
s.version = "0.0.5"
|
4
4
|
s.date = "2008-12-18"
|
5
5
|
s.summary = "Simple fixtures replacement for Sequel, ActiveRecord and probably many other ORMs"
|
6
6
|
s.email = "michel@soveran.com"
|
7
7
|
s.homepage = "http://github.com/soveran/spawner"
|
8
8
|
s.description = "Simple fixtures replacement that allows to populate the database with custom data."
|
9
|
-
s.has_rdoc =
|
9
|
+
s.has_rdoc = false
|
10
10
|
s.authors = ["Michel Martens"]
|
11
11
|
s.files = [
|
12
|
-
"README.
|
13
|
-
"
|
12
|
+
"README.markdown",
|
13
|
+
"LICENSE",
|
14
14
|
"spawner.gemspec",
|
15
15
|
"lib/spawner.rb",
|
16
16
|
"rails/init.rb",
|
17
|
-
"extras/samples.rake"
|
18
|
-
|
19
|
-
s.extra_rdoc_files = ['README.rdoc']
|
17
|
+
"extras/samples.rake",
|
18
|
+
"test/all_test.rb"]
|
20
19
|
end
|
data/test/all_test.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'contest'
|
3
|
+
require File.dirname(__FILE__) + "/../lib/spawner"
|
4
|
+
|
5
|
+
class Foo
|
6
|
+
attr_accessor :attributes
|
7
|
+
|
8
|
+
def initialize(*args)
|
9
|
+
@attributes = Hash.new
|
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!
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.create(attrs = {})
|
24
|
+
new(attrs)
|
25
|
+
end
|
26
|
+
|
27
|
+
def bar
|
28
|
+
attributes[:bar]
|
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
|
46
|
+
|
47
|
+
extend Spawner
|
48
|
+
|
49
|
+
spawner do |foo|
|
50
|
+
foo.bar = 7
|
51
|
+
foo.baz = 8
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class TestFoo < Test::Unit::TestCase
|
56
|
+
should "have a name instance method" do
|
57
|
+
assert Foo.new.respond_to?(:name)
|
58
|
+
end
|
59
|
+
|
60
|
+
context "with attributes :bar and :baz" do
|
61
|
+
context "when sent a hash on initialization" do
|
62
|
+
should "set the attributes to the passed values" do
|
63
|
+
foo = Foo.new :bar => 1, :baz => 2
|
64
|
+
assert_equal 1, foo.bar
|
65
|
+
assert_equal 2, foo.baz
|
66
|
+
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
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context "that implements Spawner" do
|
87
|
+
should "be kind of Spawner" do
|
88
|
+
assert Foo.kind_of?(Spawner)
|
89
|
+
end
|
90
|
+
|
91
|
+
context "when instantiated with spawn" do
|
92
|
+
context "without parameters" do
|
93
|
+
should "initialize the instance with the defined block" do
|
94
|
+
foo = Foo.spawn
|
95
|
+
assert_equal 7, foo.bar
|
96
|
+
assert_equal 8, foo.baz
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context "with a hash supplied" do
|
101
|
+
should "override the default values" do
|
102
|
+
foo = Foo.spawn :bar => 1
|
103
|
+
assert_equal 1, foo.bar
|
104
|
+
assert_equal 8, foo.baz
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: soveran-spawner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michel Martens
|
@@ -19,21 +19,21 @@ executables: []
|
|
19
19
|
|
20
20
|
extensions: []
|
21
21
|
|
22
|
-
extra_rdoc_files:
|
23
|
-
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
24
|
files:
|
25
|
-
- README.
|
26
|
-
-
|
25
|
+
- README.markdown
|
26
|
+
- LICENSE
|
27
27
|
- spawner.gemspec
|
28
28
|
- lib/spawner.rb
|
29
29
|
- rails/init.rb
|
30
30
|
- extras/samples.rake
|
31
|
-
|
31
|
+
- test/all_test.rb
|
32
|
+
has_rdoc: false
|
32
33
|
homepage: http://github.com/soveran/spawner
|
33
34
|
post_install_message:
|
34
|
-
rdoc_options:
|
35
|
-
|
36
|
-
- README.rdoc
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
37
|
require_paths:
|
38
38
|
- lib
|
39
39
|
required_ruby_version: !ruby/object:Gem::Requirement
|
data/README.rdoc
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
= Spawner
|
2
|
-
|
3
|
-
Spawner is a simple fixtures replacement. In the examples
|
4
|
-
I use Faker to generate random data, but you can use any method.
|
5
|
-
|
6
|
-
== Usage
|
7
|
-
|
8
|
-
With ActiveRecord:
|
9
|
-
|
10
|
-
class User < ActiveRecord::Base
|
11
|
-
spawner do |user|
|
12
|
-
user.name = Faker::Name.name
|
13
|
-
user.email = Faker::Internet.email
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
With Sequel:
|
18
|
-
|
19
|
-
class User < Sequel::Model
|
20
|
-
extend Spawner
|
21
|
-
|
22
|
-
spawner do |user|
|
23
|
-
user.name = Faker::Name.name
|
24
|
-
user.email = Faker::Internet.email
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
If you don't want to pollute your class definition, you
|
29
|
-
can of course use it from outside:
|
30
|
-
|
31
|
-
User.spawner do |user|
|
32
|
-
user.name = Faker::Name.name
|
33
|
-
user.email = Faker::Internet.email
|
34
|
-
end
|
35
|
-
|
36
|
-
Then, in your test or in any other place:
|
37
|
-
|
38
|
-
@user = User.spawn
|
39
|
-
|
40
|
-
Or, if you need something special:
|
41
|
-
|
42
|
-
@user = User.spawn do |user|
|
43
|
-
user.name = "Michel Martens"
|
44
|
-
end
|
45
|
-
|
46
|
-
Or even this:
|
47
|
-
|
48
|
-
@user = User.spawn :name => "Michel Martens"
|
49
|
-
|
50
|
-
== Installation
|
51
|
-
|
52
|
-
$ gem sources -a http://gems.github.com (you only have to do this once)
|
53
|
-
$ sudo gem install soveran-spawner
|
54
|
-
|
55
|
-
|
56
|
-
== Thanks
|
57
|
-
|
58
|
-
Thanks to Foca (http://github.com/foca/) for his suggestions and Pedro (http://github.com/peterpunk/) for the gemspec.
|
59
|
-
|
60
|
-
Copyright (c) 2008 Michel Martens for CitrusByte (http://www.citrusbyte.com).
|
61
|
-
Released under the MIT license.
|