Skip to content

Commit

Permalink
Merge branch 'better_error'
Browse files Browse the repository at this point in the history
  • Loading branch information
notahat committed Dec 14, 2009
2 parents 0714ae1 + a44c763 commit 1a17ab8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ def self.run(adapter, object, *args)
end

blueprint = object.class.blueprint
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

attributes = args.pop || {}

Expand Down
13 changes: 10 additions & 3 deletions spec/machinist_spec.rb
Original file line number Diff line number Diff line change
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 @@ -113,7 +113,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 @@ -131,12 +131,19 @@ class Son < Dad
Person.blueprint(:bar) { }
Person.named_blueprints.to_set.should == [:admin, :foo, :bar].to_set
end

it "should raise exception for make if named blueprint is not defined" do
lambda { Person.make(:bogus) }.should raise_error(
RuntimeError, "No blueprint named 'bogus' defined for class MachinistSpecs::Person"
)
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 1a17ab8

Please sign in to comment.