たろマーク (はてなブックマーク)
-
[ django ] view を class based で書けるようになってた。
- Colorful Bokeh Effect in Pixelmator | Abduzeedo | Graphic Design Inspiration and Photoshop Tutorials
[ pixelmator ] -
[ pixelmator ]
■ Catalyst::Controller::Resouce を使って、Chained アクション書くのを楽する
追記(2009-05-20T16:46:42+09:00)
現在は Catalyst 5.8 系に対応した C:C::Resources がリリースされており、この方法は使用できません。
Catalyst 5.7 系 + C:C::Resources 0.4 系であれば使用可能です。
追記(2009-06-15T01:02:03+09:00)
perl-mongers.org に Cat 5.8 系の話を書きました。
Catalyst::Controller::Resources で Chaind を楽しよう
ここからもとの文章
本来は REST とかに使うらしい Catalyst::Controller::Resources ですが、このベースとなってる C:C:Resource を使って、自分用 base controller を作って Chained アクションを楽してみます。
(ちなみにヒントはこのあたり (sorry, vag*narepos commiter only.) から得ました。あとは、昔 YAPC か何かで Catalyst::Acrtion::REST を base にしてるのを見たんだけど見つからなかった。)
package Myapp::Web::Base::Controller;
use strict;
use warnings;
use base 'Catalyst::Controller::Resource';
use Catalyst::Utils;
sub setup_collection_actions {
my $self = shift;
my $maps = Catalyst::Utils::merge_hashes($self->{collection} || {}, {
list => { method => 'GET', path => '' },
do_create => { method => 'POST', path => '' },
create => { method => 'GET', path => 'new' },
});
$self->setup_actions(collection => $maps);
}
sub setup_member_actions {
my $self = shift;
my $maps = Catalyst::Utils::merge_hashes($self->{member} || {}, {
show => { method => 'GET', path => '' },
do_update => { method => 'POST', path => 'update' },
update => { method => 'GET', path => 'update' },
do_destroy => { method => 'POST', path => 'delete' },
destroy => { method => 'GET', path => 'delete' },
});
$self->setup_actions(member => $maps);
}
1;
これを controller で継承するとこんなかんじになります。
.-------------------------------------+--------------------------------------. | Path Spec | Private | +-------------------------------------+--------------------------------------+ | /hoges/new | /hoges/collection (0) | | | => /hoges/create | | /hoges/*/delete | /hoges/collection (0) | | | -> /hoges/member (1) | | | => /hoges/destroy | | /hoges | /hoges/collection (0) | | | => /hoges/do_create | | /hoges/*/delete | /hoges/collection (0) | | | -> /hoges/member (1) | | | => /hoges/do_destroy | | /hoges/*/update | /hoges/collection (0) | | | -> /hoges/member (1) | | | => /hoges/do_update | | /hoges | /hoges/collection (0) | | | => /hoges/list | | /hoges/* | /hoges/collection (0) | | | -> /hoges/member (1) | | | => /hoges/show | | /hoges/*/update | /hoges/collection (0) | | | -> /hoges/member (1) | | | => /hoges/update | '-------------------------------------+--------------------------------------'
controller の Chained アクションによるカオスからも解放されます。
試しに制作中の Catalyst アプリで使い始めてます。
ちなみに C:C:Resource は 0.04 で C:C:Resources::Base とかに変わるらしいです。
本来想定していない使い方なので、そのあたりは注意が必要ですね。
と言うか、なにか変わるときは教えてくださいw>ikasam_a さん






