[Catalyst] Simple literal Model

Tomas Doran bobtfish at bobtfish.net
Fri Feb 25 10:06:33 GMT 2011


On 25 Feb 2011, at 01:56, John M. Dlugosz wrote:

> I'm planning several web pages that will be the same except for the  
> actual content.
> Rather than code the details in the TT template or prepare a list  
> literally in the Controller, I'm thinking that this ought to go into  
> a Model.
>
> But, it can just be a Perl declaration.  Something like:
>  my @data= (
>     { foo=>'bar', items=>[qw/first second third/], bar=>'baz },
>     # 7 more rows
>     );
>
> and add to that some subs to slice and dice the information so the  
> TT doesn't have to; e.g. give a list of all the foo's.
>
> How do I create a "static, literal" Model?
> And, what would its form be in order to make it properly abstract,  
> so it =could= be replaced with a standard DBIC or other model back- 
> end without affecting the code?

package MyApp::Model::Foo;
use Moose;
use Method::Signatures::Simple;
use namespace::autoclean;

extends 'Catalyst::Model';

method data {
	return {
		    { foo=>'bar', items=>[qw/first second third/], bar=>'baz },
			.......
	};
};

__PACKAGE__->meta->make_immutable;
1;

And you then call $c->model('Foo')->data;

The implementation of the 'data' method could then later be replaced  
by an attribute (i.e. has data => ( is => 'ro', isa => 'HashRef' );),  
and would then get the data from config, or something more complex  
(e.g. to get the data out of DBI, or another model, or whatever).

HTH
Cheers
t0m





More information about the Catalyst mailing list