[Catalyst] Re: ActionClass vs. Moose Role?

Aristotle Pagaltzis pagaltzis at gmx.de
Sat Sep 5 16:27:07 GMT 2009


* Tomas Doran <bobtfish at bobtfish.net> [2009-09-02 16:30]:
>* Bill Moseley <moseley at hank.org> [2009-08-28 20:10]:
>> I guess the difference is if you want to apply code to any
>> action vs. a specific action. RenderView is typically applied
>> just to the end() method (or a method forwarded to from end()
>> ), so maybe it's really better as a role since it would always
>> alter end().
>>
>> But if I want to apply code to different (and specific)
>> actions then ActionClass is probably better since it can be
>> set per action.
>>
>> Anyway, using "before 'end'" is probably the way to go in the
>> role instead of "sub end".
>
> Yes, that's significantly better, due to the fact that methods
> from roles will be silently ignored if the local class has
> a method of that name.
>
> What I'd be doing is something like this:
>
> package MyApp::Role::Foo;
> use Moose::Role -traits => 'MethodAttributes';
>
> sub end : Action {}
>
> before 'end' => sub { # Your code here };
>
> package MyApp::Controller::Foo;
> use Moose;
> BEGIN { extends 'Catalyst::Controller' }
> with 'MyApp::Role::Foo';
>
> # Works like this, OR you can say:
> # sub end : Action {
> #     # Your code here, will get wrapped with your modifier.
> # }

I seriously disagree. This essentially repurposes `sub end` to
perform the job of `after 'end'` and makes it non-obvious how to
run code before the mixed-in `before 'end'`. In contrast, if the
role simply does its work in `sub end`, then it is completely
clear what `before`/`after`/`around` in user will do.

> Yes, that's significantly better, due to the fact that methods
> from roles will be silently ignored if the local class has
> a method of that name.

No. Rather, it indicates to me that Ovid is not the only one who
should be banging his drum: role composition should just not
succeed silently in such cases. It should require an explicit
request of some form. To me, the semantic distortion you
suggested is an unconscious workaround that indicates that the
problem actually is one in practice and not only in theory.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>



More information about the Catalyst mailing list