Thread: [Webtek-devel] Session methods
Status: Alpha
Brought to you by:
prozessor13
From: Adrian S. <adr...@gm...> - 2008-09-04 15:48:30
|
I thought it would be nice to be able to define my own methods on the Session class. In other frameworks, it's possible to subclass the Session class, add extra attributes (not necessary in the case of WebTek) and add extra methods. I thought it might be nice if this happened automatically. So I added code in the Session class to check for the existance of an AppName::Session. If it's there, it's used, otherwise the standard WebTek::Session is used. However, i didn't get on very well. Firstly the AppName::Session is not automatically loaded (AppName::Classname is not loaded; AppName::Package::Classname is, which I didn't expect), and then I had a problem where the compiler went into an infinite loop when I tried to automatically load the class from somewhere else (I've no idea why) and so I gave up on automatic loading and put "use AppName::Session" in the Handler.pm. The module is loaded, but now I get the error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1' in delete from session where I.e. it tries to execute the statement "delete from session where" with no where condition, as far as I can see. Well, I shall continue trying tomorrow. Currently I have spent about 2 hours on this feature. It is just to display a different text if a certain condition is true, if certain attributes are set on the session at the same time, i.e. all I need is one utility method actually! Maybe I can give up on this approach if it is not possible, and e.g. put the utility method somewhere else e.g. global function somewhere. Do you have any advice on this matter? Thanks, Adrian |
From: max d. <pro...@gm...> - 2008-09-04 20:05:23
|
hmm.... yes, you are right, this is really tricky. i also spent one hour on this problem, and now i understand, there is no clean solution, because: * the session is loaded explizit via the loader, and all Modules in the app-dir via WebTek::Module->load * the session is a model - a model use the init mehtod to analyze the table and set its properties * the session has the init method which inits the session (read request->cookie, and so on...) - there is a clash with the model->init method - so there is a hack, and this is to rename the model->init mehod to init_model ( i have fixed this now, but its not already checked in ) * if you want to subclass a model, the subclass is automatically some thing like a 'single-table-inheritance', which additionally has some other magic to work -> so you can not simply subclass a model (as in traditionally OO programming) => but there is a solution (because we live in perl ;) ... dirty, but a common perl way ;) so in the file Model/Session.pm say: my $session_init = *WebTek::Session::init{'CODE'}; *WebTek::Session::init = sub { my $class = shift; #... do something here log_info('foofoo'); #... call the super mehtod $class->$session_init(); } so what is this: * first we copy the coderef into a variable * then we create a anonymous subroutine with the new session-init code -> it has to be a anonymous subroutine to dont overwrite the original init method of WebTek::Session, something like: package WebTek::Session; sub init { ... } will overwrite the original init mehtod * this new coderef will now be called via WebTek::Session->init -> and within this we can call the SUPER mehtod * tricky isnt it ;) -> and this is the common way in javascript to subclass classes (ups... sorry instances, or still classes, who will know this in javascript, mr. crockford maybe?? ;) * simply you can use Hook::LexWrap to simplify things hmm... i see there is a problem with webtek and OO thinking. i never run into such pitfalls, because i more think the webtek way (in rails they say, you have to think the rails way, otherwhise you get a lot of troubles) the early releases was really stricktly OO, and more than that, you could use every webtek module in your app dir as a stand alone module, but this is no longer possible, because i added so much magic, which will simplyfy things if you know the magic, but webtek gets much more complex with this.. hmmm... i think it is the time to meet you and make a refactoring of some parts, to dont run into such pitfalls (i remember this is not your first one, sorry for that...). max. On Sep 4, 2008, at 5:48 PM, Adrian Smith wrote: > I thought it would be nice to be able to define my own methods on > the Session class. In other frameworks, it's possible to subclass > the Session class, add extra attributes (not necessary in the case > of WebTek) and add extra methods. > > I thought it might be nice if this happened automatically. So I > added code in the Session class to check for the existance of an > AppName::Session. If it's there, it's used, otherwise the standard > WebTek::Session is used. > > However, i didn't get on very well. Firstly the AppName::Session is > not automatically loaded (AppName::Classname is not loaded; > AppName::Package::Classname is, which I didn't expect), and then I > had a problem where the compiler went into an infinite loop when I > tried to automatically load the class from somewhere else (I've no > idea why) and so I gave up on automatic loading and put "use > AppName::Session" in the Handler.pm. The module is loaded, but now I > get the error: > > You have an error in your SQL syntax; check the manual that > corresponds to your MySQL server version for the right syntax to use > near '' at line 1' in delete from session where > > I.e. it tries to execute the statement "delete from session where" > with no where condition, as far as I can see. > > Well, I shall continue trying tomorrow. Currently I have spent about > 2 hours on this feature. It is just to display a different text if a > certain condition is true, if certain attributes are set on the > session at the same time, i.e. all I need is one utility method > actually! Maybe I can give up on this approach if it is not > possible, and e.g. put the utility method somewhere else e.g. global > function somewhere. > > Do you have any advice on this matter? > > Thanks, Adrian > ------------------------------------------------------------------------- > This SF.Net email is sponsored by the Moblin Your Move Developer's > challenge > Build the coolest Linux based applications with Moblin SDK & win > great prizes > Grand prize is a trip for two to an Open Source event anywhere in > the world > http://moblin-contest.org/redirect.php?banner_id=100&url=/_______________________________________________ > Webtek-devel mailing list > Web...@li... > https://lists.sourceforge.net/lists/listinfo/webtek-devel |