clean up relbuilder choosing code
Robert Buels [Sat, 14 Aug 2010 00:18:55 +0000 (17:18 -0700)]
lib/DBIx/Class/Schema/Loader/Base.pm
lib/DBIx/Class/Schema/Loader/RelBuilder.pm

index 1219e91..d469f6c 100644 (file)
@@ -973,26 +973,26 @@ sub rescan {
 }
 
 sub _relbuilder {
-    no warnings 'uninitialized';
     my ($self) = @_;
 
     return if $self->{skip_relationships};
 
-    if ($self->naming->{relationships} eq 'v4') {
-        require DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_040;
-        return $self->{relbuilder} ||=
-            DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_040->new( $self );
-    }
-    elsif ($self->naming->{relationships} eq 'v5') {
-        require DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_05;
-        return $self->{relbuilder} ||= DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_05->new( $self );
-    }
-    elsif ($self->naming->{relationships} eq 'v6') {
-        require DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_06;
-        return $self->{relbuilder} ||= DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_06->new( $self );
-    }
+    return $self->{relbuilder} ||= do {
 
-    return $self->{relbuilder} ||= DBIx::Class::Schema::Loader::RelBuilder->new ( $self );
+        no warnings 'uninitialized';
+        my $relbuilder_suff =
+            {qw{
+                v4  ::Compat::v0_040
+                v5  ::Compat::v0_05
+                v6  ::Compat::v0_06
+            }}
+            ->{ $self->naming->{relationships}};
+
+        my $relbuilder_class = 'DBIx::Class::Schema::Loader::RelBuilder'.$relbuilder_suff;
+        eval "require $relbuilder_class"; die $@ if $@;
+        $relbuilder_class->new( $self );
+
+    };
 }
 
 sub _load_tables {
index 06edf33..be0f136 100644 (file)
@@ -4,6 +4,8 @@ use strict;
 use warnings;
 use Class::C3;
 use Carp::Clan qw/^DBIx::Class/;
+use Scalar::Util ();
+
 use Lingua::EN::Inflect::Phrase ();
 use DBIx::Class::Schema::Loader::Utils 'split_name';