Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moose method modifiers on DBIx::Class::Schema models in Catalyst

For any given result class MySchema::Result::Foo (built from default schema loader generated syntax which uses Moose/MooseX::nonmoose)

If I add a BUILDARGS method wrapper to sanitize the constructor data for a row like so:

package MySchema::Result::Foo;
use Moose;
use MooseX::NonMoose;
[etc ..]

around 'BUILDARGS' => sub {
   my $orig = shift;
   my $class = shift;
   delete $_[0]->{not_a_real_column};
   return $class->$orig(@_);
};

It works when using the schema directly. For example the following works as expected: A new row object is created with real_column=>'value' and not_a_real_column removed before ->new is called

use MySchema;
my $s = MySchema->connect('dbi:blahblahblah');
$s->resultset('Foo')->new({ real_column=>'value', not_a_real_column=>'some other thing' }); #win

However, when using the same schema via Catalyst::Model::DBIC::Schema the order is different. The following fails when trying to create a new Foo row object because not_a_real_column is invalid. In other words the arguments to new are not run through BUILDARGS before ->new is called.

$c->model('MySchemaModel')->resultset('Foo')->new({ real_column=>'value', not_a_real_column=>'some other thing' }); #fails

Interestingly enough, if I wrap around 'new' => sub{} instead of around 'BUILDARGS' => sub{} the behavior is the same in both cases, and works fine, but to my understanding Moose dogma states to never mess with new.

Anyone care to help me understand why this is the case, or if there's a better way?

like image 836
nebulous Avatar asked Dec 07 '10 14:12

nebulous


1 Answers

I see, you are using MooseX::NonMoose.

Given that, I have a guess that you need to use FOREIGNBUILDARGS

around 'FOREGINBUILDARGS' => sub {
   my $orig = shift;
   my $class = shift;
   delete $_[0]->{not_a_real_column};
   return $class->$orig(@_);
};

"MooseX::NonMoose allows you to manipulate the argument list that gets passed to the superclass constructor by defining a FOREIGNBUILDARGS method."
http://metacpan.org/pod/MooseX::NonMoose

I really hope this works for you!

like image 104
KateYoak Avatar answered Sep 20 '22 13:09

KateYoak