[Bast-commits] r4697 - in DBIx-Class/0.08/trunk: . lib/DBIx/Class t
lukes at dev.catalyst.perl.org
lukes at dev.catalyst.perl.org
Thu Jul 31 12:15:58 BST 2008
Author: lukes
Date: 2008-07-31 12:15:58 +0100 (Thu, 31 Jul 2008)
New Revision: 4697
Added:
DBIx-Class/0.08/trunk/t/100extra_source.t
Modified:
DBIx-Class/0.08/trunk/Changes
DBIx-Class/0.08/trunk/lib/DBIx/Class/Schema.pm
Log:
added register_extra_source
Modified: DBIx-Class/0.08/trunk/Changes
===================================================================
--- DBIx-Class/0.08/trunk/Changes 2008-07-30 13:50:17 UTC (rev 4696)
+++ DBIx-Class/0.08/trunk/Changes 2008-07-31 11:15:58 UTC (rev 4697)
@@ -1,6 +1,7 @@
Revision history for DBIx::Class
0.08099_04 2008-07-24 01:00:00
+ - Added register_extra_source methods for additional sources
- Added datetime_undef_if_invalid for InflateColumn::DateTime to
return undef on invalid date/time values
- Added search_related_rs method to ResultSet
Modified: DBIx-Class/0.08/trunk/lib/DBIx/Class/Schema.pm
===================================================================
--- DBIx-Class/0.08/trunk/lib/DBIx/Class/Schema.pm 2008-07-30 13:50:17 UTC (rev 4696)
+++ DBIx-Class/0.08/trunk/lib/DBIx/Class/Schema.pm 2008-07-31 11:15:58 UTC (rev 4697)
@@ -97,8 +97,33 @@
=cut
sub register_source {
- my ($self, $moniker, $source) = @_;
+ my $self = shift;
+ $self->_register_source(@_);
+}
+
+=head2 register_extra_source
+
+=over 4
+
+=item Arguments: $moniker, $result_source
+
+=back
+
+As L</register_source> but should be used if the result class already
+has a source and you want to register an extra one.
+
+=cut
+
+sub register_extra_source {
+ my $self = shift;
+
+ $self->_register_source(@_, { extra => 1 });
+}
+
+sub _register_source {
+ my ($self, $moniker, $source, $params) = @_;
+
%$source = %{ $source->new( { %$source, source_name => $moniker }) };
my %reg = %{$self->source_registrations};
@@ -107,9 +132,14 @@
$source->schema($self);
+ return if ($params->{extra});
+
weaken($source->{schema}) if ref($self);
if ($source->result_class) {
my %map = %{$self->class_mappings};
+ if (exists $map{$source->result_class}) {
+ warn $source->result_class . ' already has a source, use register_extra_source for additional sources';
+ }
$map{$source->result_class} = $moniker;
$self->class_mappings(\%map);
}
@@ -883,6 +913,7 @@
foreach my $moniker ($self->sources) {
my $source = $self->source($moniker);
my $new = $source->new($source);
+ $clone->_unregister_source($moniker);
$clone->register_source($moniker => $new);
}
$clone->storage->set_schema($clone) if $clone->storage;
Added: DBIx-Class/0.08/trunk/t/100extra_source.t
===================================================================
--- DBIx-Class/0.08/trunk/t/100extra_source.t (rev 0)
+++ DBIx-Class/0.08/trunk/t/100extra_source.t 2008-07-31 11:15:58 UTC (rev 4697)
@@ -0,0 +1,48 @@
+use strict;
+use warnings;
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+
+{
+ package DBICTest::ResultSource::OtherSource;
+ use strict;
+ use warnings;
+ use base qw/DBIx::Class::ResultSource::Table/;
+}
+
+plan tests => 3;
+
+my $schema = DBICTest->init_schema();
+my $artist_source = $schema->source('Artist');
+
+my $new_source = DBICTest::ResultSource::OtherSource->new({
+ %$artist_source,
+ name => 'artist_preview',
+ _relationships => Storable::dclone( $artist_source->_relationships ),
+});
+
+$new_source->add_column('other_col' => { data_type => 'integer', default_value => 1 });
+
+my $warn = '';
+$SIG{__WARN__} = sub { $warn = shift };
+
+{
+ $schema->register_extra_source( 'artist->extra' => $new_source );
+
+ my $source = $schema->source('DBICTest::Artist');
+ is($source->source_name, 'Artist', 'original source still primary source');
+}
+
+{
+ my $new_source_name = 'Artist->preview(artist_preview)';
+ $schema->register_source( $new_source_name => $new_source );
+
+ ok(($warn =~ /DBICTest::Artist already has a source, use register_extra_source for additional sources/), 'registering extra source causes errors');
+
+ my $source = $schema->source('DBICTest::Artist');
+ is($source->source_name, $new_source_name, 'original source still primary source');
+}
+
+1;
More information about the Bast-commits
mailing list