[Dbix-class] PATCH: load_classes_except()

Drew Taylor taylor.andrew.j at gmail.com
Fri Jul 21 19:05:36 CEST 2006


Ok, this is a VERY VERY simple patch and doesn't cover nearly the
functionality that load_classes() has, but it works for me in my
simple test. It should have unit tests and support namespaces like
it's counterpart, but you have to start somewhere. :-)

Drew

<BEGIN PATCH>

=head2 load_classes_except

=over 4

=item Arguments: @classes?

=back

This method loads all classes except for those you specify
(using L<use>), and registers them (using L</"register_class">).
It is very similar to L</"load_classes"> in that it allows
you to load all but a select few classes automatically.

Example:

  # Given classes: My::Schema::CD, My::Schema::Artist, My::Schema::Track

  # loads ::Artist and ::Track but not ::CD
  My::Schema->load_classes_except('CD');

=cut

sub load_classes_except {
  my ($class, @excluded) = @_;
  eval "require Module::Find;";
  $class->throw_exception(
    "No arguments to load_classes and couldn't load Module::Find ($@)"
  ) if $@;
  my @all_comps = map { substr $_, length "${class}::"  }
                    Module::Find::findallmod($class);
  my %excluded = map { substr $_, length "${class}::" => undef  } @excluded;
  my @comp;
  foreach my $to_check (@all_comps) {
    push(@comp, $to_check) unless exists $excluded{$to_check};
  }
  # lazy me!
  $class->load_classes(@comp);
}
<END PATCH>

-- 
----------------------------------------------------------------
 Drew Taylor                 *  Web development & consulting
 Email: drew at drewtaylor.com  *  Site implementation & hosting
 Web  : www.drewtaylor.com   *  perl/mod_perl/DBI/mysql/postgres
 ----------------------------------------------------------------



More information about the Dbix-class mailing list