From: Ash Berlin Date: Sun, 26 Nov 2006 16:18:42 +0000 (+0000) Subject: Made source_name read-only on source instances, r/w on classes X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=93405cf016ed24f7a9859bfe6107cd94164a6d9b;p=dbsrgits%2FDBIx-Class-Historic.git Made source_name read-only on source instances, r/w on classes --- diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index c577684..b8d8800 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -12,12 +12,13 @@ use base qw/DBIx::Class/; __PACKAGE__->mk_group_accessors('simple' => qw/_ordered_columns _columns _primaries _unique_constraints name resultset_attributes - schema from _relationships column_info_from_storage source_name - source_info/); + schema from _relationships column_info_from_storage source_info/); __PACKAGE__->mk_group_accessors('inherited' => qw/resultset_class result_class/); +__PACKAGE__->mk_group_ro_accessors('simple' => qw/source_name/); + =head1 NAME DBIx::Class::ResultSource - Result source object diff --git a/lib/DBIx/Class/ResultSourceProxy.pm b/lib/DBIx/Class/ResultSourceProxy.pm index b596e5c..696c9a5 100644 --- a/lib/DBIx/Class/ResultSourceProxy.pm +++ b/lib/DBIx/Class/ResultSourceProxy.pm @@ -5,13 +5,29 @@ use strict; use warnings; use base qw/DBIx::Class/; +use Scalar::Util qw/blessed/; +use Carp::Clan qw/^DBIx::Class/; sub iterator_class { shift->result_source_instance->resultset_class(@_) } sub resultset_class { shift->result_source_instance->resultset_class(@_) } sub result_class { shift->result_source_instance->result_class(@_) } -sub source_name { shift->result_source_instance->source_name(@_) } sub source_info { shift->result_source_instance->source_info(@_) } +sub set_inherited_ro_instance { + my $self = shift; + + croak "Cannot set @{[shift]} on an instance" if blessed $self; + + return $self->set_inherited(@_); +} + +sub get_inherited_ro_instance { + return shift->get_inherited(@_); +} + +__PACKAGE__->mk_group_accessors('inherited_ro_instance' => 'source_name'); + + sub resultset_attributes { shift->result_source_instance->resultset_attributes(@_); } diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index 1d6af1e..2c68cf1 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -94,10 +94,15 @@ moniker. sub register_source { my ($self, $moniker, $source) = @_; + + %$source = %{ $source->new( { %$source, source_name => $moniker }) }; + my %reg = %{$self->source_registrations}; $reg{$moniker} = $source; $self->source_registrations(\%reg); + $source->schema($self); + weaken($source->{schema}) if ref($self); if ($source->result_class) { my %map = %{$self->class_mappings}; @@ -106,6 +111,19 @@ sub register_source { } } +sub _unregister_source { + my ($self, $moniker) = @_; + my %reg = %{$self->source_registrations}; + + my $source = delete $reg{$moniker}; + $self->source_registrations(\%reg); + if ($source->result_class) { + my %map = %{$self->class_mappings}; + delete $map{$source->result_class}; + $self->class_mappings(\%map); + } +} + =head2 class =over 4 @@ -276,9 +294,10 @@ sub load_classes { } } $class->ensure_class_loaded($comp_class); - $comp_class->source_name($comp) unless $comp_class->source_name; - push(@to_register, [ $comp_class->source_name, $comp_class ]); + $comp = $comp_class->source_name || $comp; +# $DB::single = 1; + push(@to_register, [ $comp, $comp_class ]); } } } diff --git a/t/60core.t b/t/60core.t index b0d7ec6..3eb80df 100644 --- a/t/60core.t +++ b/t/60core.t @@ -255,7 +255,7 @@ ok($schema->storage(), 'Storage available'); cmp_ok(@artsn, '==', 4, "Four artists returned"); # make sure subclasses that don't set source_name are ok - ok($schema->source('ArtistSubclass', 'ArtistSubclass exists')); + ok($schema->source('ArtistSubclass'), 'ArtistSubclass exists'); } my $newbook = $schema->resultset( 'Bookmark' )->find(1); diff --git a/t/68inflate.t b/t/68inflate.t index 0ce901c..6cbc96d 100644 --- a/t/68inflate.t +++ b/t/68inflate.t @@ -5,15 +5,20 @@ use Test::More; use lib qw(t/lib); use DBICTest; -DBICTest::Schema::CD->add_column('year'); my $schema = DBICTest->init_schema(); +DBICTest::Schema::CD->add_column('year2'); +$DB::single = 1; eval { require DateTime }; plan skip_all => "Need DateTime for inflation tests" if $@; plan tests => 4; -DBICTest::Schema::CD->inflate_column( 'year', +$DB::single = 1; + +$schema->class('CD') +#DBICTest::Schema::CD +->inflate_column( 'year', { inflate => sub { DateTime->new( year => shift ) }, deflate => sub { shift->year } } );