From: Gareth Kirwan Date: Tue, 17 Jul 2012 15:49:50 +0000 (+0100) Subject: Fix Class::AccessorGroup to allow instances in get_component_class. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=91fe73bcf7e35c0c3bfa82947d5392770eef9f49;p=dbsrgits%2FDBIx-Class-Historic.git Fix Class::AccessorGroup to allow instances in get_component_class. Fixes result_class($obj) exploding instead of just using the object. --- diff --git a/lib/DBIx/Class/AccessorGroup.pm b/lib/DBIx/Class/AccessorGroup.pm index 2b0462b..30b3bd8 100644 --- a/lib/DBIx/Class/AccessorGroup.pm +++ b/lib/DBIx/Class/AccessorGroup.pm @@ -4,7 +4,7 @@ use strict; use warnings; use base qw/Class::Accessor::Grouped/; -use Scalar::Util qw/weaken/; +use Scalar::Util qw/weaken blessed/; use namespace::clean; my $successfully_loaded_components; @@ -12,6 +12,9 @@ my $successfully_loaded_components; sub get_component_class { my $class = $_[0]->get_inherited($_[1]); + # It's already an object, just go for it. + return $class if blessed $class; + if (defined $class and ! $successfully_loaded_components->{$class} ) { $_[0]->ensure_class_loaded($class); diff --git a/t/107obj_result_class.t b/t/107obj_result_class.t new file mode 100644 index 0000000..f616bcb --- /dev/null +++ b/t/107obj_result_class.t @@ -0,0 +1,35 @@ +package ResultClassInflator; + +sub new { bless {}, __PACKAGE__ } + +1; + +package main; + +use strict; +use warnings; + +use Test::More tests => 6; +use Test::Exception; +use lib qw(t/lib); +use DBICTest; + +my $schema = DBICTest->init_schema(); + +my $source = $schema->source('CD'); + +lives_ok { + $source->result_class('ResultClassInflator'); + is($source->result_class => 'ResultClassInflator', "result_class gives us back class"); + is($source->get_component_class('result_class') => 'ResultClassInflator', + "and so does get_component_class"); + + } 'Result class still works with class'; +lives_ok { + my $obj = ResultClassInflator->new(); + $source->result_class($obj); + is($source->result_class => $obj, "result_class gives us back obj"); + is($source->get_component_class('result_class') => $obj, "and so does get_component_class"); + } 'Result class works with object'; + +done_testing;