From: Dagfinn Ilmari Mannsåker Date: Sun, 27 Jan 2008 05:05:04 +0000 (+0000) Subject: Add support for load_namespaces-style class layout. X-Git-Tag: 0.04006~24 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f44ecc2f8f07575120aa7a9e3ee3b7bdada42837;p=dbsrgits%2FDBIx-Class-Schema-Loader.git Add support for load_namespaces-style class layout. --- diff --git a/Changes b/Changes index a8e9b55..f662a1b 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for Perl extension DBIx::Class::Schema::Loader Not yet released + - Add support for load_namespaces-style class layout. - Fix test skip count for main skip_rels block 0.04004 Thu Nov 15, 2007 diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index 10b206d..aa6e381 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -35,6 +35,10 @@ __PACKAGE__->mk_ro_accessors(qw/ dump_directory dump_overwrite really_erase_my_files + use_namespaces + result_namespace + resultset_namespace + default_resultset_class db_schema _tables @@ -141,6 +145,15 @@ classes. A good example would be C. Component C will be automatically added to the above C list if this option is set. +=head2 use_namespaces + +Generate result class names suitable for +L and call that instead of +L. When using this option you can also +specify any of the options for C (i.e. C, +C, C), and they will be added +to the call (and the generated result class names adjusted appropriately). + =head2 dump_directory This option is designed to be a tool to help you transition from this @@ -440,8 +453,26 @@ sub _dump_to_dir { my $schema_text = qq|package $schema_class;\n\n| . qq|use strict;\nuse warnings;\n\n| - . qq|use base 'DBIx::Class::Schema';\n\n| - . qq|__PACKAGE__->load_classes;\n|; + . qq|use base 'DBIx::Class::Schema';\n\n|; + + + if ($self->use_namespaces) { + $schema_text .= qq|__PACKAGE__->load_namespaces|; + my $namespace_options; + for my $attr (qw(result_namespace + resultset_namespace + default_resultset_class)) { + if ($self->$attr) { + $namespace_options .= qq| $attr => '| . $self->$attr . qq|',\n| + } + } + $schema_text .= qq|(\n$namespace_options)| if $namespace_options; + $schema_text .= qq|;\n|; + } + else { + $schema_text .= qq|__PACKAGE__->load_classes;\n|; + + } $self->_write_classfile($schema_class, $schema_text); @@ -569,7 +600,19 @@ sub _make_src_class { my $schema_class = $self->schema_class; my $table_moniker = $self->_table2moniker($table); - my $table_class = $schema_class . q{::} . $table_moniker; + my @result_namespace = ($schema_class); + if ($self->use_namespaces) { + my $result_namespace = $self->result_namespace || 'Result'; + if ($result_namespace =~ /^\+(.*)/) { + # Fully qualified namespace + @result_namespace = ($1) + } + else { + # Relative namespace + push @result_namespace, $result_namespace; + } + } + my $table_class = join(q{::}, @result_namespace, $table_moniker); my $table_normalized = lc $table; $self->classes->{$table} = $table_class; diff --git a/t/23dumpmore.t b/t/23dumpmore.t index 95d5e22..b3b3d14 100644 --- a/t/23dumpmore.t +++ b/t/23dumpmore.t @@ -7,7 +7,7 @@ require DBIx::Class::Schema::Loader; $^O eq 'MSWin32' ? plan(skip_all => "ActiveState perl produces additional warnings, and this test uses unix paths") - : plan(tests => 40); + : plan(tests => 82); my $DUMP_PATH = './t/_dump'; @@ -169,4 +169,96 @@ do_dump_test( }, ); +do_dump_test( + classname => 'DBICTest::DumpMore::1', + options => { use_namespaces => 1 }, + error => '', + warnings => [ + qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, + qr/Schema dump completed/, + ], + regexes => { + schema => [ + qr/package DBICTest::DumpMore::1;/, + qr/->load_namespaces/, + ], + 'Result/Foo' => [ + qr/package DBICTest::DumpMore::1::Result::Foo;/, + qr/->set_primary_key/, + qr/1;\n$/, + ], + 'Result/Bar' => [ + qr/package DBICTest::DumpMore::1::Result::Bar;/, + qr/->set_primary_key/, + qr/1;\n$/, + ], + }, +); + +do_dump_test( + classname => 'DBICTest::DumpMore::1', + options => { use_namespaces => 1, + result_namespace => 'Res', + resultset_namespace => 'RSet', + default_resultset_class => 'RSetBase', + }, + error => '', + warnings => [ + qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, + qr/Schema dump completed/, + ], + regexes => { + schema => [ + qr/package DBICTest::DumpMore::1;/, + qr/->load_namespaces/, + qr/result_namespace => 'Res'/, + qr/resultset_namespace => 'RSet'/, + qr/default_resultset_class => 'RSetBase'/, + ], + 'Res/Foo' => [ + qr/package DBICTest::DumpMore::1::Res::Foo;/, + qr/->set_primary_key/, + qr/1;\n$/, + ], + 'Res/Bar' => [ + qr/package DBICTest::DumpMore::1::Res::Bar;/, + qr/->set_primary_key/, + qr/1;\n$/, + ], + }, +); + +do_dump_test( + classname => 'DBICTest::DumpMore::1', + options => { use_namespaces => 1, + result_namespace => '+DBICTest::DumpMore::1::Res', + resultset_namespace => 'RSet', + default_resultset_class => 'RSetBase', + }, + error => '', + warnings => [ + qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, + qr/Schema dump completed/, + ], + regexes => { + schema => [ + qr/package DBICTest::DumpMore::1;/, + qr/->load_namespaces/, + qr/result_namespace => '\+DBICTest::DumpMore::1::Res'/, + qr/resultset_namespace => 'RSet'/, + qr/default_resultset_class => 'RSetBase'/, + ], + 'Res/Foo' => [ + qr/package DBICTest::DumpMore::1::Res::Foo;/, + qr/->set_primary_key/, + qr/1;\n$/, + ], + 'Res/Bar' => [ + qr/package DBICTest::DumpMore::1::Res::Bar;/, + qr/->set_primary_key/, + qr/1;\n$/, + ], + }, +); + END { rmtree($DUMP_PATH, 1, 1); }