perlquestion
Solo
<p>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.
</p>
<p>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):
</p>
<p><code>
package MyApp::Schema::DB::Result::Table;
...
extends 'DBIx::Class::Core';
...
sub TO_JSON {
return { $_[0]->get_inflated_columns };
}
</code><p>
<p>and extend Catalyst::View::JSON with some <a href="http://cpansearch.perl.org/src/ZDK/CatalystX-OAuth2-Provider-0.0005/t/lib/TestApp/View/JSON.pm">code I found</a>. (I've stumbled on different approaches since--opinions on best approach for this also welcomed):
</p>
<p><code>
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 );
}
</code></p>
<p>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...
</p>
<p><code>
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';
...
</code></p>
<p>Catalyst doesn't like this approach, yet, and I'm having a hard time figuring out which doc to read.
</p>
<p>Do I just need to tell Catalyst to ignore the '...::Base' class when loading namespaces (or whatever)?
</p>
<p>Or should I be doing this some other way entirely?
</p>
<div class="pmsig"><div class="pmsig-195975">
<p>--Solo
<font size=1><i><blockquote>--<br>
You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.
</blockquote></i></font>
</div></div>