[Catalyst] Newbie Catalyst::Model::Factory

Jonathan Rockway jon at jrock.us
Tue Oct 23 21:23:41 GMT 2007


Michael Peck wrote:
> I am new to Catalyst, but have been successful at creating simple DBIC
> CRUD apps.  Now, I am trying something different...  I want to use an
> Archive::Tar::File object as a model.  I have created my model as:

This is probably a bug in Catalyst::Model::Factory, but it doesn't
actually let you pass arguments to the adapted class.  You would want to
write a module that looks like this:

   package MyApp::Model::Tar;
   use Archive::Tar::File;
   use base 'Catalyst::Model';

   sub ACCEPT_CONTEXT {
       my ($self, $c, $file) =3D @_;
       return Archive::Tar::File->new({ file =3D> $file });
   }
   1;

and then in an action:

   sub action :Whatever {
      my ($self, $c, @args) =3D @_;
      my $archive =3D $c->model('Tar', '/path/to/file');
      ...
   }

Of course, you could just do:

   use Archive::Tar::File;
   sub action :Whatever {
      ...
      my $archive =3D Archive::Tar::File->new({ file =3D> '/path/to/file' }=
);
   }

However, (and this is a big however) I think you really want another
level of abstraction.  If you're doing much manipulation of that Tar
object in your Controller, your app is designed wrong.  You really want
something like this:

   package MyApp::Backend::SomeThingWithTapeArchives;
   use Moose; # or whatever
  =

   has file =3D> ( isa =3D> 'Str', is =3D> 'ro', required =3D> 1 );
   has archive =3D> (isa =3D> 'Archive::Tar::File', default =3D> sub { make
archive });

   sub do_something_to_the_archive { $self->archive->delete_stuff, .... }
   sub add_uploaded_file { ... }
   sub get_file_list { ... }

Then in your controller you call actions like:

   my $tb =3D $c->model('SomeThingWithTapeArchives');
   $tb->add_uploaded_files($c->req->uploads);
  =

The basic idea is to have as little code as possible in your
controller.  The only code you want in there is code that glues your
backend model to the Catalyst request and response.  Any other code
should be in your backend business logic class.  This ensures that your
business logic is testable and reusable; very important things to keep
in mind when writing an app.

This is kind of hard to explain because you're not saying what you're
doing... so please reply with some details and I can explain further.  I
have a talk that explains the details of moving stuff to a backend class
a bit better:

   =

http://www.jrock.us/ppw2007/testmore/takahashi.xul?data=3Dtestmore.txt#page1

(requires mozilla + javascript)

Hope this helps.

Regards,
Jonathan Rockway

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 370 bytes
Desc: OpenPGP digital signature
Url : http://lists.scsys.co.uk/pipermail/catalyst/attachments/20071023/c627=
6d4b/signature.pgp


More information about the Catalyst mailing list