Skip to content

Instantly share code, notes, and snippets.

@k2052
Created June 2, 2011 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k2052/1003798 to your computer and use it in GitHub Desktop.
Save k2052/1003798 to your computer and use it in GitHub Desktop.
class AllButPattern
@@match = Struct.new(:captures)
def initialize(app, *exceptions)
@controller = app.current_controller
@app = app
@exceptions = exceptions
@captures = @@match.new([])
end
def match(str)
if @exceptions.first.is_a?(Symbol)
@exceptions.each do |except|
if except.to_s.index('_')
if @app.router.runner.params.empty?
return @captures if @app.router.named_routes[except].path != str
else
return @captures if @app.router.named_routes[except].url(@app.router.runner.params)[0] != str
end
else
route_name = @controller.to_s << '_' << except.to_s
if @app.router.runner.params.empty?
return @captures if @app.router.named_routes[route_name.to_sym].path != str
else
return @captures if @app.router.named_routes[route_name.to_sym].url(@app.router.runner.params)[0] != str
end
end
end
elsif @exceptions.is_a?(Array)
@captures unless @exceptions.include?(str)
end
false
end
end
def all_but(*args)
AllButPattern.new(self, *args)
end
Brkit.controllers :user do
before all_but(:bob) do
puts "you've been hit before?"
end
get :bob, :map => '/bob' do
return 'bobs your uncle'
end
get :testget, :map => '/testget' do
return 'test GET'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment