[Dbix-class] [patch] additional syntax for Schema->load_classes

Hartmaier Alexander Alexander.Hartmaier at t-systems.at
Wed Nov 30 17:41:19 CET 2005


I've redone my patch cause mst didn't want to put it into trunk without tests.
now its not 99% mst's code but mine.
All current tests are ok and I've changed Schema.pm from the test suite to use 
the new syntax too.

If someone has an idea to use the current db with a new schema to explicitly 
test the different syntaxes please contribute.

#svn diff

Index: t/lib/DBICTest/Schema.pm
===================================================================
--- t/lib/DBICTest/Schema.pm    (revision 344)
+++ t/lib/DBICTest/Schema.pm    (working copy)
@@ -3,6 +3,24 @@
 use base qw/DBIx::Class::Schema/;

 __PACKAGE__->load_classes(qw/
-  Artist CD Track Tag LinerNotes OneKey TwoKeys FourKeys SelfRef SelfRefAlias 
/);
+  Artist
+  CD
+  #dummy
+  Track
+  Tag
+  /,
+  { 'DBICTest::Schema' => [qw/
+    LinerNotes
+    OneKey
+    #dummy
+    TwoKeys
+  /]},
+  (
+    'FourKeys',
+    '#dummy',
+    'SelfRef',
+  ),
+  qw/SelfRefAlias/
+);

 1;
Index: lib/DBIx/Class/Schema.pm
===================================================================
--- lib/DBIx/Class/Schema.pm    (revision 344)
+++ lib/DBIx/Class/Schema.pm    (working copy)
@@ -85,28 +85,58 @@
   return values %{shift->class_registrations};
 }

-=item  load_classes [<classes>}
+=item  load_classes [<classes>, (<class>, <class>), {<namespace> => 
[<classes>]}]

 Uses L<Module::Find> to find all classes under the database class' namespace,
 or uses the classes you select.  Then it loads the component (using L<use>),
 and registers them (using B<register_class>);

+It is possible to comment out classes with a leading '#'.
+
 =cut

 sub load_classes {
-  my $class = shift;
-  my @comp = grep { $_ !~ /^#/ } @_;
-  unless (@comp) {
+  my ($class, @params) = @_;
+
+  my %comps_for;
+
+  if (@params) {
+    foreach my $param (@params) {
+      if (ref $param eq 'ARRAY') {
+        # filter out commented entries
+        my @modules = grep { $_ !~ /^#/ } @$param;
+
+        push (@{$comps_for{$class}}, @modules);
+      }
+      elsif (ref $param eq 'HASH') {
+        # more than one namespace possible
+        for my $comp ( keys %$param ) {
+          # filter out commented entries
+          my @modules = grep { $_ !~ /^#/ } @{$param->{$comp}};
+
+          push (@{$comps_for{$comp}}, @modules);
+        }
+      }
+      else {
+        # filter out commented entries
+        push (@{$comps_for{$class}}, $param) if $param !~ /^#/;
+      }
+    }
+  } else {
     eval "require Module::Find;";
     $class->throw("No arguments to load_classes and couldn't load".
       " Module::Find ($@)") if $@;
-    @comp = map { substr $_, length "${class}::"  }
-              Module::Find::findallmod($class);
+    my @comp = map { substr $_, length "${class}::"  } 
Module::Find::findallmod($class);
+    $comps_for{$class} = \@comp;
   }
-  foreach my $comp (@comp) {
-    my $comp_class = "${class}::${comp}";
-    eval "use $comp_class"; # If it fails, assume the user fixed it
-    $class->register_class($comp => $comp_class);
+
+  foreach my $prefix (keys %comps_for) {
+    foreach my $comp (@{$comps_for{$prefix}||[]}) {
+      my $comp_class = "${prefix}::${comp}";
+      print "$comp_class\n";
+      eval "use $comp_class"; # If it fails, assume the user fixed it
+      $class->register_class($comp => $comp_class);
+    }
   }
 }

-Alex
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 5544 bytes
Desc: not available
Url : http://lists.rawmode.org/pipermail/dbix-class/attachments/20051130/9d1f5618/smime.bin


More information about the Dbix-class mailing list