mirror of
				https://github.com/sinatra/sinatra
				synced 2023-03-27 23:18:01 -04:00 
			
		
		
		
	Hook mechanizm for route_added [help from rtomayko]
Example:
module Sinatra
  module RouteAddedExtSample
    def self.route_added(verb, path)
      p [verb, path]
    end
  end
  register RouteAddedExtSample
end
post '/' do
  do_something
  'ok'
end
Output:
["POST", "/"]
move superclass logic into extensions attr reader
			
			
This commit is contained in:
		
							parent
							
								
									0f02bafe86
								
							
						
					
					
						commit
						8b8ddfef56
					
				
					 2 changed files with 60 additions and 0 deletions
				
			
		
							
								
								
									
										47
									
								
								test/route_added_hook_test.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								test/route_added_hook_test.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,47 @@
 | 
			
		|||
require File.dirname(__FILE__) + '/helper'
 | 
			
		||||
 | 
			
		||||
module RouteAddedTest
 | 
			
		||||
  @routes = []
 | 
			
		||||
  def self.routes ; @routes ; end
 | 
			
		||||
  def self.route_added(verb, path)
 | 
			
		||||
    @routes << [verb, path]
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
describe "route_added Hook" do
 | 
			
		||||
 | 
			
		||||
  before { RouteAddedTest.routes.clear }
 | 
			
		||||
 | 
			
		||||
  it "should be notified of an added route" do
 | 
			
		||||
    mock_app(Class.new(Sinatra::Base)) {
 | 
			
		||||
      register RouteAddedTest
 | 
			
		||||
      get('/') {}
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    assert_equal [["GET", "/"], ["HEAD", "/"]],
 | 
			
		||||
      RouteAddedTest.routes
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  it "should include hooks from superclass" do
 | 
			
		||||
    a = Class.new(Class.new(Sinatra::Base))
 | 
			
		||||
    b = Class.new(a)
 | 
			
		||||
 | 
			
		||||
    a.register RouteAddedTest
 | 
			
		||||
    b.class_eval { post("/sub_app_route") {} }
 | 
			
		||||
 | 
			
		||||
    assert_equal [["POST", "/sub_app_route"]],
 | 
			
		||||
      RouteAddedTest.routes
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  it "should only run once per extension" do
 | 
			
		||||
    mock_app(Class.new(Sinatra::Base)) {
 | 
			
		||||
      register RouteAddedTest
 | 
			
		||||
      register RouteAddedTest
 | 
			
		||||
      get('/') {}
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    assert_equal [["GET", "/"], ["HEAD", "/"]],
 | 
			
		||||
      RouteAddedTest.routes
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue