From: Luke Saunders Date: Thu, 31 Jul 2008 11:15:58 +0000 (+0000) Subject: added register_extra_source X-Git-Tag: v0.08240~389 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2a4d9487f09d04cde419d6840e06b9be5a880a23;p=dbsrgits%2FDBIx-Class.git added register_extra_source --- diff --git a/Changes b/Changes index d665ab7..f08a7f3 100644 --- a/Changes +++ b/Changes @@ -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 diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index ddef110..7f7fb37 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -97,7 +97,32 @@ moniker. =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 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 }) }; @@ -107,9 +132,14 @@ sub register_source { $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 @@ sub clone { 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; diff --git a/t/100extra_source.t b/t/100extra_source.t new file mode 100644 index 0000000..ac92209 --- /dev/null +++ b/t/100extra_source.t @@ -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;