Skip to content

Commit

Permalink
Handle named blueprints without a master better.
Browse files Browse the repository at this point in the history
This raises a more descriptive error, and mentions the need for a master
blueprint in the docs.

Note that the docs refer to a master blueprint as a "default blueprint".
I should unify this language.
  • Loading branch information
notahat committed Nov 29, 2009
1 parent 93f8338 commit bcdcccb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.markdown
Expand Up @@ -187,6 +187,8 @@ 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.


### Belongs\_to Associations

Expand Down
8 changes: 7 additions & 1 deletion lib/machinist.rb
Expand Up @@ -11,7 +11,13 @@ def self.run(adapter, object, *args)
named_blueprint = object.class.blueprint(args.shift) if args.first.is_a?(Symbol)
attributes = args.pop || {}

raise "No blueprint for class #{object.class}" if blueprint.nil?
if blueprint.nil?
if named_blueprint
raise "Can't construct an object from a named blueprint without a default blueprint for class #{object.class}"
else
raise "No blueprint for class #{object.class}"
end
end

lathe = self.new(adapter, object, attributes)
lathe.instance_eval(&named_blueprint) if named_blueprint
Expand Down
11 changes: 9 additions & 2 deletions spec/machinist_spec.rb
Expand Up @@ -29,7 +29,7 @@ class Son < Dad
end

it "should raise for make on a class with no blueprint" do
lambda { Person.make }.should raise_error(RuntimeError)
lambda { Person.make }.should raise_error(RuntimeError, "No blueprint for class MachinistSpecs::Person")
end

it "should set an attribute on the constructed object from a constant in the blueprint" do
Expand Down Expand Up @@ -120,7 +120,7 @@ class Son < Dad
end
@person = Person.make(:admin)
end

it "should override an attribute from the parent blueprint in the child blueprint" do
@person.admin.should == true
end
Expand All @@ -138,6 +138,13 @@ class Son < Dad
Person.blueprint(:bar) { }
Person.named_blueprints.to_set.should == [:admin, :foo, :bar].to_set
end

it "should raise a sensible error when calling a named blueprint with no master" do
Post.blueprint(:foo) { }
lambda { Post.make(:foo) }.should raise_error(
RuntimeError, "Can't construct an object from a named blueprint without a default blueprint for class MachinistSpecs::Post"
)
end
end

describe "blueprint inheritance" do
Expand Down

0 comments on commit bcdcccb

Please sign in to comment.