1 
2     ////////////////////////////////////////////////////////////////////////////////////////////
3     //  Copyright (c) 2012 Christopher Nicholson-Sauls                                        //
4     //                                                                                        //
5     //  Permission is hereby granted, free of charge, to any person obtaining a copy of this  //
6     //  software and associated documentation files (the "Software"), to deal in the          //
7     //  Software without restriction, including without limitation the rights to use, copy,   //
8     //  modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,   //
9     //  and to permit persons to whom the Software is furnished to do so, subject to the      //
10     //  following conditions:                                                                 //
11     //                                                                                        //
12     //  The above copyright notice and this permission notice shall be included in all        //
13     //  copies or substantial portions of the Software.                                       //
14     //                                                                                        //
15     //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,   //
16     //  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A         //
17     //  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT    //
18     //  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  //
19     //  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE  //
20     //  OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                                         //
21     ////////////////////////////////////////////////////////////////////////////////////////////
22 
23 module zeal.base.controller;
24 
25 import vibe.http.server;
26 
27 import zeal.inflector;
28 
29 import zeal.utils.keyword;
30 import zeal.utils.singleton;
31 
32 abstract class Controller {
33 	mixin Singleton;
34 	
35 	@property Action action ( string _Name, this RealClass ) () {
36 		auto self = cast( RealClass ) this;
37 		assert( self ); // if this ever trips, I shall be very confused.
38 		
39 		enum _Suffix = zeal.utils.keyword.isKeyword( _Name ) ? "_" : "";
40 		enum _Method = _Name.camelize( false ) ~ _Suffix;
41 		enum _Dlg    = `&self.` ~ _Method;
42 		
43 		static if ( is( typeof( mixin( _Dlg ) ) == Action ) ) {
44 			mixin( `return ` ~ _Dlg ~ `;` );
45 		}
46 		else {
47 			return null;
48 		}
49 	}
50 
51 	protected alias HttpServerRequest  Request;
52 	protected alias HttpServerResponse Response;
53 	
54 	alias void delegate( Request, Response ) Action;
55 	
56 }