Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Catalyst Parameterized Role with Attributes?

by duelafn (Priest)
on Sep 27, 2011 at 02:21 UTC ( #927999=perlquestion: print w/ replies, xml ) Need Help??
duelafn has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to create parameterized Catalyst roles with method attributes? The closest thing a web search finds is this post whose followup message points to a moosex-methodattributes test file which does create a parameterized role, but as far as I can tell doesn't test any attributes. My attempt (below) compiles but does not produce any (chained) actions.

package Serenevy::CatalystX::Role::CRUDForms; use MooseX::Role::Parameterized -traits => 'MooseX::MethodAttributes:: +Role::Meta::Role'; use namespace::autoclean; use re 'taint'; use 5.010; parameter [qw/ name root path_part model_name /] => ( is => "rw", isa => "Str", required => 1, ); role { my $param = shift; my $NAME = $param->name; my $ROOT = $param->root; my $MODEL = $param->model_name; my $PATH_PART = $param->path_part; method model => sub { $_[1]->model( $MODEL ) }; method root => sub :Chained($ROOT) :PathPart($PATH_PART) :CaptureA +rgs(0) { 1 }; method list => sub :Chained(root) :PathPart("") :Args(0) { my ( $self, $c ) = @_; # ... }; # ... };

This role is consumed in my controiller via

package RiverbendMath::Controller::Int::FieldClass; use Moose; use namespace::autoclean; use re 'taint'; use 5.010; BEGIN { extends 'Catalyst::Controller'; } with 'Serenevy::CatalystX::Role::CRUDForms' => { name => "Field Class", root => "../int", path_part => "field-class", model_name => "DB::EntClass", };

Catalyst 5.80025; Moose 1.09; MooseX::Role::Parameterized 0.19; MooseX::MethodAttributes 0.24

Good Day,
    Dean

Comment on Catalyst Parameterized Role with Attributes?
Select or Download Code
Replies are listed 'Best First'.
Re: Catalyst Parameterized Role with Attributes?
by roman (Monk) on Jan 10, 2012 at 15:03 UTC

    I'm puzzled that nobody commented this topic. Parameterized controller roles seem to me as a handy tool for detailed modeling of parts of action chains.

    I was also confused by the test in MooseX::MethodAttributes distribution because it doesnot work with attributes.

    Unsuccessfull with mixing MooseX::MethodAttributes and MooseX::Role::Parameterized I tried to modify 'action' hash in controller's config (described in Catalyst::Controller) instead. It seems to work.

    role { my $param = shift; my $NAME = $param->name; my $ROOT = $param->root; my $MODEL = $param->model_name; my $PATH_PART = $param->path_part; method model => sub { $_[1]->model( $MODEL ) }; method root => sub { ... }; method list => sub {...}; # The purpose of this 'before' is to add keys # into config hash first (only first) time the config is touched. my $config_called; before config => sub { return if $config_called++; my $action_config = shift()->config->{action} ||= {}; %$action_config = ( %$action_config, root => { Chained => [$ROOT], PathPart => [$PATH_PART], CaptureArgs => [0], }, list => { Chained => ['root'], PathPart => [''], Args => [0], } ); }; };

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://927999]
Approved by matze77
Front-paged by matze77
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (17)
As of 2015-07-29 20:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (268 votes), past polls