Permalink
Browse files

ask for custom constraints by name

  • Loading branch information...
1 parent 33ffbca commit f40ba57924668e8fce98d237ebb318f9b5d6855f @tcrayford committed May 1, 2012
Showing with 34 additions and 4 deletions.
  1. +11 −0 lib/raptor.rb
  2. +4 −4 lib/raptor/router.rb
  3. +19 −0 spec/app_spec.rb
View
@@ -43,9 +43,20 @@ def injectables
def find_method(method_path)
DelegateFinder.new(@app_module, method_path).find
end
+
+ def constraint_named(name)
+ raise NoSuchConstraint.new(name) unless constraint_exists?(name)
+ @app_module::Constraints.const_get(name)
+ end
+
+ def constraint_exists?(name)
+ @app_module.const_defined?(:Constraints) &&
+ @app_module::Constraints.const_defined?(name)
+ end
end
class ValidationError < RuntimeError; end
+ class NoSuchConstraint < RuntimeError; end
module Util
def self.underscore(string)
View
@@ -202,13 +202,13 @@ def custom_constraints
end
class Constraints
- def initialize(app_module)
- @app_module = app_module
+ def initialize(app)
+ @app = app
end
def matching(name)
- constraint = @app_module::Constraints.const_get(Util.camel_case(name))
- [constraint]
+ constraint = @app.constraint_named(Util.camel_case(name))
+ [constraint.new]
end
end
View
@@ -61,6 +61,11 @@ module Fruit
end
end
+ module Constraints
+ class Never
+ end
+ end
+
App = Raptor::App.new(self) do
end
end
@@ -99,5 +104,19 @@ module EmptySite
app.injectables.should == []
end
end
+
+ describe "#constraint_named" do
+ it "has the named constraint" do
+ app = AwesomeSite::App
+ app.constraint_named(:Never).should == AwesomeSite::Constraints::Never
+ end
+
+ it "warns of missing constraints" do
+ app = EmptySite::App
+ expect do
+ app.constraint_named(:DoesNotExist)
+ end.to raise_error(Raptor::NoSuchConstraint)
+ end
+ end
end

0 comments on commit f40ba57

Please sign in to comment.