Add support for load_namespaces-style class layout.
Dagfinn Ilmari Mannsåker [Sun, 27 Jan 2008 05:05:04 +0000 (05:05 +0000)]
Changes
lib/DBIx/Class/Schema/Loader/Base.pm
t/23dumpmore.t

diff --git a/Changes b/Changes
index a8e9b55..f662a1b 100644 (file)
--- 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
index 10b206d..aa6e381 100644 (file)
@@ -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<AlwaysRS>.  Component
 C<ResultSetManager> will be automatically added to the above
 C<components> list if this option is set.
 
+=head2 use_namespaces
+
+Generate result class names suitable for
+L<DBIx::Class::Schema/load_namespaces> and call that instead of
+L<DBIx::Class::Schema/load_classes>. When using this option you can also
+specify any of the options for C<load_namespaces> (i.e. C<result_namespace>,
+C<resultset_namespace>, C<default_resultset_class>), 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;
index 95d5e22..b3b3d14 100644 (file)
@@ -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); }