Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

Catalyst action attributes

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


toby.corkindale at strategicdata

Jul 12, 2009, 10:35 PM

Post #1 of 7 (1247 views)
Permalink
Catalyst action attributes

Hi,
I wondered if anyone could point me towards a working example of
creating custom action attribute handlers for Catalyst?

eg. The contents of Catalyst::Controller::SecretAgent in the example below:


package MyApp::Controller::M;
use parent 'Catalyst::Controller::SecretAgent';
# Provides Vehicle() and Gimmick() actions.

sub bond : Vehicle('Aston Martin') {
my ($self, $c) = @_;
# ...
}

sub maxwell_smart : Gimmick('Shoe-phone') {
My ($self, $c) = @_;
#...
}

1;

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


toby.corkindale at strategicdata

Jul 12, 2009, 11:27 PM

Post #2 of 7 (1182 views)
Permalink
Re: Catalyst action attributes [In reply to]

Toby Corkindale wrote:
> Hi,
> I wondered if anyone could point me towards a working example of
> creating custom action attribute handlers for Catalyst?
>
> eg. The contents of Catalyst::Controller::SecretAgent in the example below:
>
>
> package MyApp::Controller::M;
> use parent 'Catalyst::Controller::SecretAgent';
> # Provides Vehicle() and Gimmick() actions.
>
> sub bond : Vehicle('Aston Martin') {
> my ($self, $c) = @_;
> # ...
> }
>
> sub maxwell_smart : Gimmick('Shoe-phone') {
> My ($self, $c) = @_;
> #...
> }
>
> 1;


I should clarify what I'm after..
I'd still like to be able to specify regular DispatchType attributes
against the routines (eg. Local, Chained, etc), but I'd also like to
have my own methods called against the other attributes I've specified.

