FAQ
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:

package LogManager::Model::Tar;

use strict;
use warnings;
use base 'Catalyst::Model::Factory';

__PACKAGE__->config(
class => 'Archive::Tar::File',
constructor => 'new',
);

1;

The Archive::Tar::File constructor (new) requires a filename be passed e.g.,
new( file => $path ). In a controller, I have tried:

sub view : Local {
my ( $self, $c, $file ) = @_;

LogManager->config->{'Model::Tar'} = {
class => 'Archive::Tar::File',
args => {
file => "c:\\temp\\logs\\$file",
},
};
my $tarball = $c->model('LogManager::Tar');
if( $tarball ) {
my @items = $tarball->get_files;
$c->stash->{items} = @items;
}
else {
$c->stash->{error_msg} = "Couldn't instantiate LogManager::Tar
model";
}
$c->stash->{template} = 'log/view.tt2';
}

The server started without error, and I called it with *"*
http://localhost:3000/log/view/WEBCGI.usgap002.20071020093500.tar"

I have, unsuccessfully, tried several other scenarios, but all I get is an
undefined object, resulting in $c->stash->{error_msg} being set to "Couldn't
instantiate LogManager::Tar model". Any pointers in the right direction are
greatly appreciated.

BTW, Catalyst is and awesome framework!

Thanks,
Mike Peck
mpeck@pobox.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20071023/31170a87/attachment.htm

Search Discussions

  • Jonathan Rockway at Oct 23, 2007 at 9:23 pm

    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) = @_;
    return Archive::Tar::File->new({ file => $file });
    }
    1;

    and then in an action:

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

    Of course, you could just do:

    use Archive::Tar::File;
    sub action :Whatever {
    ...
    my $archive = Archive::Tar::File->new({ file => '/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 => ( isa => 'Str', is => 'ro', required => 1 );
    has archive => (isa => 'Archive::Tar::File', default => 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 = $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=testmore.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/c6276d4b/signature.pgp

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedOct 23, '07 at 5:52p
activeOct 23, '07 at 9:23p
posts2
users2
websitecatalystframework.org
irc#catalyst

2 users in discussion

Jonathan Rockway: 1 post Michael Peck: 1 post

People

Translate

site design / logo © 2022 Grokbase