cycle tests and a weaken call
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Componentised.pm
1 package # hide from PAUSE
2     DBIx::Class::Componentised;
3
4 use strict;
5 use warnings;
6
7 use Class::C3;
8
9 sub inject_base {
10   my ($class, $target, @to_inject) = @_;
11   {
12     no strict 'refs';
13     my %seen;
14     unshift( @{"${target}::ISA"},
15         grep { !$seen{ $_ }++ && $target ne $_ && !$target->isa($_) }
16             @to_inject
17     );
18   }
19
20   # Yes, this is hack. But it *does* work. Please don't submit tickets about
21   # it on the basis of the comments in Class::C3, the author was on #dbix-class
22   # while I was implementing this.
23
24   my $table = { Class::C3::_dump_MRO_table };
25   eval "package $target; import Class::C3;" unless exists $table->{$target};
26 }
27
28 sub load_components {
29   my $class = shift;
30   my $base = $class->component_base_class;
31   my @comp = map { /^\+(.*)$/ ? $1 : "${base}::$_" } grep { $_ !~ /^#/ } @_;
32   $class->_load_components(@comp);
33   Class::C3::reinitialize();
34 }
35
36 sub load_own_components {
37   my $class = shift;
38   my @comp = map { "${class}::$_" } grep { $_ !~ /^#/ } @_;
39   $class->_load_components(@comp);
40 }
41
42 sub _load_components {
43   my ($class, @comp) = @_;
44   foreach my $comp (@comp) {
45     eval "use $comp";
46     die $@ if $@;
47   }
48   $class->inject_base($class => @comp);
49 }
50
51 1;