Skip to content

Commit

Permalink
ask for custom constraints by name
Browse files Browse the repository at this point in the history
  • Loading branch information
tcrayford committed May 1, 2012
1 parent 33ffbca commit f40ba57
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
11 changes: 11 additions & 0 deletions lib/raptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions lib/raptor/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions spec/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ module Fruit
end
end

module Constraints
class Never
end
end

App = Raptor::App.new(self) do
end
end
Expand Down Expand Up @@ -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.