http://qs321.pair.com?node_id=921416

Solo has asked for the wisdom of the Perl Monks concerning the following question:

It's difficult for me to title this question, because I'm not sure which Catalyst magic I'm breaking. I'm new to Catalyst and Moose and lost in all the pod.

I'm trying to add generic JSON support to a Catalyst model. To do so, I add a TO_JSON object to my result class(es):

package MyApp::Schema::DB::Result::Table; ... extends 'DBIx::Class::Core'; ... sub TO_JSON { return { $_[0]->get_inflated_columns }; }

and extend Catalyst::View::JSON with some code I found. (I've stumbled on different approaches since--opinions on best approach for this also welcomed):

package MyApp::View::JSON; use Moose; use JSON::XS (); extends 'Catalyst::View::JSON'; my $encoder = JSON::XS->new->utf8->pretty(0)->indent(0) ->allow_blessed(1)->convert_blessed(1); sub encode_json { my( $self, $c, $data ) = @_; $encoder->encode( $data ); }

This works for each result class individually, but I'd like it to be DRYer. So, I think to create a result base class and extend that for my result classes...

package MyApp::Schema::DB::Result::Base; use Moose; use namespace::autoclean; extends 'DBIx::Class::Core'; sub TO_JSON {...} package MyApp::Schema::DB::Result::Table; ... extends 'MyApp::Schema::DB::Result::Base'; ...

Catalyst doesn't like this approach, yet, and I'm having a hard time figuring out which doc to read.

Do I just need to tell Catalyst to ignore the '...::Base' class when loading namespaces (or whatever)?

Or should I be doing this some other way entirely?

--Solo

--
You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.

Replies are listed 'Best First'.
Re: How to add TO_JSON base method for DBIC Result in Catalyst
by Solo (Deacon) on Aug 20, 2011 at 17:10 UTC
    Castaway advised on irc.perl.org #catalyst this was a DBIx::Class issue with Base being in the same namespace/folder as the other result classes. The solution is to move the base class up or out.

    package MyApp::Schema::DB::ResultBase; extends 'DBIx::Class::Core'; ... package MyApp::Schema::DB::Result::Table; extends 'MyApp::Schema::DB::ResultBase'; ...

    Thanks!

    --Solo

    --
    You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.