1. do find => rescue
2. model operation more than 3 lines => define class method in model
3. def current_user
4. render_as_form
5. more than one relation in one table
6. composite primary key => hook
Tuesday, April 28, 2009
Tuesday, April 7, 2009
Class::Implant - No &import() !
Class::implant is a experimental little helper implemented when I developed Railsish with gugod. Manipulating mixin and inheritance outside package is its primary function, also with abilities to select methods which would import.
Original idea is inspire by the purpose: we want to import whole Helper methods into Controller which the Helper is named after. And it is should not be left to framework users to do it by themself. In Ruby, we just need to write
Even use Exporter and edit
Everything done! All methods in XxxHelper is imported into XxxController!
If call
In the release 0.02_01, two other options work.
For example,
means import methods whose name start with h in Foo and Bar into Cat.
I also write UNIVERSAL::Implant. As its name, require once, and write as follow everywhere.
Do the same thing as previous example. That means, caller package Cat assign
Chinese Version is @ Chupei.pm.org
Original idea is inspire by the purpose: we want to import whole Helper methods into Controller which the Helper is named after. And it is should not be left to framework users to do it by themself. In Ruby, we just need to write
XxxController.send(:include, XxxHelper)
In Perl, it can also be written aseval qq{ package XxxController; use XxxHelper; }
But a tedious &import() should be write in XxxHelper as follow.and edit the export methods list by hand.sub import {
for (qw(hello world foo bar method...)) {
*{blah::$_} = \&$_
}
}
Even use Exporter and edit
@EXPORT
and export by symbol :all
, users still need to do follow by handClass::Implant provide simplified equivalent.use base Exporter;
our @EXPORT = qw(..);
package main;
use Class::Implant;
implant "XxxHelper", { into => "XxxController" };
Everything done! All methods in XxxHelper is imported into XxxController!
If call
use Class::Implant
in other package, default import target would become it. That means, above code is the same as follow:package XxxController;
use Class::Implant;
implant "XxxHelper";
In the release 0.02_01, two other options work.
{ inherit => 1 }
make imported packages appear in @ISA
of import target. { match => pattern }
filter, and import methods conform to pattern.For example,
package main;
use Class::Implant;
implant qw(Foo Bar), { into => "Cat", match => qr{h\w+} };
means import methods whose name start with h in Foo and Bar into Cat.
I also write UNIVERSAL::Implant. As its name, require once, and write as follow everywhere.
Cat->implant qw(Foo Bar), { match => qr{h\w+} }
Do the same thing as previous example. That means, caller package Cat assign
{ into => "Cat" }
.Chinese Version is @ Chupei.pm.org
Labels:
perl,
perl module
Subscribe to:
Posts (Atom)