[Bast-commits] r5117 - in DBIx-Class/0.08/trunk: lib/DBIx/Class t
ribasushi at dev.catalyst.perl.org
ribasushi at dev.catalyst.perl.org
Thu Nov 13 07:34:26 GMT 2008
Author: ribasushi
Date: 2008-11-13 07:34:26 +0000 (Thu, 13 Nov 2008)
New Revision: 5117
Modified:
DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSet.pm
DBIx-Class/0.08/trunk/t/68inflate_resultclass_hashrefinflator.t
DBIx-Class/0.08/trunk/t/77prefetch.t
DBIx-Class/0.08/trunk/t/97result_class.t
Log:
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()
Modified: DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSet.pm
===================================================================
--- DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSet.pm 2008-11-12 23:23:03 UTC (rev 5116)
+++ DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSet.pm 2008-11-13 07:34:26 UTC (rev 5117)
@@ -15,7 +15,7 @@
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 @@
# 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 @@
bless $self, $class;
+ $self->result_class(
+ $attrs->{result_class} || $source->resolve->result_class
+ );
+
return $self;
}
@@ -988,6 +991,14 @@
=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
Modified: DBIx-Class/0.08/trunk/t/68inflate_resultclass_hashrefinflator.t
===================================================================
--- DBIx-Class/0.08/trunk/t/68inflate_resultclass_hashrefinflator.t 2008-11-12 23:23:03 UTC (rev 5116)
+++ DBIx-Class/0.08/trunk/t/68inflate_resultclass_hashrefinflator.t 2008-11-13 07:34:26 UTC (rev 5117)
@@ -4,7 +4,6 @@
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 @@
$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 @@
{
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 @@
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;
Modified: DBIx-Class/0.08/trunk/t/77prefetch.t
===================================================================
--- DBIx-Class/0.08/trunk/t/77prefetch.t 2008-11-12 23:23:03 UTC (rev 5116)
+++ DBIx-Class/0.08/trunk/t/77prefetch.t 2008-11-13 07:34:26 UTC (rev 5117)
@@ -348,7 +348,6 @@
# (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' });
Modified: DBIx-Class/0.08/trunk/t/97result_class.t
===================================================================
--- DBIx-Class/0.08/trunk/t/97result_class.t 2008-11-12 23:23:03 UTC (rev 5116)
+++ DBIx-Class/0.08/trunk/t/97result_class.t 2008-11-13 07:34:26 UTC (rev 5117)
@@ -2,20 +2,39 @@
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');
More information about the Bast-commits
mailing list