[Catalyst] uri_with and ACCEPT_CONTEXT

Tomas Doran bobtfish at bobtfish.net
Fri Jan 8 14:50:18 GMT 2010


Dermot wrote:
> use parent 'Catalyst::Model::Factory';
> use Moose;
> use namespace::autoclean;

Ewww. Use Moose, or don't - don't use both parent.pm and Moose..

> 
> __PACKAGE__->config(
>         class       => 'MyApp::Local::Class',
>         args        => { arg    =>  $self->arg};     # Global symbol
> "$self" requires...
>         );
> 
> 
> sub ACCEPT_CONTEXT {
>     my ($self, $c) = @_;
>     my $new = $self->meta->clone_object($self, arg => $c->uri_with('') );
>     $new = $self->meta->clone_object($self, arg => $c->req->path  );
>     return $new;
> }

If you're implementing your own ACCEPT_CONTEXT method, you're overriding 
the one provided by Catalyst::Model::Factory - so there is no point in 
it being there at all..

Also, why are you cloning the model object you already have? Shouldn't 
you be acting as a factory for an external class?

What I _think_ you want is something more like this:

package MyApp::Model::Foo;
use Moose;
use Foo; # Your 'real' model class..
use namespace::clean -except => 'meta';

extends 'Catalyst::Model';
with qw
     Catalyst::Component::InstancePerContext
     Catalyst::Component::ContextClosure
/;

has arg1 => ( is => 'ro', required => 1 );
has arg2 => ( is => 'ro', required => 1 );

sub build_per_context_instance {
     my ($self, $ctx) = @_;
     Foo->new(
         arg1 => $self->arg1,
         arg2 => $self->arg2,
         uri_gen => $self->make_context_closure(sub {
             my ($ctx, @args) = @_;
             $ctx->uri_with(@args);
         }, $ctx),
     );
}

__PACKAGE__->meta->make_immutable;



More information about the Catalyst mailing list