Skip to content

Commit

Permalink
Made source_name read-only on source instances, r/w on classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ashb committed Nov 26, 2006
1 parent 362c66d commit 93405cf
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/DBIx/Class/ResultSource.pm
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion lib/DBIx/Class/ResultSourceProxy.pm
Expand Up @@ -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(@_);
}
Expand Down
23 changes: 21 additions & 2 deletions lib/DBIx/Class/Schema.pm
Expand Up @@ -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};
Expand All @@ -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
Expand Down Expand Up @@ -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 ]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion t/60core.t
Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions t/68inflate.t
Expand Up @@ -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 } }
);
Expand Down

0 comments on commit 93405cf

Please sign in to comment.