The first method I tried was:
package Catalyst::Controller::SecretAgent;
use strict / use warnings
use parent 'Catalyst::Controller';
use attributes __PACKAGE__ => \&set_vehicle, "Vehicle";
sub set_vehicle { # do something }
...

but I think that messes up catalyst's own handling of attributes.

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


devin.austin at gmail

Jul 12, 2009, 11:36 PM

Post #3 of 7 (1190 views)
Permalink
Re: Catalyst action attributes [In reply to]

Check this out:
http://search.cpan.org/~hkclark/Catalyst-Manual-5.8000/lib/Catalyst/Manual/ExtendingCatalyst.pod#Attributesand
http://search.cpan.org/~hkclark/Catalyst-Manual-5.8000/lib/Catalyst/Manual/ExtendingCatalyst.pod#Action_classes


On Mon, Jul 13, 2009 at 12:27 AM, Toby Corkindale <
toby.corkindale [at] strategicdata> wrote:

> Toby Corkindale wrote:
>
>> Hi,
>> I wondered if anyone could point me towards a working example of creating
>> custom action attribute handlers for Catalyst?
>>
>> eg. The contents of Catalyst::Controller::SecretAgent in the example
>> below:
>>
>>
>> package MyApp::Controller::M;
>> use parent 'Catalyst::Controller::SecretAgent';
>> # Provides Vehicle() and Gimmick() actions.
>>
>> sub bond : Vehicle('Aston Martin') {
>> my ($self, $c) = @_;
>> # ...
>> }
>>
>> sub maxwell_smart : Gimmick('Shoe-phone') {
>> My ($self, $c) = @_;
>> #...
>> }
>>
>> 1;
>>
>
>
> I should clarify what I'm after..
> I'd still like to be able to specify regular DispatchType attributes
> against the routines (eg. Local, Chained, etc), but I'd also like to have my
> own methods called against the other attributes I've specified.
>
> The first method I tried was:
> package Catalyst::Controller::SecretAgent;
> use strict / use warnings
> use parent 'Catalyst::Controller';
> use attributes __PACKAGE__ => \&set_vehicle, "Vehicle";
> sub set_vehicle { # do something }
> ...
>
> but I think that messes up catalyst's own handling of attributes.
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/
>



--
Devin Austin
http://www.codedright.net
http://www.dreamhost.com/r.cgi?326568/hosting.html - Host with DreamHost!


toby.corkindale at strategicdata

Jul 12, 2009, 11:50 PM

Post #4 of 7 (1187 views)
Permalink
Re: Catalyst action attributes [In reply to]

Devin Austin wrote:
> Check this out:
> http://search.cpan.org/~hkclark/Catalyst-Manual-5.8000/lib/Catalyst/Manual/ExtendingCatalyst.pod#Attributes
> and
> http://search.cpan.org/~hkclark/Catalyst-Manual-5.8000/lib/Catalyst/Manual/ExtendingCatalyst.pod#Action_classes

Thanks, although I had already read that before posting.

As far as I can see, there is no way to pass parameters to the
ActionClass. Have I missed something?

> On Mon, Jul 13, 2009 at 12:27 AM, Toby Corkindale
> <toby.corkindale [at] strategicdata
> <mailto:toby.corkindale [at] strategicdata>> wrote:
>
> Toby Corkindale wrote:
>
> Hi,
> I wondered if anyone could point me towards a working example of
> creating custom action attribute handlers for Catalyst?
>
> eg. The contents of Catalyst::Controller::SecretAgent in the
> example below:
>
>
> package MyApp::Controller::M;
> use parent 'Catalyst::Controller::SecretAgent';
> # Provides Vehicle() and Gimmick() actions.
>
> sub bond : Vehicle('Aston Martin') {
> my ($self, $c) = @_;
> # ...
> }
>
> sub maxwell_smart : Gimmick('Shoe-phone') {
> My ($self, $c) = @_;
> #...
> }
>
> 1;
>
>
>
> I should clarify what I'm after..
> I'd still like to be able to specify regular DispatchType attributes
> against the routines (eg. Local, Chained, etc), but I'd also like to
> have my own methods called against the other attributes I've specified.
>
> The first method I tried was:
> package Catalyst::Controller::SecretAgent;
> use strict / use warnings
> use parent 'Catalyst::Controller';
> use attributes __PACKAGE__ => \&set_vehicle, "Vehicle";
> sub set_vehicle { # do something }
> ...
>
> but I think that messes up catalyst's own handling of attributes.
>
>
> _______________________________________________
> List: Catalyst [at] lists <mailto:Catalyst [at] lists>
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/
>
>
>
>
> --
> Devin Austin
> http://www.codedright.net
> http://www.dreamhost.com/r.cgi?326568/hosting.html - Host with DreamHost!
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/


--
Strategic Data Pty Ltd
Ph: 03 9340 9000

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


devin.austin at gmail

Jul 13, 2009, 12:44 AM

Post #5 of 7 (1200 views)
Permalink
Re: Catalyst action attributes [In reply to]

Try this:

sub post_chain_root : Chained('/') CaptureArgs(0) {

my ($self, $c) = @_;

foreach my $action (@{$c->action->chain}) {
my $role = $action->attributes->{RequiresRole}[0];
next unless $role;
## test role here...
}

}


That's a bit of code that you can put in the root of a Chained chain and do
something like

sub create_new_post : RequiresRole('can_post') {

# ...

}
HTH.


On Mon, Jul 13, 2009 at 12:50 AM, Toby Corkindale <
toby.corkindale [at] strategicdata> wrote:

> Devin Austin wrote:
>
>> Check this out:
>> http://search.cpan.org/~hkclark/Catalyst-Manual-5.8000/lib/Catalyst/Manual/ExtendingCatalyst.pod#Attributes<http://search.cpan.org/%7Ehkclark/Catalyst-Manual-5.8000/lib/Catalyst/Manual/ExtendingCatalyst.pod#Attributes>and
>> http://search.cpan.org/~hkclark/Catalyst-Manual-5.8000/lib/Catalyst/Manual/ExtendingCatalyst.pod#Action_classes<http://search.cpan.org/%7Ehkclark/Catalyst-Manual-5.8000/lib/Catalyst/Manual/ExtendingCatalyst.pod#Action_classes>
>>
>
> Thanks, although I had already read that before posting.
>
> As far as I can see, there is no way to pass parameters to the ActionClass.
> Have I missed something?
>
> On Mon, Jul 13, 2009 at 12:27 AM, Toby Corkindale <
>> toby.corkindale [at] strategicdata <mailto:
>> toby.corkindale [at] strategicdata>> wrote:
>>
>> Toby Corkindale wrote:
>>
>> Hi,
>> I wondered if anyone could point me towards a working example of
>> creating custom action attribute handlers for Catalyst?
>>
>> eg. The contents of Catalyst::Controller::SecretAgent in the
>> example below:
>>
>>
>> package MyApp::Controller::M;
>> use parent 'Catalyst::Controller::SecretAgent';
>> # Provides Vehicle() and Gimmick() actions.
>>
>> sub bond : Vehicle('Aston Martin') {
>> my ($self, $c) = @_;
>> # ...
>> }
>>
>> sub maxwell_smart : Gimmick('Shoe-phone') {
>> My ($self, $c) = @_;
>> #...
>> }
>>
>> 1;
>>
>>
>>
>> I should clarify what I'm after..
>> I'd still like to be able to specify regular DispatchType attributes
>> against the routines (eg. Local, Chained, etc), but I'd also like to
>> have my own methods called against the other attributes I've specified.
>>
>> The first method I tried was:
>> package Catalyst::Controller::SecretAgent;
>> use strict / use warnings
>> use parent 'Catalyst::Controller';
>> use attributes __PACKAGE__ => \&set_vehicle, "Vehicle";
>> sub set_vehicle { # do something }
>> ...
>>
>> but I think that messes up catalyst's own handling of attributes.
>>
>>
>> _______________________________________________
>> List: Catalyst [at] lists <mailto:Catalyst [at] lists>
>> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> Searchable archive:
>> http://www.mail-archive.com/catalyst [at] lists/
>> Dev site: http://dev.catalyst.perl.org/
>>
>>
>>
>>
>> --
>> Devin Austin
>> http://www.codedright.net
>> http://www.dreamhost.com/r.cgi?326568/hosting.html - Host with DreamHost!
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> List: Catalyst [at] lists
>> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> Searchable archive:
>> http://www.mail-archive.com/catalyst [at] lists/
>> Dev site: http://dev.catalyst.perl.org/
>>
>
>
> --
> Strategic Data Pty Ltd
> Ph: 03 9340 9000
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/
>



--
Devin Austin
http://www.codedright.net
http://www.dreamhost.com/r.cgi?326568/hosting.html - Host with DreamHost!


bobtfish at bobtfish

Jul 13, 2009, 1:32 AM

Post #6 of 7 (1181 views)
Permalink
Re: Catalyst action attributes [In reply to]

On 13 Jul 2009, at 07:50, Toby Corkindale wrote:

> Devin Austin wrote:
>> Check this out: http://search.cpan.org/~hkclark/Catalyst-
>> Manual-5.8000/lib/Catalyst/Manual/ExtendingCatalyst.pod#Attributes
>> and http://search.cpan.org/~hkclark/Catalyst-Manual-5.8000/lib/
>> Catalyst/Manual/ExtendingCatalyst.pod#Action_classes
>
> Thanks, although I had already read that before posting.
>
> As far as I can see, there is no way to pass parameters to the
> ActionClass. Have I missed something?

The action class is passed the attributes when it is constructed in
its arguments.

However - before we get into this, I recommend you write an
ActionRole (ala Catalyst::Controller::ActionRole) instead of an
action class, as you can use multiple actionroles together (which is
not the case with action classes).

Simple example of some prior art you can steal to get you going:

http://search.cpan.org/~bobtfish/Catalyst-ActionRole-
FindViewByIsa-0.000002/lib/Catalyst/ActionRole/FindViewByIsa.pm

Cheers
t0m


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


toby.corkindale at strategicdata

Jul 13, 2009, 11:46 PM

Post #7 of 7 (1156 views)
Permalink
Re: Catalyst action attributes [In reply to]

Tomas Doran wrote:
>
> On 13 Jul 2009, at 07:50, Toby Corkindale wrote:
>
>> Devin Austin wrote:
>>> Check this out:
>>> http://search.cpan.org/~hkclark/Catalyst-Manual-5.8000/lib/Catalyst/Manual/ExtendingCatalyst.pod#Attributes
>>> and
>>> http://search.cpan.org/~hkclark/Catalyst-Manual-5.8000/lib/Catalyst/Manual/ExtendingCatalyst.pod#Action_classes
>>>
>>
>> Thanks, although I had already read that before posting.
>>
>> As far as I can see, there is no way to pass parameters to the
>> ActionClass. Have I missed something?
>
> The action class is passed the attributes when it is constructed in its
> arguments.
>
> However - before we get into this, I recommend you write an ActionRole
> (ala Catalyst::Controller::ActionRole) instead of an action class, as
> you can use multiple actionroles together (which is not the case with
> action classes).
>
> Simple example of some prior art you can steal to get you going:
>
> http://search.cpan.org/~bobtfish/Catalyst-ActionRole-FindViewByIsa-0.000002/lib/Catalyst/ActionRole/FindViewByIsa.pm

Thanks, that's great.

So to summarise, the "best practice" way of passing parameters to these
attribute-handlers is by using more attributes, that you *don't*
specifically handle, but access via $c->action->attributes->{..}

I think that's right?

Cheers,
Toby

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.