lawrencepit-machinery 0.3 → 0.3.1
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 +20 -0
- data/README.markdown +132 -0
- data/lib/machinery/active_record.rb +43 -0
- metadata +4 -1
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Lawrence Pit
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
Machinery
|
2
|
+
=========
|
3
|
+
|
4
|
+
*Machinery to create object graphs.*
|
5
|
+
|
6
|
+
Machinery helps to create object graphs for use in tests without needing fixtures.
|
7
|
+
These object graphs are easily re-usable in Test::Unit, RSpec, Cucumber and
|
8
|
+
in rake tasks to populate a database.
|
9
|
+
|
10
|
+
Why write stuff like:
|
11
|
+
|
12
|
+
users(:admin).should be_nil
|
13
|
+
|
14
|
+
when you can just as well write:
|
15
|
+
|
16
|
+
@user_admin.should be_nil
|
17
|
+
|
18
|
+
Use less magic. Less code. Makes software softer.
|
19
|
+
|
20
|
+
|
21
|
+
Usage
|
22
|
+
-----
|
23
|
+
|
24
|
+
In /spec/scenarios.rb
|
25
|
+
|
26
|
+
scenario :earth_ships do
|
27
|
+
@titanic = Ship.create!(:name => "Titanic")
|
28
|
+
@pirates = Ship.create!(:name => "Pirate")
|
29
|
+
end
|
30
|
+
|
31
|
+
scenario :space_ships do
|
32
|
+
@apollo = Ship.create!(:name => "Apollo")
|
33
|
+
@uss_enterprise = Ship.create!(:name => "USS Enterprise")
|
34
|
+
end
|
35
|
+
|
36
|
+
scenario :all_ships do
|
37
|
+
scenarios :earth_ships, :space_ships
|
38
|
+
@sunken_ship = Ship.create!(:name => "Sunk")
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
In /spec/spec_helper.rb
|
43
|
+
|
44
|
+
require 'machinery'
|
45
|
+
Machinery.load(File.dirname(__FILE__) + '/scenarios.rb')
|
46
|
+
|
47
|
+
|
48
|
+
In /spec/models/ship_spec.rb
|
49
|
+
|
50
|
+
describe Ship do
|
51
|
+
scenarios :all_ships
|
52
|
+
|
53
|
+
it "should create a pirates ship" do
|
54
|
+
@pirates.should_not be_nil
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should have 5 ships" do
|
58
|
+
Ship.count.should == 5
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
Installation
|
65
|
+
------------
|
66
|
+
|
67
|
+
### Rails installation (Test::Unit)
|
68
|
+
|
69
|
+
#### As a Gem
|
70
|
+
|
71
|
+
Use this if you prefer to use versioned releases of Machinery.
|
72
|
+
Specify the gem dependency in your config/environment.rb file:
|
73
|
+
|
74
|
+
Rails::Initializer.run do |config|
|
75
|
+
config.gem "lawrencepit-machinery", :lib => "machinery", :source => "http://gems.github.com"
|
76
|
+
end
|
77
|
+
|
78
|
+
Then:
|
79
|
+
|
80
|
+
$ rake gems:install
|
81
|
+
$ rake gems:unpack
|
82
|
+
|
83
|
+
#### As a Plugin
|
84
|
+
|
85
|
+
Use this if you prefer to use the edge version of Machinery:
|
86
|
+
|
87
|
+
$ script/plugin install git://github.com/lawrencepit/machinery.git
|
88
|
+
|
89
|
+
### Rails installation (RSpec)
|
90
|
+
|
91
|
+
If you're using Machinery with RSpec, it is recommended that you add config.gem lines
|
92
|
+
for RSpec and Machinery in your config/environment/test.rb file, but do not ask
|
93
|
+
Rails to load the RSpec and Machinery libraries:
|
94
|
+
|
95
|
+
config.gem 'rspec', :lib => false
|
96
|
+
config.gem 'rspec-rails', :lib => false
|
97
|
+
config.gem 'lawrencepit-machinery', :lib => false, :source => 'http://gems.github.com'
|
98
|
+
|
99
|
+
Then require machinery from your spec/spec_helper.rb file, before Spec::Runner is
|
100
|
+
configured:
|
101
|
+
|
102
|
+
# requires for RSpec
|
103
|
+
require 'machinery'
|
104
|
+
Spec::Runner.configure do |config|
|
105
|
+
# ...
|
106
|
+
|
107
|
+
You should not need to require anything besides the top-level machinery library.
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
Credits
|
112
|
+
-------
|
113
|
+
|
114
|
+
Written by [Lawrence Pit](http://lawrencepit.com).
|
115
|
+
|
116
|
+
Thanks to
|
117
|
+
[Machinist](http://github.com/notahat/machinist),
|
118
|
+
[ModelStubbing](http://github.com/technoweenie/model_stubbing),
|
119
|
+
[Shoulda](http://github.com/thoughtbot/shoulda) and
|
120
|
+
[FixtureScenarios](http://errtheblog.com/posts/61-fixin-fixtures)
|
121
|
+
for inspiration.
|
122
|
+
|
123
|
+
After using model_stubbing on a relatively large project for over a year I decided
|
124
|
+
a simpler solution was needed. Object graphs need to be easily re-usable by specs,
|
125
|
+
unit tests, cucumber stories and ad-hoc database loading.
|
126
|
+
|
127
|
+
Machinery works especially great in combination with Machinist.
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
----
|
132
|
+
Copyright (c) 2009 Lawrence Pit, released under the MIT license
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Machinery
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
@@toys = []
|
5
|
+
@@recording = false
|
6
|
+
|
7
|
+
def self.record
|
8
|
+
@@recording = true
|
9
|
+
yield
|
10
|
+
@@recording = false
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.clear
|
14
|
+
@@toys.each { |toy| toy.destroy }
|
15
|
+
@@toys = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.created_with_machinery(obj)
|
19
|
+
@@toys << obj if @@recording
|
20
|
+
end
|
21
|
+
|
22
|
+
module Extensions
|
23
|
+
private
|
24
|
+
|
25
|
+
def self.included(base)
|
26
|
+
base.class_eval do
|
27
|
+
alias_method :create_without_machinery, :create
|
28
|
+
alias_method :create, :create_with_machinery
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def create_with_machinery
|
33
|
+
id = create_without_machinery
|
34
|
+
Machinery::ActiveRecord.created_with_machinery(self)
|
35
|
+
id
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
ActiveRecord::Base.send(:include, Machinery::ActiveRecord::Extensions)
|
43
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lawrencepit-machinery
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lawrence Pit
|
@@ -22,7 +22,10 @@ extensions: []
|
|
22
22
|
extra_rdoc_files: []
|
23
23
|
|
24
24
|
files:
|
25
|
+
- README.markdown
|
26
|
+
- MIT-LICENSE
|
25
27
|
- lib/machinery.rb
|
28
|
+
- lib/machinery/active_record.rb
|
26
29
|
has_rdoc: false
|
27
30
|
homepage: http://github.com/lawrencepit/machinery
|
28
31
|
post_install_message:
|