X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FComponentised.pm;h=e23a0b4a948eebc4ce8db6bbe53a1fea9bda78d0;hb=68c9468725220fe409987fa46bd73676460adb53;hp=72dd6f23fb5da5c0eb33a53ad03f91c3826342a7;hpb=227d4dee52a91339e04dd509712ac062450bbb82;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/lib/DBIx/Class/Componentised.pm b/lib/DBIx/Class/Componentised.pm index 72dd6f2..e23a0b4 100644 --- a/lib/DBIx/Class/Componentised.pm +++ b/lib/DBIx/Class/Componentised.pm @@ -1,17 +1,36 @@ -package DBIx::Class::Componentised; +package # hide from PAUSE + DBIx::Class::Componentised; + +use strict; +use warnings; + +use Class::C3; +use Class::Inspector; sub inject_base { my ($class, $target, @to_inject) = @_; { no strict 'refs'; - unshift(@{"${target}::ISA"}, grep { $target ne $_ } @to_inject); + foreach my $to (reverse @to_inject) { + unshift( @{"${target}::ISA"}, $to ) + unless ($target eq $to || $target->isa($to)); + } } + + # Yes, this is hack. But it *does* work. Please don't submit tickets about + # it on the basis of the comments in Class::C3, the author was on #dbix-class + # while I was implementing this. + + my $table = { Class::C3::_dump_MRO_table }; + eval "package $target; import Class::C3;" unless exists $table->{$target}; } sub load_components { my $class = shift; - my @comp = map { "DBIx::Class::$_" } grep { $_ !~ /^#/ } @_; + my $base = $class->component_base_class; + my @comp = map { /^\+(.*)$/ ? $1 : "${base}::$_" } grep { $_ !~ /^#/ } @_; $class->_load_components(@comp); + Class::C3::reinitialize(); } sub load_own_components { @@ -23,10 +42,20 @@ sub load_own_components { sub _load_components { my ($class, @comp) = @_; foreach my $comp (@comp) { - eval "use $comp"; - die $@ if $@; + $class->ensure_class_loaded($comp); } $class->inject_base($class => @comp); } +# TODO: handle ->has_many('rel', 'Class'...) instead of +# ->has_many('rel', 'Some::Schema::Class'...) +sub ensure_class_loaded { + my ($class, $f_class) = @_; + eval "require $f_class"; + my $err = $@; + Class::Inspector->loaded($f_class) + or die $err || "require $f_class was successful but the package". + "is not defined"; +} + 1;