From: Peter Rabbitson Date: Thu, 13 Nov 2008 07:34:26 +0000 (+0000) Subject: result_class goodies by Caelum: X-Git-Tag: v0.08240~243 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c5bc9ba6e36d69c76fefc18a3653596a9ea13921;p=dbsrgits%2FDBIx-Class.git result_class goodies by Caelum: - result_class can now be specified as a search attribute, attaching it to the returned resultset - the specified class is now autoloaded via ensure_loaded() --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index d7c36f6..ed08ef1 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -15,7 +15,7 @@ use List::Util (); use Scalar::Util (); use base qw/DBIx::Class/; -__PACKAGE__->mk_group_accessors('simple' => qw/result_class _source_handle/); +__PACKAGE__->mk_group_accessors('simple' => qw/_result_class _source_handle/); =head1 NAME @@ -108,7 +108,6 @@ sub new { # see https://bugzilla.redhat.com/show_bug.cgi?id=196836 my $self = { _source_handle => $source, - result_class => $attrs->{result_class} || $source->resolve->result_class, cond => $attrs->{where}, count => undef, pager => undef, @@ -117,6 +116,10 @@ sub new { bless $self, $class; + $self->result_class( + $attrs->{result_class} || $source->resolve->result_class + ); + return $self; } @@ -988,6 +991,14 @@ L<"table"|DBIx::Class::Manual::Glossary/"ResultSource"> class. =cut +sub result_class { + my ($self, $result_class) = @_; + if ($result_class) { + $self->ensure_class_loaded($result_class); + $self->_result_class($result_class); + } + $self->_result_class; +} =head2 count diff --git a/t/68inflate_resultclass_hashrefinflator.t b/t/68inflate_resultclass_hashrefinflator.t index 214e11a..7138989 100644 --- a/t/68inflate_resultclass_hashrefinflator.t +++ b/t/68inflate_resultclass_hashrefinflator.t @@ -4,7 +4,6 @@ use warnings; use Test::More qw(no_plan); use lib qw(t/lib); use DBICTest; -use DBIx::Class::ResultClass::HashRefInflator; my $schema = DBICTest->init_schema(); @@ -62,6 +61,7 @@ sub check_cols_of { $schema->resultset('CD')->create({ title => 'Silence is golden', artist => 3, year => 2006 }); # order_by to ensure both resultsets have the rows in the same order +# also check result_class-as-an-attribute syntax my $rs_dbic = $schema->resultset('CD')->search(undef, { prefetch => [ qw/ artist tracks / ], @@ -72,9 +72,9 @@ my $rs_hashrefinf = $schema->resultset('CD')->search(undef, { prefetch => [ qw/ artist tracks / ], order_by => [ 'me.cdid', 'tracks.position' ], + result_class => 'DBIx::Class::ResultClass::HashRefInflator', } ); -$rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator'); my @dbic = $rs_dbic->all; my @hashrefinf = $rs_hashrefinf->all; @@ -98,8 +98,8 @@ $rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, { select => [qw/name tracks.title tracks.cd /], as => [qw/name cds.tracks.title cds.tracks.cd /], order_by => [qw/cds.cdid tracks.trackid/], + result_class => 'DBIx::Class::ResultClass::HashRefInflator', }); -$rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator'); @dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all); @hashrefinf = $rs_hashrefinf->all; diff --git a/t/77prefetch.t b/t/77prefetch.t index a91c429..608c8eb 100644 --- a/t/77prefetch.t +++ b/t/77prefetch.t @@ -348,7 +348,6 @@ is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no # (the TODO block itself contains tests ensuring that the warns are removed) TODO: { local $TODO = 'Prefetch of multiple has_many rels at the same level (currently warn to protect the clueless git)'; - use DBIx::Class::ResultClass::HashRefInflator; #( 1 -> M + M ) my $cd_rs = $schema->resultset('CD')->search ({ 'me.title' => 'Forkful of bees' }); diff --git a/t/97result_class.t b/t/97result_class.t index 7921158..0b0db50 100644 --- a/t/97result_class.t +++ b/t/97result_class.t @@ -2,20 +2,39 @@ use strict; use warnings; use Test::More; +use Test::Exception; use lib qw(t/lib); use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 9; +plan tests => 12; { my $cd_rc = $schema->resultset("CD")->result_class; + throws_ok { + $schema->resultset("Artist") + ->search_rs({}, {result_class => "IWillExplode"}) + } qr/Can't locate IWillExplode/, 'nonexistant result_class exception'; + +# to make ensure_class_loaded happy, dies on inflate + eval 'package IWillExplode; sub dummy {}'; + my $artist_rs = $schema->resultset("Artist") ->search_rs({}, {result_class => "IWillExplode"}); is($artist_rs->result_class, 'IWillExplode', 'Correct artist result_class'); - + + throws_ok { + $artist_rs->result_class('mtfnpy') + } qr/Can't locate mtfnpy/, + 'nonexistant result_access exception (from accessor)'; + + throws_ok { + $artist_rs->first + } qr/Can't locate object method "inflate_result" via package "IWillExplode"/, + 'IWillExplode explodes on inflate'; + my $cd_rs = $artist_rs->related_resultset('cds'); is($cd_rs->result_class, $cd_rc, 'Correct cd result_class');