Permalink
Browse files

Merge branch 'master' into strategies

  • Loading branch information...
2 parents abca63c + 49393d8 commit 486519ea6634e3a940a9c3d006c9893b7493354b @notahat committed Mar 2, 2013
Showing with 75 additions and 85 deletions.
  1. +29 −29 Gemfile.lock
  2. +34 −38 README.markdown
  3. +2 −7 Rakefile
  4. +1 −1 lib/machinist/blueprint.rb
  5. +3 −3 lib/machinist/lathe.rb
  6. +0 −1 machinist.gemspec
  7. +2 −2 spec/inheritance_spec.rb
  8. +4 −4 spec/machinable_spec.rb
View
@@ -1,38 +1,39 @@
PATH
remote: .
specs:
- machinist (2.0.0.beta2)
+ machinist (2.0)
GEM
remote: http://rubygems.org/
specs:
- activemodel (3.0.9)
- activesupport (= 3.0.9)
- builder (~> 2.1.2)
- i18n (~> 0.5.0)
- activerecord (3.0.9)
- activemodel (= 3.0.9)
- activesupport (= 3.0.9)
- arel (~> 2.0.10)
- tzinfo (~> 0.3.23)
- activesupport (3.0.9)
- arel (2.0.10)
- builder (2.1.2)
- diff-lcs (1.1.2)
- i18n (0.5.0)
- mysql (2.8.1)
- rake (0.9.2)
- rcov (0.9.9)
- rdoc (3.6.1)
- rspec (2.6.0)
- rspec-core (~> 2.6.0)
- rspec-expectations (~> 2.6.0)
- rspec-mocks (~> 2.6.0)
- rspec-core (2.6.4)
- rspec-expectations (2.6.0)
- diff-lcs (~> 1.1.2)
- rspec-mocks (2.6.0)
- tzinfo (0.3.28)
+ activemodel (3.2.12)
+ activesupport (= 3.2.12)
+ builder (~> 3.0.0)
+ activerecord (3.2.12)
+ activemodel (= 3.2.12)
+ activesupport (= 3.2.12)
+ arel (~> 3.0.2)
+ tzinfo (~> 0.3.29)
+ activesupport (3.2.12)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ arel (3.0.2)
+ builder (3.0.4)
+ diff-lcs (1.2.1)
+ i18n (0.6.4)
+ multi_json (1.6.1)
+ mysql (2.9.1)
+ rake (10.0.3)
+ rdoc (4.0.0)
+ rspec (2.13.0)
+ rspec-core (~> 2.13.0)
+ rspec-expectations (~> 2.13.0)
+ rspec-mocks (~> 2.13.0)
+ rspec-core (2.13.0)
+ rspec-expectations (2.13.0)
+ diff-lcs (>= 1.1.3, < 2.0)
+ rspec-mocks (2.13.0)
+ tzinfo (0.3.35)
PLATFORMS
ruby
@@ -42,6 +43,5 @@ DEPENDENCIES
machinist!
mysql
rake
- rcov
rdoc
rspec
View
@@ -2,26 +2,12 @@
*Fixtures aren't fun. Machinist is.*
-Machinist 2 is **still in beta**!
-
-If you're using Rails 3, you'll want to give Machinist 2 a go, but be aware
-that the documentation is still patchy.
-
-That said, have a look at [the
-specs](https://github.com/notahat/machinist/tree/master/spec), starting with
-[the spec for
-Machinable](https://github.com/notahat/machinist/blob/master/spec/machinable_spec.rb).
-No, really, have a look. I wrote this code to be read, and the specs do a
-pretty clean job of documenting what it all does.
-
-If, on the other hand, you want the tried, tested, and well-documented official
-release version of Machinist, [then go with Machinist
-1](http://github.com/notahat/machinist/tree/1.0-maintenance).
-
- [Home page](http://github.com/notahat/machinist)
- [Google group](http://groups.google.com/group/machinist-users), for support
- [Bug tracker](http://github.com/notahat/machinist/issues), for reporting Machinist bugs
+If you want Machinist 1, [go here](http://github.com/notahat/machinist/tree/1.0-maintenance).
+
## Introduction
@@ -98,25 +84,25 @@ See [the wiki](http://wiki.github.com/notahat/machinist/rails-2).
A blueprint describes how to generate an object. The blueprint takes care of
providing attributes that your test doesn't care about, leaving you to focus on
just the attributes that are important for the test.
-
+
A simple blueprint might look like this:
-
+
Post.blueprint do
title { "A Post" }
body { "Lorem ipsum..." }
end
-
+
You can then construct a Post from this blueprint with:
Post.make!
-
+
When you call `make!`, Machinist calls `Post.new`, then runs through the
attributes in your blueprint, calling the block for each attribute to generate
a value. It then saves and reloads the Post. (It throws an exception if the
Post can't be saved.)
You can override values defined in the blueprint by passing a hash to make:
-
+
Post.make!(:title => "A Specific Title")
If you want to generate an object without saving it to the database, replace
@@ -131,7 +117,7 @@ within the attribute block to get a unique serial number for the object.
User.blueprint do
username { "user-#{sn}" }
end
-
+
### Associations
@@ -140,20 +126,20 @@ If your object needs associated objects, you can generate them like this:
Comment.blueprint do
post { Post.make }
end
-
+
Calling `Comment.make!` will construct a Comment and its associated Post, and
save both.
Machinist is smart enough to look at the association and work out what sort of
object it needs to create, so you can shorten the above blueprint to:
-
+
Comment.blueprint do
post
end
If you want to override the value for post when constructing the comment, you
can do this:
-
+
post = Post.make(:title => "A particular title)
comment = Comment.make(:post => post)
@@ -170,35 +156,35 @@ multiple associated objects like this:
Named blueprints let you define variations on an object. For example, suppose
some of your Users are administrators:
-
+
User.blueprint do
name { "User #{sn}" }
email { "user-#{sn}@example.com" }
end
-
+
User.blueprint(:admin) do
name { "Admin User #{sn}" }
admin { true }
end
-
+
Calling:
-
+
User.make!(:admin)
-
+
will use the `:admin` blueprint.
-
+
Named blueprints call the default blueprint to set any attributes not
specifically provided, so in this example the `email` attribute will still be
generated even for an admin user.
-
+
You must define a default blueprint for any class that has a named blueprint,
even if the default blueprint is empty.
### Blueprints on Plain Old Ruby Objects
Machinist also works with plain old Ruby objects. Let's say you have a class like:
-
+
class Post
extend Machinist::Machinable
@@ -207,7 +193,7 @@ Machinist also works with plain old Ruby objects. Let's say you have a class lik
end
You can blueprint the Post class just like anything else:
-
+
Post.blueprint do
title { "A title!" }
body { "A body!" }
@@ -219,19 +205,28 @@ And `Post.make` will construct a new Post.
### Other Tricks
You can refer to already assigned attributes when constructing a new attribute:
-
+
Post.blueprint do
author { "Author #{sn}" }
body { "Post by #{object.author}" }
end
+### More Details
+
+Read the code! No, really. I wrote this code to be read.
+
+Check out [the specs](https://github.com/notahat/machinist/tree/master/spec),
+starting with [the spec for
+Machinable](https://github.com/notahat/machinist/blob/master/spec/machinable_spec.rb).
+
+
## Compatibility
I've tested this with:
-Ruby versions: 1.8.7, 1.9.2
-Rails versions: 2.3, 3.0
+Ruby versions: 1.8.7, 1.9.2, 1.9.3, 2.0.0
+Rails versions: 2.3, 3.0, 3.2
It may well be happy with other versions too, but I'm not promising anything.
Compatibility patches are welcome.
@@ -257,7 +252,8 @@ If you want to submit a patch:
In active use in a number of large Rails 2 apps.
-Development has been sporadic, but is picking up again.
+Development is sporadic at best, as I find myself with less and less need for
+factories in tests. If anybody wants to take over maintenance, let me know.
## Contributors
View
@@ -7,15 +7,10 @@ require 'rspec/core/rake_task'
require 'rdoc/task'
-RSpec::Core::RakeTask.new
-
-RSpec::Core::RakeTask.new(:rcov) do |spec|
- spec.rcov = true
- spec.rcov_opts = ['--exclude', 'spec', '--exclude', '.rvm']
-end
+RSpec::Core::RakeTask.new(:spec)
desc 'Run the specs.'
-task :default => :rcov
+task :default => :spec
RDoc::Task.new(:rdoc) do |rdoc|
@@ -50,7 +50,7 @@ def parent_blueprint
find_blueprint_in_superclass_chain(@parent)
end
end
-
+
# Yields the parent blueprint, its parent blueprint, etc.
def each_ancestor
ancestor = parent_blueprint
View
@@ -57,17 +57,17 @@ def make_one_value(attribute, args) #:nodoc:
raise_argument_error(attribute) unless args.empty?
yield
end
-
+
def assign_attribute(key, value) #:nodoc:
@assigned_attributes[key.to_sym] = value
end
-
+
def attribute_assigned?(key) #:nodoc:
@assigned_attributes.has_key?(key.to_sym)
end
def raise_argument_error(attribute) #:nodoc:
- raise ArgumentError.new("Invalid arguments to attribute #{attribute} in blueprint")
+ raise ArgumentError.new("Invalid arguments to attribute #{attribute} in blueprint")
end
end
View
@@ -21,7 +21,6 @@ Gem::Specification.new do |s|
s.add_development_dependency "activerecord"
s.add_development_dependency "mysql"
s.add_development_dependency "rake"
- s.add_development_dependency "rcov"
s.add_development_dependency "rspec"
s.add_development_dependency "rdoc"
end
View
@@ -6,7 +6,7 @@ class Grandpa
extend Machinist::Machinable
attr_accessor :name, :age
end
-
+
class Dad < Grandpa
extend Machinist::Machinable
attr_accessor :name, :age
@@ -28,7 +28,7 @@ class Son < Dad
end
child_blueprint = Machinist::Blueprint.new(OpenStruct, :parent => parent_blueprint) do
- name { "Bill" }
+ name { "Bill" }
end
child = child_blueprint.make
View
@@ -60,7 +60,7 @@ class Comment
it "makes array attributes from the blueprint" do
MachinableSpecs::Comment.blueprint { }
- MachinableSpecs::Post.blueprint do
+ MachinableSpecs::Post.blueprint do
comments(3) { MachinableSpecs::Comment.make }
end
@@ -75,14 +75,14 @@ class Comment
it "fails without a blueprint" do
expect do
MachinableSpecs::Post.make
- end.should raise_error(Machinist::NoBlueprintError) do |exception|
+ end.to raise_error(Machinist::NoBlueprintError) do |exception|
exception.klass.should == MachinableSpecs::Post
exception.name.should == :master
end
expect do
MachinableSpecs::Post.make(:some_name)
- end.should raise_error(Machinist::NoBlueprintError) do |exception|
+ end.to raise_error(Machinist::NoBlueprintError) do |exception|
exception.klass.should == MachinableSpecs::Post
exception.name.should == :some_name
end
@@ -93,7 +93,7 @@ class Comment
expect do
MachinableSpecs::Post.make!
- end.should raise_error(Machinist::BlueprintCantSaveError) do |exception|
+ end.to raise_error(Machinist::BlueprintCantSaveError) do |exception|
exception.blueprint.klass.should == MachinableSpecs::Post
end
end

0 comments on commit 486519e

Please sign in to comment.