From: Dagfinn Ilmari Mannsåker Date: Sat, 5 Apr 2008 00:18:50 +0000 (+0000) Subject: - Move loader_class from Schema::Loader::Base to Schema::Loader X-Git-Tag: 0.04999_05~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=29ddb54c1face495013d878a6386efa25c32d6ea;p=dbsrgits%2FDBIx-Class-Schema-Loader.git - Move loader_class from Schema::Loader::Base to Schema::Loader - Allow specifying loader_class in the connection info (primariy for make_schema_at) - Add more tests --- diff --git a/Changes b/Changes index b741b40..06d912c 100644 --- a/Changes +++ b/Changes @@ -3,7 +3,8 @@ Revision history for Perl extension DBIx::Class::Schema::Loader Not yet released - Fix limiting table list to the specified schema for DB2 - Default db_schema to the username for DB2 - - Allow specifying a custom loader_class in loader_options + - Allow specifying a custom loader_class, overriding the + storage_type-based detection 0.04999_04 Wed Mar 12, 2008 - Add is_auto_increment detecton for DB2 diff --git a/lib/DBIx/Class/Schema/Loader.pm b/lib/DBIx/Class/Schema/Loader.pm index 7965299..d1e3f46 100644 --- a/lib/DBIx/Class/Schema/Loader.pm +++ b/lib/DBIx/Class/Schema/Loader.pm @@ -14,7 +14,7 @@ use Scalar::Util qw/ weaken /; our $VERSION = '0.04999_04'; __PACKAGE__->mk_classaccessor('_loader_args' => {}); -__PACKAGE__->mk_classaccessors(qw/dump_to_dir _loader_invoked _loader/); +__PACKAGE__->mk_classaccessors(qw/dump_to_dir _loader_invoked _loader loader_class/); =head1 NAME @@ -69,6 +69,16 @@ the road. =head1 METHODS +=head2 loader_class + +Set the loader class to be instantiated when L is called. +If the classname starts with "::", "DBIx::Class::Schema::Loader" is +prepended. Defaults to L (which must +start with "::" when using L). + +This is mostly useful for subclassing existing loaders or in conjunction +with L. + =head2 loader_options Example in Synopsis above demonstrates a few common arguments. For @@ -106,8 +116,9 @@ sub _invoke_loader { $args->{dump_directory} ||= $self->dump_to_dir; # XXX this only works for relative storage_type, like ::DBI ... - my $impl = $args->{loader_class} + my $impl = $self->loader_class || "DBIx::Class::Schema::Loader" . $self->storage_type; + $impl = "DBIx::Class::Schema::Loader${impl}" if $impl =~ /^::/; $impl->require or croak qq/Could not load storage_type loader "$impl": / . qq/"$UNIVERSAL::require::ERROR"/; @@ -123,9 +134,10 @@ sub _invoke_loader { See L for basic usage. -If the final argument is a hashref, and it contains a key C, -that key will be deleted, and its value will be used for the loader options, -just as if set via the L method above. +If the final argument is a hashref, and it contains the keys C +or C, those keys will be deleted, and their values value will be +used for the loader options or class, respectively, just as if set via the +L or L methods above. The actual auto-loading operation (the heart of this module) will be invoked as soon as the connection information is defined. @@ -136,10 +148,12 @@ sub connection { my $self = shift; if($_[-1] && ref $_[-1] eq 'HASH') { - if(my $loader_opts = delete $_[-1]->{loader_options}) { - $self->loader_options($loader_opts); - pop @_ if !keys %{$_[-1]}; + for my $option (qw/ loader_class loader_options /) { + if(my $value = delete $_[-1]->{$option}) { + $self->$option($value); + } } + pop @_ if !keys %{$_[-1]}; } $self = $self->next::method(@_); diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index a45d654..30e4c9b 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -62,14 +62,7 @@ classes, and implements the common functionality between them. =head1 CONSTRUCTOR OPTIONS These constructor options are the base options for -L. Available constructor options are: - -=head2 loader_class - -Use the specified class as the loader instead of -C. This is mostly useful for -subclassing existing loaders or in conjunction with -L. +L. Available constructor options are: =head2 skip_relationships diff --git a/t/24loader_subclass.t b/t/24loader_subclass.t index 43de59b..143c5db 100644 --- a/t/24loader_subclass.t +++ b/t/24loader_subclass.t @@ -4,14 +4,39 @@ use Test::More; use lib qw(t/lib); use make_dbictest_db; -{ - package DBICTest::Schema; - use base qw/ DBIx::Class::Schema::Loader /; - __PACKAGE__->loader_options( loader_class => 'TestLoaderSubclass' ); -} +my %loader_class = ( 'TestLoaderSubclass' => 'TestLoaderSubclass', + '::DBI::SQLite' => 'DBIx::Class::Schema::Loader::DBI::SQLite' + ); + +my %invocations = ( + loader_class => sub { + package DBICTest::Schema::1; + use base qw/ DBIx::Class::Schema::Loader /; + __PACKAGE__->loader_class(shift); + __PACKAGE__->connect($make_dbictest_db::dsn); + }, + connect_info => sub { + package DBICTeset::Schema::2; + use base qw/ DBIx::Class::Schema::Loader /; + __PACKAGE__->connect($make_dbictest_db::dsn, { loader_class => shift }); + }, + make_schema_at => sub { + use DBIx::Class::Schema::Loader qw/ make_schema_at /; + make_schema_at( + 'DBICTeset::Schema::3', + { }, + [ $make_dbictest_db::dsn, { loader_class => shift } ] + ); + } +); -plan tests => 2; +# one test per invocation/class combo +plan tests => keys(%invocations) * keys(%loader_class); -my $schema = DBICTest::Schema->connect($make_dbictest_db::dsn); -isa_ok($schema->storage, 'DBIx::Class::Storage::DBI::SQLite'); -isa_ok($schema->_loader, 'TestLoaderSubclass'); +while (my ($style,$subref) = each %invocations) { + while (my ($arg, $class) = each %loader_class) { + my $schema = $subref->($arg); + $schema = $schema->clone unless ref $schema; + isa_ok($schema->_loader, $class, "$style($arg)"); + } +}