machinist 2.0.0.beta2 → 2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -5
- data/Gemfile +2 -8
- data/Gemfile.lock +47 -0
- data/README.markdown +228 -18
- data/Rakefile +5 -20
- data/VERSION +1 -1
- data/lib/generators/machinist/install/install_generator.rb +10 -12
- data/lib/generators/machinist/install/templates/machinist.rb.erb +0 -3
- data/lib/machinist.rb +0 -12
- data/lib/machinist/active_record/blueprint.rb +0 -25
- data/lib/machinist/blueprint.rb +1 -1
- data/lib/machinist/lathe.rb +3 -4
- data/lib/machinist/machinable.rb +2 -8
- data/lib/machinist/version.rb +3 -0
- data/machinist.gemspec +20 -71
- data/spec/active_record_spec.rb +9 -36
- data/spec/blueprint_spec.rb +10 -8
- data/spec/exceptions_spec.rb +2 -2
- data/spec/inheritance_spec.rb +6 -6
- data/spec/machinable_spec.rb +6 -6
- data/spec/spec_helper.rb +0 -4
- metadata +103 -30
- data/lib/machinist/configuration.rb +0 -32
- data/lib/machinist/shop.rb +0 -52
- data/lib/machinist/warehouse.rb +0 -36
- data/spec/shop_spec.rb +0 -94
- data/spec/warehouse_spec.rb +0 -24
data/spec/machinable_spec.rb
CHANGED
@@ -18,7 +18,7 @@ describe Machinist::Machinable do
|
|
18
18
|
MachinableSpecs::Post.clear_blueprints!
|
19
19
|
end
|
20
20
|
|
21
|
-
it "
|
21
|
+
it "makes an object" do
|
22
22
|
MachinableSpecs::Post.blueprint do
|
23
23
|
title { "First Post" }
|
24
24
|
end
|
@@ -28,7 +28,7 @@ describe Machinist::Machinable do
|
|
28
28
|
post.title.should == "First Post"
|
29
29
|
end
|
30
30
|
|
31
|
-
it "
|
31
|
+
it "makes an object from a named blueprint" do
|
32
32
|
MachinableSpecs::Post.blueprint do
|
33
33
|
title { "First Post" }
|
34
34
|
body { "Woot!" }
|
@@ -44,7 +44,7 @@ describe Machinist::Machinable do
|
|
44
44
|
post.body.should == "Woot!"
|
45
45
|
end
|
46
46
|
|
47
|
-
it "
|
47
|
+
it "makes an array of objects" do
|
48
48
|
MachinableSpecs::Post.blueprint do
|
49
49
|
title { "First Post" }
|
50
50
|
end
|
@@ -58,7 +58,7 @@ describe Machinist::Machinable do
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
-
it "
|
61
|
+
it "makes array attributes from the blueprint" do
|
62
62
|
MachinableSpecs::Comment.blueprint { }
|
63
63
|
MachinableSpecs::Post.blueprint do
|
64
64
|
comments(3) { MachinableSpecs::Comment.make }
|
@@ -72,7 +72,7 @@ describe Machinist::Machinable do
|
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
75
|
-
it "
|
75
|
+
it "fails without a blueprint" do
|
76
76
|
expect do
|
77
77
|
MachinableSpecs::Post.make
|
78
78
|
end.should raise_error(Machinist::NoBlueprintError) do |exception|
|
@@ -88,7 +88,7 @@ describe Machinist::Machinable do
|
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
|
-
it "
|
91
|
+
it "fails when calling make! on an unsavable object" do
|
92
92
|
MachinableSpecs::Post.blueprint { }
|
93
93
|
|
94
94
|
expect do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: machinist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 3
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 2
|
8
8
|
- 0
|
9
|
-
|
10
|
-
- beta2
|
11
|
-
version: 2.0.0.beta2
|
9
|
+
version: "2.0"
|
12
10
|
platform: ruby
|
13
11
|
authors:
|
14
12
|
- Pete Yandell
|
@@ -16,21 +14,105 @@ autorequire:
|
|
16
14
|
bindir: bin
|
17
15
|
cert_chain: []
|
18
16
|
|
19
|
-
date:
|
20
|
-
|
21
|
-
|
22
|
-
|
17
|
+
date: 2012-01-13 00:00:00 Z
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: activerecord
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 3
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: mysql
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
hash: 3
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
version: "0"
|
45
|
+
type: :development
|
46
|
+
version_requirements: *id002
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
prerelease: false
|
50
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
type: :development
|
60
|
+
version_requirements: *id003
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rcov
|
63
|
+
prerelease: false
|
64
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
hash: 3
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
type: :development
|
74
|
+
version_requirements: *id004
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rspec
|
77
|
+
prerelease: false
|
78
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
hash: 3
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
type: :development
|
88
|
+
version_requirements: *id005
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rdoc
|
91
|
+
prerelease: false
|
92
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
hash: 3
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
version: "0"
|
101
|
+
type: :development
|
102
|
+
version_requirements: *id006
|
23
103
|
description:
|
24
|
-
email:
|
104
|
+
email:
|
105
|
+
- pete@notahat.com
|
25
106
|
executables: []
|
26
107
|
|
27
108
|
extensions: []
|
28
109
|
|
29
|
-
extra_rdoc_files:
|
30
|
-
|
110
|
+
extra_rdoc_files: []
|
111
|
+
|
31
112
|
files:
|
32
113
|
- .gitignore
|
33
114
|
- Gemfile
|
115
|
+
- Gemfile.lock
|
34
116
|
- MIT-LICENSE
|
35
117
|
- README.markdown
|
36
118
|
- Rakefile
|
@@ -45,29 +127,24 @@ files:
|
|
45
127
|
- lib/machinist/active_record/blueprint.rb
|
46
128
|
- lib/machinist/active_record/lathe.rb
|
47
129
|
- lib/machinist/blueprint.rb
|
48
|
-
- lib/machinist/configuration.rb
|
49
130
|
- lib/machinist/exceptions.rb
|
50
131
|
- lib/machinist/lathe.rb
|
51
132
|
- lib/machinist/machinable.rb
|
52
|
-
- lib/machinist/
|
53
|
-
- lib/machinist/warehouse.rb
|
133
|
+
- lib/machinist/version.rb
|
54
134
|
- machinist.gemspec
|
55
135
|
- spec/active_record_spec.rb
|
56
136
|
- spec/blueprint_spec.rb
|
57
137
|
- spec/exceptions_spec.rb
|
58
138
|
- spec/inheritance_spec.rb
|
59
139
|
- spec/machinable_spec.rb
|
60
|
-
- spec/shop_spec.rb
|
61
140
|
- spec/spec_helper.rb
|
62
141
|
- spec/support/active_record_environment.rb
|
63
|
-
- spec/warehouse_spec.rb
|
64
|
-
has_rdoc: true
|
65
142
|
homepage: http://github.com/notahat/machinist
|
66
143
|
licenses: []
|
67
144
|
|
68
145
|
post_install_message:
|
69
|
-
rdoc_options:
|
70
|
-
|
146
|
+
rdoc_options: []
|
147
|
+
|
71
148
|
require_paths:
|
72
149
|
- lib
|
73
150
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -82,18 +159,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
82
159
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
160
|
none: false
|
84
161
|
requirements:
|
85
|
-
- - "
|
162
|
+
- - ">="
|
86
163
|
- !ruby/object:Gem::Version
|
87
|
-
hash:
|
164
|
+
hash: 3
|
88
165
|
segments:
|
89
|
-
-
|
90
|
-
|
91
|
-
- 1
|
92
|
-
version: 1.3.1
|
166
|
+
- 0
|
167
|
+
version: "0"
|
93
168
|
requirements: []
|
94
169
|
|
95
|
-
rubyforge_project:
|
96
|
-
rubygems_version: 1.
|
170
|
+
rubyforge_project: machinist
|
171
|
+
rubygems_version: 1.8.10
|
97
172
|
signing_key:
|
98
173
|
specification_version: 3
|
99
174
|
summary: Fixtures aren't fun. Machinist is.
|
@@ -103,7 +178,5 @@ test_files:
|
|
103
178
|
- spec/exceptions_spec.rb
|
104
179
|
- spec/inheritance_spec.rb
|
105
180
|
- spec/machinable_spec.rb
|
106
|
-
- spec/shop_spec.rb
|
107
181
|
- spec/spec_helper.rb
|
108
182
|
- spec/support/active_record_environment.rb
|
109
|
-
- spec/warehouse_spec.rb
|
@@ -1,32 +0,0 @@
|
|
1
|
-
module Machinist
|
2
|
-
|
3
|
-
# Configure Machinist.
|
4
|
-
#
|
5
|
-
# To change Machinist configuration, do something like this in your
|
6
|
-
# config/environments/test.rb or somewhere similar:
|
7
|
-
#
|
8
|
-
# Machinist.configure do |config|
|
9
|
-
# config.cache_objects = false
|
10
|
-
# end
|
11
|
-
class Configuration
|
12
|
-
# Set this to false to disable object caching. Defaults to true.
|
13
|
-
attr_accessor :cache_objects
|
14
|
-
|
15
|
-
def cache_objects? #:nodoc:
|
16
|
-
@cache_objects
|
17
|
-
end
|
18
|
-
|
19
|
-
def initialize #:nodoc:
|
20
|
-
self.cache_objects = true
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.configuration
|
25
|
-
@configuration ||= Configuration.new
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.configure
|
29
|
-
yield(configuration)
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
data/lib/machinist/shop.rb
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
module Machinist
|
2
|
-
|
3
|
-
# The shop takes care of caching database objects.
|
4
|
-
#
|
5
|
-
# Calling make! on a class requests objects from the shop; you don't
|
6
|
-
# normally access the shop directly.
|
7
|
-
#
|
8
|
-
# Read more about object caching on the
|
9
|
-
# wiki[http://wiki.github.com/notahat/machinist/object-caching].
|
10
|
-
class Shop
|
11
|
-
|
12
|
-
# Return the singleton Shop instance.
|
13
|
-
def self.instance
|
14
|
-
@instance ||= Shop.new
|
15
|
-
end
|
16
|
-
|
17
|
-
def initialize #:nodoc:
|
18
|
-
reset!
|
19
|
-
end
|
20
|
-
|
21
|
-
# Throw out the entire collection of cached objects.
|
22
|
-
def reset!
|
23
|
-
@warehouse = Warehouse.new
|
24
|
-
restock
|
25
|
-
end
|
26
|
-
|
27
|
-
# Restock the shop with all the cached objects we've got.
|
28
|
-
#
|
29
|
-
# This should be called before each test.
|
30
|
-
def restock
|
31
|
-
@back_room = @warehouse.clone
|
32
|
-
end
|
33
|
-
|
34
|
-
# Buy a (possibly cached) object from the shop.
|
35
|
-
#
|
36
|
-
# This is just like constructing an object by calling Blueprint#make!,
|
37
|
-
# but it will return a previously cached object if one is available.
|
38
|
-
def buy(blueprint, attributes = {})
|
39
|
-
raise BlueprintCantSaveError.new(blueprint) unless blueprint.respond_to?(:make!)
|
40
|
-
|
41
|
-
shelf = @back_room[blueprint, attributes]
|
42
|
-
if shelf.empty?
|
43
|
-
object = blueprint.outside_transaction { blueprint.make!(attributes) }
|
44
|
-
@warehouse[blueprint, attributes] << blueprint.box(object)
|
45
|
-
object
|
46
|
-
else
|
47
|
-
blueprint.unbox(shelf.shift)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
end
|
data/lib/machinist/warehouse.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
module Machinist
|
2
|
-
|
3
|
-
# A Warehouse is a hash supports lists as keys.
|
4
|
-
#
|
5
|
-
# It's used for storing cached objects created by Machinist::Shop.
|
6
|
-
#
|
7
|
-
# warehouse[1, 2] = "Hello, world!"
|
8
|
-
# warehouse[1, 2] # => "Hello, world!"
|
9
|
-
class Warehouse < Hash
|
10
|
-
|
11
|
-
# Assign a value for the given list of keys.
|
12
|
-
def []=(*keys)
|
13
|
-
value = keys.pop
|
14
|
-
super(keys, value)
|
15
|
-
end
|
16
|
-
|
17
|
-
# Return the value for the given list of keys.
|
18
|
-
#
|
19
|
-
# If the list of keys doesn't exist in the hash, this assigns a new empty
|
20
|
-
# array to that list of keys.
|
21
|
-
def [](*keys)
|
22
|
-
self[*keys] = [] if !has_key?(keys)
|
23
|
-
super(keys)
|
24
|
-
end
|
25
|
-
|
26
|
-
# Return a new warehouse with the same keys, and dups of all the values.
|
27
|
-
def clone
|
28
|
-
clone = Warehouse.new
|
29
|
-
each_pair do |key, value|
|
30
|
-
clone[*key] = value.dup
|
31
|
-
end
|
32
|
-
clone
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
36
|
-
end
|
data/spec/shop_spec.rb
DELETED
@@ -1,94 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
require 'support/active_record_environment'
|
3
|
-
|
4
|
-
describe Machinist::Shop do
|
5
|
-
|
6
|
-
before(:each) do
|
7
|
-
@shop = Machinist::Shop.new
|
8
|
-
end
|
9
|
-
|
10
|
-
def fake_a_test
|
11
|
-
ActiveRecord::Base.transaction do
|
12
|
-
@shop.restock
|
13
|
-
yield
|
14
|
-
raise ActiveRecord::Rollback
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should cache an object" do
|
19
|
-
blueprint = Machinist::ActiveRecord::Blueprint.new(Post) { }
|
20
|
-
|
21
|
-
post_a, post_b = nil, nil
|
22
|
-
fake_a_test { post_a = @shop.buy(blueprint) }
|
23
|
-
fake_a_test { post_b = @shop.buy(blueprint) }
|
24
|
-
|
25
|
-
post_b.should == post_a
|
26
|
-
post_b.should_not equal(post_a)
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should cache an object with attributes" do
|
30
|
-
blueprint = Machinist::ActiveRecord::Blueprint.new(Post) { }
|
31
|
-
|
32
|
-
post_a, post_b = nil, nil
|
33
|
-
fake_a_test { post_a = @shop.buy(blueprint, :title => "Test Title") }
|
34
|
-
fake_a_test { post_b = @shop.buy(blueprint, :title => "Test Title") }
|
35
|
-
|
36
|
-
post_b.should == post_a
|
37
|
-
post_b.should_not equal(post_a)
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should not confuse objects with different blueprints" do
|
41
|
-
blueprint_a = Machinist::ActiveRecord::Blueprint.new(Post) { }
|
42
|
-
blueprint_b = Machinist::ActiveRecord::Blueprint.new(Post) { }
|
43
|
-
|
44
|
-
post_a, post_b = nil, nil
|
45
|
-
fake_a_test { post_a = @shop.buy(blueprint_a) }
|
46
|
-
fake_a_test { post_b = @shop.buy(blueprint_b) }
|
47
|
-
|
48
|
-
post_b.should_not == post_a
|
49
|
-
end
|
50
|
-
|
51
|
-
it "should not confuse objects with the same blueprint but different attributes" do
|
52
|
-
blueprint = Machinist::ActiveRecord::Blueprint.new(Post) { }
|
53
|
-
|
54
|
-
post_a, post_b = nil, nil
|
55
|
-
fake_a_test { post_a = @shop.buy(blueprint, :title => "A Title") }
|
56
|
-
fake_a_test { post_b = @shop.buy(blueprint, :title => "Not A Title") }
|
57
|
-
|
58
|
-
post_b.should_not == post_a
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should cache multiple similar objects" do
|
62
|
-
blueprint = Machinist::ActiveRecord::Blueprint.new(Post) { }
|
63
|
-
|
64
|
-
post_a, post_b = nil, nil
|
65
|
-
fake_a_test do
|
66
|
-
post_a = @shop.buy(blueprint, :title => "Test Title")
|
67
|
-
post_b = @shop.buy(blueprint, :title => "Test Title")
|
68
|
-
post_b.should_not == post_a
|
69
|
-
end
|
70
|
-
|
71
|
-
fake_a_test do
|
72
|
-
@shop.buy(blueprint, :title => "Test Title").should == post_a
|
73
|
-
@shop.buy(blueprint, :title => "Test Title").should == post_b
|
74
|
-
post_c = @shop.buy(blueprint, :title => "Test Title")
|
75
|
-
post_c.should_not == post_a
|
76
|
-
post_c.should_not == post_b
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should ensure future copies of a cached object do not reflect changes to the original" do
|
81
|
-
blueprint = Machinist::ActiveRecord::Blueprint.new(Post) { }
|
82
|
-
|
83
|
-
post_a, post_b = nil, nil
|
84
|
-
fake_a_test do
|
85
|
-
post_a = @shop.buy(blueprint, :title => "Test Title")
|
86
|
-
post_a.title = "Changed Title"
|
87
|
-
post_a.save!
|
88
|
-
end
|
89
|
-
fake_a_test { post_b = @shop.buy(blueprint, :title => "Test Title") }
|
90
|
-
|
91
|
-
post_b.title.should == "Test Title"
|
92
|
-
end
|
93
|
-
|
94
|
-
end
|