Re: [Catalyst] Cross-link: How do I turn Moose objects into JSON for use in Catalyst?

2010-08-03 Thread Alex J. G. Burzyński
Hi,

On 03/08/10 16:31, Evan Carroll wrote:
 I've posted a question and a few possible fixes. Does anyone have
 anything to add here? How do you go about dumping Moose to JSON in
 Cat?

 http://stackoverflow.com/questions/3391967/how-do-i-turn-moose-objects-into-json-for-use-in-catalyst

   

That's the method I'm using to dump my classes for AJAX-driven control
panel.

Works recursively on my other moose classes (ResultSet::* classes from
DBIx::BlackBox consume MyApp::DB::ResultSet::Roles::ToJSON).

Cheers,
Alex


package MyApp::DB::ResultSet::Roles::ToJSON;

use Moose::Role;

sub TO_JSON {
my $self = shift;

my $json_hash = {};

for my $attr ( $self-meta-get_all_attributes ) {
my $name = $attr-name;
$json_hash-{ $name } = $self-$name;
}

return $json_hash;
}
 
package MyApp::DB::ResultSet::SuppliersList;

use Moose;

with qw(
MyApp::DB::ResultSet::Roles::ToJSON
); 
   
use namespace::autoclean;
   
has 'org_id' = (
is = 'rw',
isa = 'Str',
required = 1,
);
has 'org_name' = (
is = 'rw',
isa = 'Str',
required = 1,
);
has 'number_of_courses' = (
is = 'rw',
isa = 'Int',
default = 0,
required = 1,
); 
has 'number_of_enabled_courses' = (
is = 'rw',
isa = 'Int',
default = 0,
required = 1,
);


package MyApp::View::JSON;

use strict;
use base 'Catalyst::View::JSON';

use JSON::XS ();

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);
}

package MyApp::Controller::Foo;
...

sub list_suppliers :Local {
my ( $self, $c ) = @_;

$c-stash( json = []);

my $rs = $c-model('DB')-exec('ListSuppliers');
do {
while ( my $supplier = $rs-next_row ) {
push @{ $c-stash-{json} }, $supplier;
}
} while ( $rs-next_resultset );

$c-forward('View::JSON');
}

___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


Re: Follow Up: [Catalyst] Alternatives to DBIx:Class?

2010-04-19 Thread Alex J. G. Burzyński

Hi,

On 2010-04-19 22:37, John Karr wrote:

Most of the responses to this thread seem to say that DBIC is worth the
effort. I looked at Fey:SQL and SQL::DB and concluded that they also require
some effort, and suffer (along with DBIC) from what is for me a huge issue
-- the documentation focuses on telling you how each piece works rather than
on how to drive the darn thing.

At this point for everything I'm working on, I have the luxury that I can
write all of my joins and complex statements into a view or
stored_procedure, but in the real world programmers are too frequently
denied this, so even though right now I would like something that just
wrapped the DBI up in a manner that made it easy to write and keep the DRY
principle, it would be potentially more valuable to learn DBIC or Fey. DBIC
is the more widely adapted solution, while Fey seems to offer most of the
same capability with a syntax that draws on SQL (which is a lot of points in
its' favor), but has less momentum and lacks equivalents of some of the
helper scripts that are available for DBIC.

So let me ask a follow up: What materials would you provide to an
Intermediate Level Programmer to help them learn either Fey or DBIC?
Materials could be working code, articles, things in documentation,
documentation for other things that happens to explain it well, chapters in
books, etc.

Afternote. There is a tutorial in Fey::ORM's documentation, but it is more
of a quick run-through. DBIx::Class has a better introduction in its'
documentation, but that hasn't helped me much. The tutorial Kennedy Clark
wrote for the Catalyst Manual was how I figured out Catalyst. Again
developer isn't my normal job title, I will probably never get paid for
knowing anything about ORM, time I spend learning it is only a benefit if it
gives me pleasure (not so far) or saves me time in the long run (the clock
is running the other way).

   



Since you've mentioned stored procedures I'm letting myself to mention 
DBIx::BlackBox.


I've created it at $work to ease the integration of my Catalyst app with 
existing MS SQL database with hundreds of tables, views and stored 
procedures.


The main driver was that I don't really want to learn all relationships 
between those tables + I've got very skilled DBA in my team who already 
knows this database :)


So all those complex queries can happily live and be optimized at 
database level, while I can deal with data returned by those stored 
procedures.


But I'm a huge fan of DBIx::Class and I can assure you that time spent 
on learning it won't be wasted, because maybe with your next job there 
won't be so complex queries and typing SELECT t1.col1, t2.col2 FROM tab1 
JOIN tab2 all over the place is just a waste of time if you can simply 
write $tab1-col1, $tab1-tab2-col2.


Regarding documentation I've found DBIx::Class man pages good enough + 
most of the Catalyst examples/blogs are bundled with DBIC ones.


Cheers,
Alex



___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/