http://www.perlmonks.org?node_id=885010


in reply to instantiate objects from non-Catalyst model classes with parameters

interesting you mention this. I have just wrote a model factory based on Catalyst::Component::InstancePerContext...

package Leilao::Model::Negocio; use Moose; extends 'Catalyst::Model'; use namespace::autoclean; use Module::Find; use Module::Load; use Sub::Name; use Catalyst::Component::InstancePerContext; use Moose::Meta::Class; sub BUILD { my ($self) = @_; my @modules = findallmod 'Leilao::Negocio'; foreach my $neg (@modules) { load $neg; (my $mod = $neg) =~ s/^Leilao::Negocio/Leilao::Model::Negocio/ +; my $sub = sub { my ($self, $c) = @_; return $neg->new ( #user => $c->user, schema => $c->model('DB')->schema ); }; my $class = Moose::Meta::Class->create( $mod, superclasses => +['Catalyst::Model']); $class->add_method(build_per_context_instance => $sub); Catalyst::Component::InstancePerContext->meta->apply($class); } } __PACKAGE__->meta->make_immutable; 1;

This model will use create adapted models for every class in the Leilao::Negocio namespace using Catalyst::Component::InstancePerContext and initializing the classes with arbitrary parameters.

daniel
  • Comment on Re: instantiate objects from non-Catalyst model classes with parameters
  • Download Code