Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Better exceptions.

  • Loading branch information...
commit 566d2f6c5fb108b7d58f73bed31c18efbc946636 1 parent 2ecccec
Pete Yandell notahat authored
1  lib/machinist.rb
... ... @@ -1,4 +1,5 @@
1 1 require 'machinist/blueprint'
  2 +require 'machinist/exceptions'
2 3 require 'machinist/lathe'
3 4 require 'machinist/machinable'
4 5 require 'machinist/shop'
33 lib/machinist/exceptions.rb
... ... @@ -0,0 +1,33 @@
  1 +module Machinist
  2 +
  3 + # Raised when make! is called on a class whose blueprints don't support
  4 + # saving.
  5 + class BlueprintCantSaveError < RuntimeError
  6 + attr_reader :blueprint
  7 +
  8 + def initialize(blueprint)
  9 + @blueprint = blueprint
  10 + end
  11 +
  12 + def message
  13 + "make! is not supported by blueprints for class #{@blueprint.klass.name}"
  14 + end
  15 + end
  16 +
  17 +
  18 + # Raised when calling make on a class with no corresponding blueprint
  19 + # defined.
  20 + class NoBlueprintError < RuntimeError
  21 + attr_reader :klass, :name
  22 +
  23 + def initialize(klass, name)
  24 + @klass = klass
  25 + @name = name
  26 + end
  27 +
  28 + def message
  29 + "No #{@name} blueprint defined for class #{@klass.name}"
  30 + end
  31 + end
  32 +
  33 +end
2  lib/machinist/machinable.rb
@@ -84,7 +84,7 @@ def decode_args_to_make(*args) #:nodoc:
84 84
85 85 @blueprints ||= {}
86 86 blueprint = @blueprints[name]
87   - raise "No blueprint defined" unless blueprint
  87 + raise NoBlueprintError.new(self, name) unless blueprint
88 88
89 89 if count.nil?
90 90 yield(blueprint, attributes)
2  lib/machinist/shop.rb
@@ -36,6 +36,8 @@ def restock
36 36 # This is just like constructing an object by calling Blueprint#make!,
37 37 # but it will return a previously cached object if one is available.
38 38 def buy(blueprint, attributes = {})
  39 + raise BlueprintCantSaveError.new(blueprint) unless blueprint.respond_to?(:make!)
  40 +
39 41 shelf = @back_room[blueprint, attributes]
40 42 if shelf.empty?
41 43 object = blueprint.outside_transaction { blueprint.make!(attributes) }
20 spec/exceptions_spec.rb
... ... @@ -0,0 +1,20 @@
  1 +require File.dirname(__FILE__) + '/spec_helper'
  2 +
  3 +describe Machinist, "exceptions" do
  4 +
  5 + describe Machinist::BlueprintCantSaveError do
  6 + it "should present the right message" do
  7 + blueprint = Machinist::Blueprint.new(String) { }
  8 + exception = Machinist::BlueprintCantSaveError.new(blueprint)
  9 + exception.message.should == "make! is not supported by blueprints for class String"
  10 + end
  11 + end
  12 +
  13 + describe Machinist::NoBlueprintError do
  14 + it "should present the right message" do
  15 + exception = Machinist::NoBlueprintError.new(String, :master)
  16 + exception.message.should == "No master blueprint defined for class String"
  17 + end
  18 + end
  19 +
  20 +end
25 spec/machinable_spec.rb
@@ -73,8 +73,29 @@ class Comment
73 73 end
74 74
75 75 it "should fail without a blueprint" do
76   - lambda { MachinableSpecs::Post.make }.should raise_error("No blueprint defined")
77   - lambda { MachinableSpecs::Post.make(:some_name) }.should raise_error("No blueprint defined")
  76 + expect do
  77 + MachinableSpecs::Post.make
  78 + end.should raise_error(Machinist::NoBlueprintError) do |exception|
  79 + exception.klass.should == MachinableSpecs::Post
  80 + exception.name.should == :master
  81 + end
  82 +
  83 + expect do
  84 + MachinableSpecs::Post.make(:some_name)
  85 + end.should raise_error(Machinist::NoBlueprintError) do |exception|
  86 + exception.klass.should == MachinableSpecs::Post
  87 + exception.name.should == :some_name
  88 + end
  89 + end
  90 +
  91 + it "should fail when calling make! on an unsavable object" do
  92 + MachinableSpecs::Post.blueprint { }
  93 +
  94 + expect do
  95 + MachinableSpecs::Post.make!
  96 + end.should raise_error(Machinist::BlueprintCantSaveError) do |exception|
  97 + exception.blueprint.klass.should == MachinableSpecs::Post
  98 + end
78 99 end
79 100
80 101 end

0 comments on commit 566d2f6

Please sign in to comment.
Something went wrong with that request. Please try again.