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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.